|
| 1 | +import { Globe } from 'lucide-react' |
| 2 | + |
| 3 | +import { |
| 4 | + Combobox, |
| 5 | + ComboboxCollection, |
| 6 | + ComboboxContent, |
| 7 | + ComboboxEmpty, |
| 8 | + ComboboxGroup, |
| 9 | + ComboboxInput, |
| 10 | + ComboboxItem, |
| 11 | + ComboboxLabel, |
| 12 | + ComboboxList, |
| 13 | + ComboboxSeparator, |
| 14 | + useComboboxAnchor |
| 15 | +} from '@/components/ui/combobox' |
| 16 | +import { InputGroupAddon } from '@/components/ui/input-group' |
| 17 | + |
| 18 | +interface TimezoneOption { |
| 19 | + value: string |
| 20 | + label: string |
| 21 | +} |
| 22 | + |
| 23 | +interface TimezoneGroup { |
| 24 | + value: string |
| 25 | + items: TimezoneOption[] |
| 26 | +} |
| 27 | + |
| 28 | +// "GMT-5", "GMT+5:30", or "GMT" for the zone's current offset. |
| 29 | +function gmtLabel(timeZone: string): string { |
| 30 | + const name = new Intl.DateTimeFormat('en-US', { |
| 31 | + timeZone, |
| 32 | + timeZoneName: 'shortOffset' |
| 33 | + }) |
| 34 | + .formatToParts(new Date()) |
| 35 | + .find((part) => part.type === 'timeZoneName')?.value |
| 36 | + return name ?? 'GMT' |
| 37 | +} |
| 38 | + |
| 39 | +function offsetMinutes(gmt: string): number { |
| 40 | + const match = gmt.match(/GMT([+-])(\d{1,2})(?::(\d{2}))?/) |
| 41 | + if (!match) return 0 |
| 42 | + const sign = match[1] === '-' ? -1 : 1 |
| 43 | + return sign * (Number(match[2]) * 60 + Number(match[3] ?? 0)) |
| 44 | +} |
| 45 | + |
| 46 | +// Grouped by IANA area ("America", "Europe", ...), each option labelled |
| 47 | +// e.g. "America/New_York" -> "(GMT-5) New York" under the "America" group. |
| 48 | +const TIMEZONE_GROUPS: TimezoneGroup[] = (() => { |
| 49 | + const byArea = new Map<string, (TimezoneOption & { offset: number })[]>() |
| 50 | + for (const tz of Intl.supportedValuesOf('timeZone')) { |
| 51 | + const area = tz.split('/')[0] |
| 52 | + const gmt = gmtLabel(tz) |
| 53 | + const city = tz.split('/').pop()?.replace(/_/g, ' ') ?? tz |
| 54 | + const items = byArea.get(area) ?? [] |
| 55 | + items.push({ |
| 56 | + value: tz, |
| 57 | + label: `(${gmt}) ${city}`, |
| 58 | + offset: offsetMinutes(gmt) |
| 59 | + }) |
| 60 | + byArea.set(area, items) |
| 61 | + } |
| 62 | + return [...byArea.entries()] |
| 63 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 64 | + .map(([area, items]) => ({ |
| 65 | + value: area, |
| 66 | + items: items |
| 67 | + .sort((a, b) => a.offset - b.offset || a.label.localeCompare(b.label)) |
| 68 | + .map(({ value, label }) => ({ value, label })) |
| 69 | + })) |
| 70 | +})() |
| 71 | + |
| 72 | +const TIMEZONE_OPTIONS = TIMEZONE_GROUPS.flatMap((group) => group.items) |
| 73 | + |
| 74 | +interface TimezoneComboboxProps { |
| 75 | + id?: string |
| 76 | + value: string |
| 77 | + onValueChange: (value: string) => void |
| 78 | +} |
| 79 | + |
| 80 | +export function TimezoneCombobox({ |
| 81 | + id, |
| 82 | + value, |
| 83 | + onValueChange |
| 84 | +}: TimezoneComboboxProps) { |
| 85 | + const anchor = useComboboxAnchor() |
| 86 | + const selected = TIMEZONE_OPTIONS.find((tz) => tz.value === value) ?? null |
| 87 | + |
| 88 | + return ( |
| 89 | + <Combobox |
| 90 | + id={id} |
| 91 | + items={TIMEZONE_GROUPS} |
| 92 | + value={selected} |
| 93 | + onValueChange={(option) => onValueChange(option?.value ?? '')} |
| 94 | + > |
| 95 | + <div ref={anchor}> |
| 96 | + <ComboboxInput placeholder="Select timezone"> |
| 97 | + <InputGroupAddon> |
| 98 | + <Globe className="size-4" /> |
| 99 | + </InputGroupAddon> |
| 100 | + </ComboboxInput> |
| 101 | + </div> |
| 102 | + <ComboboxContent anchor={anchor} className="min-w-0"> |
| 103 | + <ComboboxEmpty>No timezone found.</ComboboxEmpty> |
| 104 | + <ComboboxList> |
| 105 | + {(group: TimezoneGroup, index: number) => ( |
| 106 | + <ComboboxGroup key={group.value} items={group.items}> |
| 107 | + <ComboboxLabel>{group.value}</ComboboxLabel> |
| 108 | + <ComboboxCollection> |
| 109 | + {(tz: TimezoneOption) => ( |
| 110 | + <ComboboxItem key={tz.value} value={tz}> |
| 111 | + {tz.label} |
| 112 | + </ComboboxItem> |
| 113 | + )} |
| 114 | + </ComboboxCollection> |
| 115 | + {index < TIMEZONE_GROUPS.length - 1 && <ComboboxSeparator />} |
| 116 | + </ComboboxGroup> |
| 117 | + )} |
| 118 | + </ComboboxList> |
| 119 | + </ComboboxContent> |
| 120 | + </Combobox> |
| 121 | + ) |
| 122 | +} |
0 commit comments