|
| 1 | +import { useCallback, useMemo } from 'react'; |
| 2 | +import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils'; |
| 3 | +import { |
| 4 | + Box, |
| 5 | + ComboboxData, |
| 6 | + ComboboxItem, |
| 7 | + Group, |
| 8 | + Select, |
| 9 | + SelectProps, |
| 10 | + Text, |
| 11 | +} from '@mantine/core'; |
| 12 | +import { IconCheck } from '@tabler/icons-react'; |
| 13 | + |
| 14 | +import { |
| 15 | + formatTimeZoneName, |
| 16 | + getLocalTimeZone, |
| 17 | + getSupportedTimeZones, |
| 18 | + getTimeZoneOffsetLabel, |
| 19 | + getTimeZoneShortName, |
| 20 | + LOCAL_TIME_ZONE, |
| 21 | + resolveTimeZonePreference, |
| 22 | +} from '@/timeZone'; |
| 23 | + |
| 24 | +type TimeZoneSelectProps = Omit< |
| 25 | + SelectProps, |
| 26 | + 'value' | 'onChange' | 'data' | 'renderOption' |
| 27 | +> & { |
| 28 | + /** A time zone setting: `LOCAL_TIME_ZONE` or an IANA name. */ |
| 29 | + value: string; |
| 30 | + onChange: (value: string) => void; |
| 31 | +}; |
| 32 | + |
| 33 | +/** Offsets shown next to each option, keyed by option value. */ |
| 34 | +function getOffsetLabels(zones: string[]): Map<string, string> { |
| 35 | + const localZone = getLocalTimeZone(); |
| 36 | + |
| 37 | + return new Map([ |
| 38 | + [ |
| 39 | + LOCAL_TIME_ZONE, |
| 40 | + `${getTimeZoneShortName(localZone)} · ${getTimeZoneOffsetLabel(localZone)}`, |
| 41 | + ], |
| 42 | + [UTC_TIME_ZONE, getTimeZoneOffsetLabel(UTC_TIME_ZONE)], |
| 43 | + ...zones.map(zone => [zone, getTimeZoneOffsetLabel(zone)] as const), |
| 44 | + ]); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Picks a time zone setting. Local and UTC are pinned above every zone the |
| 49 | + * browser knows. |
| 50 | + */ |
| 51 | +export function TimeZoneSelect({ |
| 52 | + value, |
| 53 | + onChange, |
| 54 | + ...props |
| 55 | +}: TimeZoneSelectProps) { |
| 56 | + const selected = resolveTimeZonePreference(value) ?? LOCAL_TIME_ZONE; |
| 57 | + |
| 58 | + const zones = useMemo(() => { |
| 59 | + const supported = getSupportedTimeZones(); |
| 60 | + |
| 61 | + const isPinned = selected === LOCAL_TIME_ZONE || selected === UTC_TIME_ZONE; |
| 62 | + |
| 63 | + const isListed = supported.includes(selected); |
| 64 | + |
| 65 | + if (isPinned || isListed) { |
| 66 | + return supported; |
| 67 | + } |
| 68 | + |
| 69 | + return [...supported, selected]; |
| 70 | + }, [selected]); |
| 71 | + |
| 72 | + const data = useMemo<ComboboxData>( |
| 73 | + () => [ |
| 74 | + { |
| 75 | + value: LOCAL_TIME_ZONE, |
| 76 | + label: `Local (${formatTimeZoneName(getLocalTimeZone())})`, |
| 77 | + }, |
| 78 | + { value: UTC_TIME_ZONE, label: 'UTC' }, |
| 79 | + { |
| 80 | + group: 'All time zones', |
| 81 | + items: zones.map(zone => ({ |
| 82 | + value: zone, |
| 83 | + label: formatTimeZoneName(zone), |
| 84 | + })), |
| 85 | + }, |
| 86 | + ], |
| 87 | + [zones], |
| 88 | + ); |
| 89 | + |
| 90 | + const offsetLabels = useMemo(() => getOffsetLabels(zones), [zones]); |
| 91 | + |
| 92 | + const renderOption = useCallback( |
| 93 | + ({ option, checked }: { option: ComboboxItem; checked?: boolean }) => ( |
| 94 | + <Group w="100%"> |
| 95 | + <Box flex={1}> |
| 96 | + <Text fz="sm">{option.label}</Text> |
| 97 | + |
| 98 | + <Text size="xs" c="dimmed"> |
| 99 | + {offsetLabels.get(option.value)} |
| 100 | + </Text> |
| 101 | + </Box> |
| 102 | + |
| 103 | + {checked && ( |
| 104 | + <IconCheck size={14} color="var(--color-text-brand)" stroke={2.5} /> |
| 105 | + )} |
| 106 | + </Group> |
| 107 | + ), |
| 108 | + [offsetLabels], |
| 109 | + ); |
| 110 | + |
| 111 | + return ( |
| 112 | + <Select |
| 113 | + searchable |
| 114 | + allowDeselect={false} |
| 115 | + maxDropdownHeight={280} |
| 116 | + nothingFoundMessage="No matching time zones" |
| 117 | + {...props} |
| 118 | + data={data} |
| 119 | + value={selected} |
| 120 | + onChange={next => next && onChange(next)} |
| 121 | + renderOption={renderOption} |
| 122 | + /> |
| 123 | + ); |
| 124 | +} |
0 commit comments