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
6 changes: 6 additions & 0 deletions .changeset/date-picker-keep-year.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 52 additions & 1 deletion packages/app/src/__tests__/timeQuery.test.ts
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
7 changes: 3 additions & 4 deletions packages/app/src/components/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 minor — Range-to-text formatting is still written out in two places, which is why this bug needed two fixes

handleSearch builds formatDate(d, {format:'withYear', clock}).join(' - ') by hand, and dateRangeToString in packages/app/src/timeQuery.ts:37 builds the same string. Add an optional clock parameter to dateRangeToString and call it here, so the range format has one source of truth and the next format change can't miss one copy.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

.map(d => d && format(d, formatStr))
.map(d => formatDate(d, { format: 'withYear', clock: timeFormat }))
.join(' - ');
onChange(rangeStr);
onSearch(rangeStr);
Expand Down
31 changes: 31 additions & 0 deletions packages/app/src/components/TimePicker/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatDate } from '@hyperdx/common-utils/dist/core/utils';

import {
dateParser,
parseTimeRangeInput,
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 4 additions & 2 deletions packages/app/src/timeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})}`;
};
Expand Down
Loading