From 0b3ce4a317952ade96d0f38ecfe8552cf1eb45e3 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Mon, 12 Oct 2026 12:11:51 +0200 Subject: [PATCH 1/2] fix(app): keep selected year when applying a date range in the time picker --- .changeset/date-picker-keep-year.md | 7 +++ packages/app/src/__tests__/timeQuery.test.ts | 44 ++++++++++++++++- .../src/components/TimePicker/TimePicker.tsx | 6 ++- .../TimePicker/__tests__/utils.test.ts | 49 +++++++++++++++++++ .../app/src/components/TimePicker/utils.ts | 14 ++++++ packages/app/src/timeQuery.ts | 11 +++-- 6 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 .changeset/date-picker-keep-year.md diff --git a/.changeset/date-picker-keep-year.md b/.changeset/date-picker-keep-year.md new file mode 100644 index 0000000000..f29836bc9a --- /dev/null +++ b/.changeset/date-picker-keep-year.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': patch +--- + +fix: the time picker no longer resets a selected date range to the current +year. The range text now includes the year when the range isn't in the current +year (or reaches into the future), 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..93c8cd1a54 100644 --- a/packages/app/src/__tests__/timeQuery.test.ts +++ b/packages/app/src/__tests__/timeQuery.test.ts @@ -1,6 +1,48 @@ 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('omits the year for a past range in the current year', () => { + expect( + dateRangeToString( + [new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')], + false, + ), + ).toBe('Mar 3 10:00:00 - Mar 5 10:00:00'); + }); + + it('includes the year for ranges outside the current year', () => { + expect( + dateRangeToString( + [new Date('2024-03-03T10:00:00'), new Date('2024-03-05T10:00:00')], + false, + ), + ).toBe('Mar 3 2024 10:00:00 - Mar 5 2024 10: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')], + ])('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..7d879f6b6d 100644 --- a/packages/app/src/components/TimePicker/TimePicker.tsx +++ b/packages/app/src/components/TimePicker/TimePicker.tsx @@ -36,6 +36,7 @@ import { LIVE_TAIL_DURATION_MS, LIVE_TAIL_TIME_QUERY, parseTimeRangeInput, + rangeNeedsExplicitYear, RELATIVE_TIME_OPTIONS, } from './utils'; @@ -190,8 +191,11 @@ const TimePickerComponent = ({ if (!from || !to) { return; } + const datePart = rangeNeedsExplicitYear([from, to]) + ? 'MMM d yyyy' + : 'MMM d'; const formatStr = - timeFormat === '24h' ? 'MMM d HH:mm:ss' : 'MMM d h:mm:ss a'; + timeFormat === '24h' ? `${datePart} HH:mm:ss` : `${datePart} h:mm:ss a`; const rangeStr = [from, to] .map(d => d && format(d, formatStr)) .join(' - '); diff --git a/packages/app/src/components/TimePicker/__tests__/utils.test.ts b/packages/app/src/components/TimePicker/__tests__/utils.test.ts index de760bb57a..cfb8954c59 100644 --- a/packages/app/src/components/TimePicker/__tests__/utils.test.ts +++ b/packages/app/src/components/TimePicker/__tests__/utils.test.ts @@ -1,6 +1,7 @@ import { dateParser, parseTimeRangeInput, + rangeNeedsExplicitYear, timeRangeInputToSeconds, } from '@/components/TimePicker/utils'; @@ -159,6 +160,54 @@ describe('parseTimeRangeInput', () => { }); }); +describe('rangeNeedsExplicitYear', () => { + beforeEach(() => { + jest.useFakeTimers(); + jest.setSystemTime(new Date('2025-06-15T12:00:00')); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + + it('is false for a past range in the current year', () => { + expect( + rangeNeedsExplicitYear([ + new Date('2025-03-01T10:00:00'), + new Date('2025-03-02T10:00:00'), + ]), + ).toBe(false); + }); + + it('is true when either date is in another year', () => { + expect( + rangeNeedsExplicitYear([ + new Date('2024-03-01T10:00:00'), + new Date('2025-03-02T10:00:00'), + ]), + ).toBe(true); + }); + + it('is true when a date in the current year is in the future', () => { + expect( + rangeNeedsExplicitYear([ + new Date('2025-06-01T10:00:00'), + new Date('2025-12-25T10:00:00'), + ]), + ).toBe(true); + }); + + it('compares years in UTC when isUTC is set', () => { + jest.setSystemTime(new Date('2025-06-15T12:00:00Z')); + expect( + rangeNeedsExplicitYear( + [new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')], + true, + ), + ).toBe(true); + }); +}); + describe('timeRangeInputToSeconds', () => { beforeEach(() => { jest.useFakeTimers(); diff --git a/packages/app/src/components/TimePicker/utils.ts b/packages/app/src/components/TimePicker/utils.ts index ab21fb5117..b386416f84 100644 --- a/packages/app/src/components/TimePicker/utils.ts +++ b/packages/app/src/components/TimePicker/utils.ts @@ -72,6 +72,20 @@ export function parseTimeRangeInput( } } +// A range string without years is re-parsed with an inferred year (the current +// one, or the previous one for dates >1 day ahead). Only dates in the current +// year that aren't in the future survive that round trip, so every other range +// needs an explicit year in its text. +export function rangeNeedsExplicitYear( + range: readonly [Date, Date], + isUTC: boolean = false, +): boolean { + // eslint-disable-next-line no-restricted-syntax + const now = new Date(); + const getYear = (d: Date) => (isUTC ? d.getUTCFullYear() : d.getFullYear()); + return range.some(d => getYear(d) !== getYear(now) || d > now); +} + // Duration of a time range input in seconds, or null if it doesn't parse. export function timeRangeInputToSeconds( str: string, diff --git a/packages/app/src/timeQuery.ts b/packages/app/src/timeQuery.ts index 3216bc298d..29057b3798 100644 --- a/packages/app/src/timeQuery.ts +++ b/packages/app/src/timeQuery.ts @@ -25,7 +25,10 @@ import { import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; import { DateRange } from '@hyperdx/common-utils/dist/types'; -import { parseTimeRangeInput } from './components/TimePicker/utils'; +import { + parseTimeRangeInput, + rangeNeedsExplicitYear, +} from './components/TimePicker/utils'; import { useUserPreferences } from './useUserPreferences'; import { usePrevious } from './utils'; @@ -33,13 +36,15 @@ const LIVE_TAIL_TIME_QUERY = 'Live Tail'; const LIVE_TAIL_REFRESH_INTERVAL_MS = 1000; export const dateRangeToString = (range: [Date, Date], isUTC: boolean) => { + const format = rangeNeedsExplicitYear(range, isUTC) ? 'withYear' : 'normal'; + return `${formatDate(range[0], { isUTC, - format: 'normal', + format, clock: '24h', })} - ${formatDate(range[1], { isUTC, - format: 'normal', + format, clock: '24h', })}`; }; From 602ac3996d9ecbca6cd25ed6cb3ad02d8ad883f0 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Mon, 12 Oct 2026 12:25:37 +0200 Subject: [PATCH 2/2] fix(app): always include the year in time picker range text --- .changeset/date-picker-keep-year.md | 3 +- packages/app/src/__tests__/timeQuery.test.ts | 31 ++++++---- .../src/components/TimePicker/TimePicker.tsx | 11 +--- .../TimePicker/__tests__/utils.test.ts | 60 +++++++------------ .../app/src/components/TimePicker/utils.ts | 14 ----- packages/app/src/timeQuery.ts | 13 ++-- 6 files changed, 50 insertions(+), 82 deletions(-) diff --git a/.changeset/date-picker-keep-year.md b/.changeset/date-picker-keep-year.md index f29836bc9a..5fdd33b521 100644 --- a/.changeset/date-picker-keep-year.md +++ b/.changeset/date-picker-keep-year.md @@ -3,5 +3,4 @@ --- fix: the time picker no longer resets a selected date range to the current -year. The range text now includes the year when the range isn't in the current -year (or reaches into the future), so it survives being re-parsed. +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 93c8cd1a54..21fbeda52a 100644 --- a/packages/app/src/__tests__/timeQuery.test.ts +++ b/packages/app/src/__tests__/timeQuery.test.ts @@ -13,28 +13,37 @@ describe('dateRangeToString', () => { jest.useRealTimers(); }); - it('omits the year for a past range in the current year', () => { - expect( - dateRangeToString( - [new Date('2026-03-03T10:00:00'), new Date('2026-03-05T10:00:00')], - false, - ), - ).toBe('Mar 3 10:00:00 - Mar 5 10:00:00'); + 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('includes the year for ranges outside the current year', () => { + it('formats in UTC when isUTC is set', () => { expect( dateRangeToString( - [new Date('2024-03-03T10:00:00'), new Date('2024-03-05T10:00:00')], - false, + [new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')], + true, ), - ).toBe('Mar 3 2024 10:00:00 - Mar 5 2024 10:00:00'); + ).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), diff --git a/packages/app/src/components/TimePicker/TimePicker.tsx b/packages/app/src/components/TimePicker/TimePicker.tsx index 7d879f6b6d..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, @@ -36,7 +37,6 @@ import { LIVE_TAIL_DURATION_MS, LIVE_TAIL_TIME_QUERY, parseTimeRangeInput, - rangeNeedsExplicitYear, RELATIVE_TIME_OPTIONS, } from './utils'; @@ -191,13 +191,8 @@ const TimePickerComponent = ({ if (!from || !to) { return; } - const datePart = rangeNeedsExplicitYear([from, to]) - ? 'MMM d yyyy' - : 'MMM d'; - const formatStr = - timeFormat === '24h' ? `${datePart} HH:mm:ss` : `${datePart} 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 cfb8954c59..66f752bf84 100644 --- a/packages/app/src/components/TimePicker/__tests__/utils.test.ts +++ b/packages/app/src/components/TimePicker/__tests__/utils.test.ts @@ -1,7 +1,8 @@ +import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; + import { dateParser, parseTimeRangeInput, - rangeNeedsExplicitYear, timeRangeInputToSeconds, } from '@/components/TimePicker/utils'; @@ -160,52 +161,33 @@ describe('parseTimeRangeInput', () => { }); }); -describe('rangeNeedsExplicitYear', () => { +// 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('2025-06-15T12:00:00')); + jest.setSystemTime(new Date('2026-06-15T12:00:00')); }); afterEach(() => { jest.useRealTimers(); }); - it('is false for a past range in the current year', () => { - expect( - rangeNeedsExplicitYear([ - new Date('2025-03-01T10:00:00'), - new Date('2025-03-02T10:00:00'), - ]), - ).toBe(false); - }); - - it('is true when either date is in another year', () => { - expect( - rangeNeedsExplicitYear([ - new Date('2024-03-01T10:00:00'), - new Date('2025-03-02T10:00:00'), - ]), - ).toBe(true); - }); - - it('is true when a date in the current year is in the future', () => { - expect( - rangeNeedsExplicitYear([ - new Date('2025-06-01T10:00:00'), - new Date('2025-12-25T10:00:00'), - ]), - ).toBe(true); - }); - - it('compares years in UTC when isUTC is set', () => { - jest.setSystemTime(new Date('2025-06-15T12:00:00Z')); - expect( - rangeNeedsExplicitYear( - [new Date('2024-12-31T23:00:00Z'), new Date('2025-01-01T01:00:00Z')], - true, - ), - ).toBe(true); - }); + 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', () => { diff --git a/packages/app/src/components/TimePicker/utils.ts b/packages/app/src/components/TimePicker/utils.ts index b386416f84..ab21fb5117 100644 --- a/packages/app/src/components/TimePicker/utils.ts +++ b/packages/app/src/components/TimePicker/utils.ts @@ -72,20 +72,6 @@ export function parseTimeRangeInput( } } -// A range string without years is re-parsed with an inferred year (the current -// one, or the previous one for dates >1 day ahead). Only dates in the current -// year that aren't in the future survive that round trip, so every other range -// needs an explicit year in its text. -export function rangeNeedsExplicitYear( - range: readonly [Date, Date], - isUTC: boolean = false, -): boolean { - // eslint-disable-next-line no-restricted-syntax - const now = new Date(); - const getYear = (d: Date) => (isUTC ? d.getUTCFullYear() : d.getFullYear()); - return range.some(d => getYear(d) !== getYear(now) || d > now); -} - // Duration of a time range input in seconds, or null if it doesn't parse. export function timeRangeInputToSeconds( str: string, diff --git a/packages/app/src/timeQuery.ts b/packages/app/src/timeQuery.ts index 29057b3798..a53053f17e 100644 --- a/packages/app/src/timeQuery.ts +++ b/packages/app/src/timeQuery.ts @@ -25,26 +25,23 @@ import { import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; import { DateRange } from '@hyperdx/common-utils/dist/types'; -import { - parseTimeRangeInput, - rangeNeedsExplicitYear, -} from './components/TimePicker/utils'; +import { parseTimeRangeInput } from './components/TimePicker/utils'; import { useUserPreferences } from './useUserPreferences'; 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) => { - const format = rangeNeedsExplicitYear(range, isUTC) ? 'withYear' : 'normal'; - return `${formatDate(range[0], { isUTC, - format, + format: 'withYear', clock: '24h', })} - ${formatDate(range[1], { isUTC, - format, + format: 'withYear', clock: '24h', })}`; };