From 47f5455ffa671fcb9eecd5a3b8a0592766e3af52 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Tue, 13 Oct 2026 10:26:39 +0200 Subject: [PATCH 1/2] fix(app): only show the year in time picker range text when needed --- .changeset/date-picker-keep-year.md | 3 +- packages/app/src/__tests__/timeQuery.test.ts | 31 +++--- .../src/components/TimePicker/TimePicker.tsx | 6 +- .../TimePicker/__tests__/utils.test.ts | 94 +++++++++++++++++-- .../app/src/components/TimePicker/utils.ts | 27 ++++++ packages/app/src/timeQuery.ts | 21 ++--- 6 files changed, 137 insertions(+), 45 deletions(-) diff --git a/.changeset/date-picker-keep-year.md b/.changeset/date-picker-keep-year.md index 5fdd33b521..f29836bc9a 100644 --- a/.changeset/date-picker-keep-year.md +++ b/.changeset/date-picker-keep-year.md @@ -3,4 +3,5 @@ --- 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. +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 21fbeda52a..b1788fc27b 100644 --- a/packages/app/src/__tests__/timeQuery.test.ts +++ b/packages/app/src/__tests__/timeQuery.test.ts @@ -13,21 +13,22 @@ describe('dateRangeToString', () => { 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('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('formats in UTC when isUTC is set', () => { diff --git a/packages/app/src/components/TimePicker/TimePicker.tsx b/packages/app/src/components/TimePicker/TimePicker.tsx index 83d1fd7cbb..486643dea5 100644 --- a/packages/app/src/components/TimePicker/TimePicker.tsx +++ b/packages/app/src/components/TimePicker/TimePicker.tsx @@ -3,7 +3,6 @@ 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, @@ -34,6 +33,7 @@ import { dateParser, DURATION_OPTIONS, DURATIONS, + formatTimeRangeText, LIVE_TAIL_DURATION_MS, LIVE_TAIL_TIME_QUERY, parseTimeRangeInput, @@ -191,9 +191,7 @@ const TimePickerComponent = ({ if (!from || !to) { return; } - const rangeStr = [from, to] - .map(d => formatDate(d, { format: 'withYear', clock: timeFormat })) - .join(' - '); + const rangeStr = formatTimeRangeText([from, to], { clock: timeFormat }); onChange(rangeStr); onSearch(rangeStr); close(); diff --git a/packages/app/src/components/TimePicker/__tests__/utils.test.ts b/packages/app/src/components/TimePicker/__tests__/utils.test.ts index 66f752bf84..4fc7a286f5 100644 --- a/packages/app/src/components/TimePicker/__tests__/utils.test.ts +++ b/packages/app/src/components/TimePicker/__tests__/utils.test.ts @@ -1,8 +1,8 @@ -import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; - import { dateParser, + formatTimeRangeText, parseTimeRangeInput, + rangeNeedsExplicitYear, timeRangeInputToSeconds, } from '@/components/TimePicker/utils'; @@ -161,8 +161,55 @@ describe('parseTimeRangeInput', () => { }); }); -// Mirrors how TimePicker.handleSearch builds the range text for each clock. -describe('parseTimeRangeInput with year-qualified picker text', () => { +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('formatTimeRangeText', () => { beforeEach(() => { jest.useFakeTimers(); jest.setSystemTime(new Date('2026-06-15T12:00:00')); @@ -172,19 +219,46 @@ describe('parseTimeRangeInput with year-qualified picker text', () => { jest.useRealTimers(); }); + it('omits the year for a past range in the current year', () => { + expect( + formatTimeRangeText([ + new Date('2026-03-03T10:00:00'), + new Date('2026-03-05T10:00:00'), + ]), + ).toBe('Mar 3 10:00:00 - Mar 5 10:00:00'); + }); + + it('includes the year when any date is outside the current year', () => { + expect( + formatTimeRangeText([ + new Date('2025-12-30T10:00:00'), + new Date('2026-02-01T10:00:00'), + ]), + ).toBe('Dec 30 2025 10:00:00 - Feb 1 2026 10:00:00'); + }); + + it('uses the requested clock', () => { + expect( + formatTimeRangeText( + [new Date('2024-03-03T13:05:09'), new Date('2024-03-05T01:00:00')], + { clock: '12h' }, + ), + ).toBe('Mar 3 2024 1:05:09 PM - Mar 5 2024 1:00:00 AM'); + }); + it.each(['12h', '24h'] as const)( - 'round-trips past, boundary and future ranges with a %s clock', + 'round-trips past, boundary, current-year 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-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')], ]; - for (const [start, end] of ranges) { - const text = [start, end] - .map(d => formatDate(d, { format: 'withYear', clock })) - .join(' - '); - expect(parseTimeRangeInput(text)).toEqual([start, end]); + for (const range of ranges) { + expect( + parseTimeRangeInput(formatTimeRangeText(range, { clock })), + ).toEqual(range); } }, ); diff --git a/packages/app/src/components/TimePicker/utils.ts b/packages/app/src/components/TimePicker/utils.ts index ab21fb5117..8fe25bb0d5 100644 --- a/packages/app/src/components/TimePicker/utils.ts +++ b/packages/app/src/components/TimePicker/utils.ts @@ -1,5 +1,6 @@ import * as chrono from 'chrono-node'; import ms from 'ms'; +import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; function normalizeParsedDate(parsed?: chrono.ParsedComponents): Date | null { if (!parsed) { @@ -72,6 +73,32 @@ export function parseTimeRangeInput( } } +// Range text without a year is re-parsed with an inferred year (the current +// one, or the previous one for dates >1 day ahead). Only past dates in the +// current year survive that, so every other range needs an explicit year. +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); +} + +// Formats a range as text that parseTimeRangeInput maps back to the same dates. +// The year is only included when it's needed to do so, to keep the text short. +export function formatTimeRangeText( + range: readonly [Date, Date], + { + isUTC = false, + clock = '24h', + }: { isUTC?: boolean; clock?: '12h' | '24h' } = {}, +): string { + const format = rangeNeedsExplicitYear(range, isUTC) ? 'withYear' : 'normal'; + return range.map(d => formatDate(d, { isUTC, format, clock })).join(' - '); +} + // 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 a53053f17e..8a33845e0d 100644 --- a/packages/app/src/timeQuery.ts +++ b/packages/app/src/timeQuery.ts @@ -22,29 +22,20 @@ import { useQueryState, useQueryStates, } from 'nuqs'; -import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; import { DateRange } from '@hyperdx/common-utils/dist/types'; -import { parseTimeRangeInput } from './components/TimePicker/utils'; +import { + formatTimeRangeText, + 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) => { - return `${formatDate(range[0], { - isUTC, - format: 'withYear', - clock: '24h', - })} - ${formatDate(range[1], { - isUTC, - format: 'withYear', - clock: '24h', - })}`; -}; +export const dateRangeToString = (range: [Date, Date], isUTC: boolean) => + formatTimeRangeText(range, { isUTC, clock: '24h' }); function isInputTimeQueryLive(inputTimeQuery: string) { return inputTimeQuery === '' || inputTimeQuery.includes(LIVE_TAIL_TIME_QUERY); From 7f564368a42f25b17e274ea051ac6d7ccaeead2c Mon Sep 17 00:00:00 2001 From: Karl Power Date: Tue, 13 Oct 2026 10:39:02 +0200 Subject: [PATCH 2/2] fix(app): reuse one clock helper to stay within the eslint-disable ratchet --- packages/app/src/components/TimePicker/utils.ts | 14 ++++++++------ scripts/ci/ratchet-baseline.json | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/app/src/components/TimePicker/utils.ts b/packages/app/src/components/TimePicker/utils.ts index 8fe25bb0d5..e5628ffec5 100644 --- a/packages/app/src/components/TimePicker/utils.ts +++ b/packages/app/src/components/TimePicker/utils.ts @@ -2,13 +2,17 @@ import * as chrono from 'chrono-node'; import ms from 'ms'; import { formatDate } from '@hyperdx/common-utils/dist/core/utils'; +function currentDate(): Date { + // eslint-disable-next-line no-restricted-syntax + return new Date(); +} + function normalizeParsedDate(parsed?: chrono.ParsedComponents): Date | null { if (!parsed) { return null; } - // eslint-disable-next-line no-restricted-syntax - const now = new Date(); + const now = currentDate(); const parsedDate = parsed.date(); // If all of the time components have been inferred, set the time components of now @@ -60,8 +64,7 @@ export function parseTimeRangeInput( ? parsedTimeResults[0] : parsedTimeResults[1]; const start = normalizeParsedDate(parsedTimeResult.start); - // eslint-disable-next-line no-restricted-syntax - const end = normalizeParsedDate(parsedTimeResult.end) || new Date(); + const end = normalizeParsedDate(parsedTimeResult.end) || currentDate(); if (end && start && end < start) { // For date range strings that omit years, the chrono parser will infer the year // using the current year. This can cause the start date to be in the future, and @@ -80,8 +83,7 @@ export function rangeNeedsExplicitYear( range: readonly [Date, Date], isUTC: boolean = false, ): boolean { - // eslint-disable-next-line no-restricted-syntax - const now = new Date(); + const now = currentDate(); const getYear = (d: Date) => (isUTC ? d.getUTCFullYear() : d.getFullYear()); return range.some(d => getYear(d) !== getYear(now) || d > now); } diff --git a/scripts/ci/ratchet-baseline.json b/scripts/ci/ratchet-baseline.json index 4d7e1f1fba..21457c9e1e 100644 --- a/scripts/ci/ratchet-baseline.json +++ b/scripts/ci/ratchet-baseline.json @@ -8,7 +8,7 @@ "app": { "as-any": 213, "ts-ignore": 0, - "eslint-disable": 143, + "eslint-disable": 142, "@source": 0 }, "cli": {