diff --git a/.changeset/date-picker-keep-year.md b/.changeset/date-picker-keep-year.md new file mode 100644 index 0000000000..5fdd33b521 --- /dev/null +++ b/.changeset/date-picker-keep-year.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +--- + +fix: the time picker no longer resets a selected date range to the current +year. Range text now always includes the year, so it survives being re-parsed. diff --git a/packages/app/src/__tests__/timeQuery.test.ts b/packages/app/src/__tests__/timeQuery.test.ts index e82936735f..21fbeda52a 100644 --- a/packages/app/src/__tests__/timeQuery.test.ts +++ b/packages/app/src/__tests__/timeQuery.test.ts @@ -1,6 +1,57 @@ import { renderHook } from '@testing-library/react'; -import { useDefaultTimeRange } from '@/timeQuery'; +import { parseTimeRangeInput } from '@/components/TimePicker/utils'; +import { dateRangeToString, useDefaultTimeRange } from '@/timeQuery'; + +describe('dateRangeToString', () => { + beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2026-06-15T12:00:00')); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it.each([ + [ + '2024-03-03T10:00:00', + '2024-03-05T10:00:00', + 'Mar 3 2024 10:00:00 - Mar 5 2024 10:00:00', + ], + [ + '2026-03-03T10:00:00', + '2026-03-05T10:00:00', + 'Mar 3 2026 10:00:00 - Mar 5 2026 10:00:00', + ], + ])('always includes the year: %s - %s', (start, end, expected) => { + expect(dateRangeToString([new Date(start), new Date(end)], false)).toBe( + expected, + ); + }); + + it('formats in UTC when isUTC is set', () => { + expect( + dateRangeToString( + [new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')], + true, + ), + ).toBe('Dec 31 2024 23:00:00 - Jan 1 2025 01:00:00'); + }); + + it.each([ + [new Date('2024-03-03T10:00:00'), new Date('2024-03-05T10:00:00')], + [new Date('2025-12-30T10:00:00'), new Date('2026-02-01T10:00:00')], + [new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')], + [new Date('2026-08-01T10:00:00'), new Date('2026-09-01T10:00:00')], + ])('round-trips %s - %s through parseTimeRangeInput', (start, end) => { + const [parsedStart, parsedEnd] = parseTimeRangeInput( + dateRangeToString([start, end], false), + ); + expect(parsedStart).toEqual(start); + expect(parsedEnd).toEqual(end); + }); +}); describe('useDefaultTimeRange', () => { beforeEach(() => { diff --git a/packages/app/src/components/TimePicker/TimePicker.tsx b/packages/app/src/components/TimePicker/TimePicker.tsx index fe10393425..83d1fd7cbb 100644 --- a/packages/app/src/components/TimePicker/TimePicker.tsx +++ b/packages/app/src/components/TimePicker/TimePicker.tsx @@ -1,8 +1,9 @@ import React, { memo, useMemo, useState } from 'react'; -import { add, Duration, format, sub } from 'date-fns'; +import { add, Duration, sub } from 'date-fns'; import { useAtom } from 'jotai'; import { atomWithStorage } from 'jotai/utils'; import { useHotkeys } from 'react-hotkeys-hook'; +import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; import { Button, Card, @@ -190,10 +191,8 @@ const TimePickerComponent = ({ if (!from || !to) { return; } - const formatStr = - timeFormat === '24h' ? 'MMM d HH:mm:ss' : 'MMM d h:mm:ss a'; const rangeStr = [from, to] - .map(d => d && format(d, formatStr)) + .map(d => formatDate(d, { format: 'withYear', clock: timeFormat })) .join(' - '); onChange(rangeStr); onSearch(rangeStr); diff --git a/packages/app/src/components/TimePicker/__tests__/utils.test.ts b/packages/app/src/components/TimePicker/__tests__/utils.test.ts index de760bb57a..66f752bf84 100644 --- a/packages/app/src/components/TimePicker/__tests__/utils.test.ts +++ b/packages/app/src/components/TimePicker/__tests__/utils.test.ts @@ -1,3 +1,5 @@ +import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; + import { dateParser, parseTimeRangeInput, @@ -159,6 +161,35 @@ describe('parseTimeRangeInput', () => { }); }); +// Mirrors how TimePicker.handleSearch builds the range text for each clock. +describe('parseTimeRangeInput with year-qualified picker text', () => { + beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2026-06-15T12:00:00')); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it.each(['12h', '24h'] as const)( + 'round-trips past, boundary and future ranges with a %s clock', + clock => { + const ranges: [Date, Date][] = [ + [new Date('2024-03-03T13:05:09'), new Date('2024-03-05T01:00:00')], + [new Date('2025-12-30T10:00:00'), new Date('2026-02-01T10:00:00')], + [new Date('2026-08-01T10:00:00'), new Date('2026-09-01T10:00:00')], + ]; + for (const [start, end] of ranges) { + const text = [start, end] + .map(d => formatDate(d, { format: 'withYear', clock })) + .join(' - '); + expect(parseTimeRangeInput(text)).toEqual([start, end]); + } + }, + ); +}); + describe('timeRangeInputToSeconds', () => { beforeEach(() => { jest.useFakeTimers(); diff --git a/packages/app/src/timeQuery.ts b/packages/app/src/timeQuery.ts index 3216bc298d..a53053f17e 100644 --- a/packages/app/src/timeQuery.ts +++ b/packages/app/src/timeQuery.ts @@ -32,14 +32,16 @@ import { usePrevious } from './utils'; const LIVE_TAIL_TIME_QUERY = 'Live Tail'; const LIVE_TAIL_REFRESH_INTERVAL_MS = 1000; +// The text is re-parsed into dates (picker input, `tq` URL param). Without a +// year, chrono infers one and drops anything older than the current year. export const dateRangeToString = (range: [Date, Date], isUTC: boolean) => { return `${formatDate(range[0], { isUTC, - format: 'normal', + format: 'withYear', clock: '24h', })} - ${formatDate(range[1], { isUTC, - format: 'normal', + format: 'withYear', clock: '24h', })}`; };