diff --git a/.changeset/time-zone-select.md b/.changeset/time-zone-select.md new file mode 100644 index 0000000000..258d5743f4 --- /dev/null +++ b/.changeset/time-zone-select.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +feat: Pick any time zone in User Preferences, replacing the "Use UTC time" switch. Local and UTC stay pinned at the top of the list diff --git a/packages/app/src/UserPreferencesModal.tsx b/packages/app/src/UserPreferencesModal.tsx index 3c46ad17c2..c5c56cac5e 100644 --- a/packages/app/src/UserPreferencesModal.tsx +++ b/packages/app/src/UserPreferencesModal.tsx @@ -1,6 +1,5 @@ import * as React from 'react'; import { Badge } from '@clickhouse/click-ui'; -import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils'; import { Autocomplete, Divider, @@ -8,11 +7,11 @@ import { Modal, Select, Stack, - Switch, Text, Tooltip, } from '@mantine/core'; +import { TimeZoneSelect } from './components/TimeZoneSelect'; import { isContentFontSize, OPTIONS_CONTENT_FONT_SIZE, @@ -20,7 +19,6 @@ import { } from './config/fonts'; import { useAppTheme } from './theme/ThemeProvider'; import { isValidThemeName, themes } from './theme'; -import { LOCAL_TIME_ZONE } from './timeZone'; import { DEFAULT_ROW_CLICK_ACTION, RowClickAction, @@ -60,7 +58,7 @@ const SettingContainer = ({
{label} {!!description && ( - + {description} )} @@ -112,18 +110,14 @@ export const UserPreferencesModal = ({ allowDeselect={false} /> - - - setUserPreference({ - timeZone: e.currentTarget.checked - ? UTC_TIME_ZONE - : LOCAL_TIME_ZONE, - }) - } + + setUserPreference({ timeZone })} /> @@ -194,11 +188,11 @@ export const UserPreferencesModal = ({ {/* Brand Theme Selector - DEV MODE ONLY - + This is intentionally NOT available in production. Brand theme (HyperDX vs ClickStack) is deployment-configured via NEXT_PUBLIC_THEME environment variable. Each deployment is branded for a specific product; users don't choose this. - + This dev-only UI exists for testing theme implementations during development. */} {isDev && ( diff --git a/packages/app/src/__tests__/UserPreferencesModal.test.tsx b/packages/app/src/__tests__/UserPreferencesModal.test.tsx index 72868fb7e4..ef1b72a446 100644 --- a/packages/app/src/__tests__/UserPreferencesModal.test.tsx +++ b/packages/app/src/__tests__/UserPreferencesModal.test.tsx @@ -27,6 +27,25 @@ const readStoredPreferences = (): UserPreferences => JSON.parse(window.localStorage.getItem(STORAGE_KEY) ?? '{}'); describe('UserPreferencesModal', () => { + describe('Time zone', () => { + it('reflects a stored zone', () => { + renderModal({ timeZone: 'Etc/UTC' }); + + expect(screen.getByTestId('time-zone-select')).toHaveValue('UTC'); + }); + + it('stores the selected zone', async () => { + renderModal({ timeZone: 'local' }); + + await userEvent.click(screen.getByTestId('time-zone-select')); + await userEvent.click( + await screen.findByRole('option', { name: /^UTC/, hidden: true }), + ); + + expect(readStoredPreferences().timeZone).toBe('Etc/UTC'); + }); + }); + describe('Row click', () => { it('shows the default action for users with no stored preference', () => { renderModal(); diff --git a/packages/app/src/__tests__/timeZone.test.ts b/packages/app/src/__tests__/timeZone.test.ts index 4c039440cf..b2022b77bf 100644 --- a/packages/app/src/__tests__/timeZone.test.ts +++ b/packages/app/src/__tests__/timeZone.test.ts @@ -1,4 +1,7 @@ import { + formatTimeZoneName, + getSupportedTimeZones, + getTimeZoneOffsetLabel, getTimeZoneShortName, isValidTimeZone, LOCAL_TIME_ZONE, @@ -21,6 +24,39 @@ describe('resolveTimeZonePreference', () => { }); }); +describe('getTimeZoneOffsetLabel', () => { + it('uses the offset in effect on the date', () => { + expect( + getTimeZoneOffsetLabel('America/Los_Angeles', new Date('2026-07-01Z')), + ).toBe('UTC-07:00'); + expect( + getTimeZoneOffsetLabel('America/Los_Angeles', new Date('2026-12-01Z')), + ).toBe('UTC-08:00'); + expect(getTimeZoneOffsetLabel('Asia/Kolkata')).toBe('UTC+05:30'); + }); + + it('labels UTC with a zero offset', () => { + expect(getTimeZoneOffsetLabel('Etc/UTC')).toBe('UTC+00:00'); + }); +}); + +describe('getSupportedTimeZones', () => { + it('lists named zones without UTC', () => { + const zones = getSupportedTimeZones(); + expect(zones).toContain('Asia/Tokyo'); + expect(zones).not.toContain('UTC'); + expect(zones).not.toContain('Etc/UTC'); + }); +}); + +describe('formatTimeZoneName', () => { + it('replaces underscores with spaces', () => { + expect(formatTimeZoneName('America/Los_Angeles')).toBe( + 'America/Los Angeles', + ); + }); +}); + describe('getTimeZoneShortName', () => { it('uses the abbreviation in effect on the date', () => { expect( @@ -31,8 +67,15 @@ describe('getTimeZoneShortName', () => { ).toBe('PST'); }); - it('names UTC and zones without an abbreviation', () => { + it('names UTC and labels zones without an abbreviation from UTC', () => { expect(getTimeZoneShortName('Etc/UTC')).toBe('UTC'); - expect(getTimeZoneShortName('Asia/Tokyo')).toBe('GMT+9'); + expect(getTimeZoneShortName('Asia/Tokyo')).toBe('UTC+9'); + expect(getTimeZoneShortName('Asia/Kolkata')).toBe('UTC+5:30'); + expect(getTimeZoneShortName('Europe/London', new Date('2026-01-15Z'))).toBe( + 'UTC', + ); + expect(getTimeZoneShortName('Europe/London', new Date('2026-07-15Z'))).toBe( + 'UTC+1', + ); }); }); diff --git a/packages/app/src/components/TimePicker/utils.ts b/packages/app/src/components/TimePicker/utils.ts index b64a14e5f1..b6f207b2c0 100644 --- a/packages/app/src/components/TimePicker/utils.ts +++ b/packages/app/src/components/TimePicker/utils.ts @@ -6,13 +6,15 @@ import { toWallClock, } from '@hyperdx/common-utils/dist/core/utils'; +import { getLocalTimeZone } from '@/timeZone'; + function currentDate(): Date { // eslint-disable-next-line no-restricted-syntax return new Date(); } function resolveTimeZone(timeZone?: string): string { - return timeZone ?? Intl.DateTimeFormat().resolvedOptions().timeZone; + return timeZone ?? getLocalTimeZone(); } // chrono reads text in one fixed offset. Using the zone's current offset makes diff --git a/packages/app/src/components/TimeZoneSelect.tsx b/packages/app/src/components/TimeZoneSelect.tsx new file mode 100644 index 0000000000..4de300eb1c --- /dev/null +++ b/packages/app/src/components/TimeZoneSelect.tsx @@ -0,0 +1,124 @@ +import { useCallback, useMemo } from 'react'; +import { UTC_TIME_ZONE } from '@hyperdx/common-utils/dist/core/utils'; +import { + Box, + ComboboxData, + ComboboxItem, + Group, + Select, + SelectProps, + Text, +} from '@mantine/core'; +import { IconCheck } from '@tabler/icons-react'; + +import { + formatTimeZoneName, + getLocalTimeZone, + getSupportedTimeZones, + getTimeZoneOffsetLabel, + getTimeZoneShortName, + LOCAL_TIME_ZONE, + resolveTimeZonePreference, +} from '@/timeZone'; + +type TimeZoneSelectProps = Omit< + SelectProps, + 'value' | 'onChange' | 'data' | 'renderOption' +> & { + /** A time zone setting: `LOCAL_TIME_ZONE` or an IANA name. */ + value: string; + onChange: (value: string) => void; +}; + +/** Offsets shown next to each option, keyed by option value. */ +function getOffsetLabels(zones: string[]): Map { + const localZone = getLocalTimeZone(); + + return new Map([ + [ + LOCAL_TIME_ZONE, + `${getTimeZoneShortName(localZone)} · ${getTimeZoneOffsetLabel(localZone)}`, + ], + [UTC_TIME_ZONE, getTimeZoneOffsetLabel(UTC_TIME_ZONE)], + ...zones.map(zone => [zone, getTimeZoneOffsetLabel(zone)] as const), + ]); +} + +/** + * Picks a time zone setting. Local and UTC are pinned above every zone the + * browser knows. + */ +export function TimeZoneSelect({ + value, + onChange, + ...props +}: TimeZoneSelectProps) { + const selected = resolveTimeZonePreference(value) ?? LOCAL_TIME_ZONE; + + const zones = useMemo(() => { + const supported = getSupportedTimeZones(); + + const isPinned = selected === LOCAL_TIME_ZONE || selected === UTC_TIME_ZONE; + + const isListed = supported.includes(selected); + + if (isPinned || isListed) { + return supported; + } + + return [...supported, selected]; + }, [selected]); + + const data = useMemo( + () => [ + { + value: LOCAL_TIME_ZONE, + label: `Local (${formatTimeZoneName(getLocalTimeZone())})`, + }, + { value: UTC_TIME_ZONE, label: 'UTC' }, + { + group: 'All time zones', + items: zones.map(zone => ({ + value: zone, + label: formatTimeZoneName(zone), + })), + }, + ], + [zones], + ); + + const offsetLabels = useMemo(() => getOffsetLabels(zones), [zones]); + + const renderOption = useCallback( + ({ option, checked }: { option: ComboboxItem; checked?: boolean }) => ( + + + {option.label} + + + {offsetLabels.get(option.value)} + + + + {checked && ( + + )} + + ), + [offsetLabels], + ); + + return ( +