'use client'; import { Button } from '@workspace/ui/components/button'; import { Command, CommandInput, CommandItem, CommandList } from '@workspace/ui/components/command'; import { Popover, PopoverContent, PopoverTrigger } from '@workspace/ui/components/popover'; import { Icon } from '@workspace/ui/custom-components/icon'; import { cn } from '@workspace/ui/lib/utils'; import { useMemo, useState } from 'react'; interface TimezoneOption { value: string; label: string; offset: string; } function getAllTimezones(): TimezoneOption[] { try { const timeZones = Intl.supportedValuesOf('timeZone'); return [ { value: 'UTC', label: 'UTC', offset: '+00:00', }, ].concat( timeZones .map((tz) => { const parts = tz.split('/'); let label = tz; if (parts.length >= 2) { const region = parts[0]; const city = parts[1]?.replace(/_/g, ' ') || ''; label = `${city} (${region})`; } return { value: tz, label: label, offset: getTimezoneOffset(tz), }; }) .sort((a, b) => a.label.localeCompare(b.label)), ); } catch { return [ { value: 'UTC', label: 'UTC', offset: '+00:00', }, ]; } } function getTimezoneOffset(timezone: string): string { try { const now = new Date(); const utc = new Date(now.getTime() + now.getTimezoneOffset() * 60000); const targetTime = new Date(utc.toLocaleString('en-US', { timeZone: timezone })); const offset = (targetTime.getTime() - utc.getTime()) / (1000 * 60 * 60); const sign = offset >= 0 ? '+' : '-'; const hours = Math.floor(Math.abs(offset)); const minutes = Math.floor((Math.abs(offset) - hours) * 60); return `${sign}${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; } catch { return '+00:00'; } } export default function TimezoneSwitch() { const [timezone, setTimezone] = useState('UTC'); const [open, setOpen] = useState(false); const timezoneOptions = useMemo(() => getAllTimezones(), []); const handleTimezoneChange = (newTimezone: string) => { setTimezone(newTimezone); localStorage.setItem('timezone', newTimezone); setOpen(false); window.dispatchEvent( new CustomEvent('timezoneChanged', { detail: { timezone: newTimezone }, }), ); }; return ( {timezoneOptions.map((option) => ( handleTimezoneChange(option.value)} >
{option.label} {option.value} • {option.offset}
))}
); }