web/rightpanel: ignore timezone if value is unsupported

This commit is contained in:
Tulir Asokan 2025-01-24 01:04:28 +02:00
parent 9cff332671
commit fabf3404af

View file

@ -27,14 +27,19 @@ const currentTimeAdjusted = (tz: string) => {
timeZoneName: "short", timeZoneName: "short",
timeZone: tz, timeZone: tz,
}).format(new Date()) }).format(new Date())
} catch (e) { } catch {
return `${e}` return null
} }
} }
const ClockElement = ({ tz }: { tz: string }) => { const ClockElement = ({ tz }: { tz: string }) => {
const [time, setTime] = useState(currentTimeAdjusted(tz)) const cta = currentTimeAdjusted(tz)
const isValidTZ = cta !== null
const [time, setTime] = useState(cta)
useEffect(() => { useEffect(() => {
if (!isValidTZ) {
return
}
let interval: number | undefined let interval: number | undefined
const updateTime = () => setTime(currentTimeAdjusted(tz)) const updateTime = () => setTime(currentTimeAdjusted(tz))
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
@ -42,8 +47,11 @@ const ClockElement = ({ tz }: { tz: string }) => {
updateTime() updateTime()
}, (1001 - Date.now() % 1000)) }, (1001 - Date.now() % 1000))
return () => interval ? clearInterval(interval) : clearTimeout(timeout) return () => interval ? clearInterval(interval) : clearTimeout(timeout)
}, [tz]) }, [tz, isValidTZ])
if (!isValidTZ) {
return null
}
return <> return <>
<div title={tz}>Time:</div> <div title={tz}>Time:</div>
<div title={tz}>{time}</div> <div title={tz}>{time}</div>