Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/time-zone-select.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 12 additions & 18 deletions packages/app/src/UserPreferencesModal.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
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,
Group,
Modal,
Select,
Stack,
Switch,
Text,
Tooltip,
} from '@mantine/core';

import { TimeZoneSelect } from './components/TimeZoneSelect';
import {
isContentFontSize,
OPTIONS_CONTENT_FONT_SIZE,
OPTIONS_FONTS,
} 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,
Expand Down Expand Up @@ -60,7 +58,7 @@ const SettingContainer = ({
<div style={{ flex: 1 }}>
{label}
{!!description && (
<Text size="xs" mt={2}>
<Text size="xs" mt={2} c="var(--color-text-muted)">
{description}
</Text>
)}
Expand Down Expand Up @@ -112,18 +110,14 @@ export const UserPreferencesModal = ({
allowDeselect={false}
/>
</SettingContainer>
<SettingContainer label="Use UTC time">
<Switch
size="md"
onLabel="UTC"
checked={userPreferences.timeZone === UTC_TIME_ZONE}
onChange={e =>
setUserPreference({
timeZone: e.currentTarget.checked
? UTC_TIME_ZONE
: LOCAL_TIME_ZONE,
})
}
<SettingContainer
label="Time zone"
description="Times are displayed and input in this zone."
>
<TimeZoneSelect
data-testid="time-zone-select"
value={userPreferences.timeZone}
onChange={timeZone => setUserPreference({ timeZone })}
/>
</SettingContainer>

Expand Down Expand Up @@ -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 && (
Expand Down
19 changes: 19 additions & 0 deletions packages/app/src/__tests__/UserPreferencesModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
47 changes: 45 additions & 2 deletions packages/app/src/__tests__/timeZone.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {
formatTimeZoneName,
getSupportedTimeZones,
getTimeZoneOffsetLabel,
getTimeZoneShortName,
isValidTimeZone,
LOCAL_TIME_ZONE,
Expand All @@ -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(
Expand All @@ -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',
);
});
});
4 changes: 3 additions & 1 deletion packages/app/src/components/TimePicker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
124 changes: 124 additions & 0 deletions packages/app/src/components/TimeZoneSelect.tsx
Original file line number Diff line number Diff line change
@@ -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<string, string> {
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<ComboboxData>(
() => [
{
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 }) => (
<Group w="100%">
<Box flex={1}>
<Text fz="sm">{option.label}</Text>

<Text size="xs" c="dimmed">
{offsetLabels.get(option.value)}
</Text>
</Box>

{checked && (
<IconCheck size={14} color="var(--color-text-brand)" stroke={2.5} />
)}
</Group>
),
[offsetLabels],
);

return (
<Select
searchable
allowDeselect={false}
maxDropdownHeight={280}
nothingFoundMessage="No matching time zones"
{...props}
data={data}
value={selected}
onChange={next => next && onChange(next)}
renderOption={renderOption}
/>
);
}
66 changes: 66 additions & 0 deletions packages/app/src/components/__tests__/TimeZoneSelect.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { TimeZoneSelect } from '@/components/TimeZoneSelect';

// Tests run with TZ=America/New_York.
const renderSelect = (value: string, onChange = jest.fn()) => {
renderWithMantine(
<TimeZoneSelect
data-testid="time-zone-select"
value={value}
onChange={onChange}
/>,
);
return { onChange };
};

const optionNames = () =>
screen
.getAllByRole('option', { hidden: true })
.map(option => option.textContent);

describe('TimeZoneSelect', () => {
it('shows the local zone for the local setting', () => {
renderSelect('local');
expect(screen.getByTestId('time-zone-select')).toHaveValue(
'Local (America/New York)',
);
});

it('pins local and UTC above the other zones', async () => {
renderSelect('local');
await userEvent.click(screen.getByTestId('time-zone-select'));

const names = optionNames();
expect(names[0]).toMatch(
/^Local \(America\/New York\)E[SD]T · UTC-0[45]:00$/,
);
expect(names[1]).toBe('UTCUTC+00:00');
expect(names.slice(2)).toContain('Asia/TokyoUTC+09:00');
expect(names.filter(name => name?.startsWith('UTC'))).toHaveLength(1);
});

it('finds zones by name and reports the IANA value', async () => {
const { onChange } = renderSelect('local');

const input = screen.getByTestId('time-zone-select');
await userEvent.clear(input);
await userEvent.type(input, 'los ang');
await userEvent.click(
await screen.findByRole('option', {
name: /America\/Los Angeles/,
hidden: true,
}),
);

expect(onChange).toHaveBeenCalledWith('America/Los_Angeles');
});

it('shows local for a zone the browser does not recognise', () => {
renderSelect('Mars/Olympus_Mons');
expect(screen.getByTestId('time-zone-select')).toHaveValue(
'Local (America/New York)',
);
});
});
Loading
Loading