Skip to content

Commit 99a1f00

Browse files
authored
Debounce startTime and endTime in date range picker (#2801)
* debounce startTime and endTime in date range picker * the linter is wise
1 parent 1d37fcc commit 99a1f00

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

app/components/form/fields/DateTimeRangePicker.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { getLocalTimeZone, now as getNow, type DateValue } from '@internationalized/date'
99
import { useMemo, useState } from 'react'
10+
import { useDebounce } from 'use-debounce'
1011

1112
import { DateRangePicker } from '~/ui/lib/DateRangePicker'
1213
import { Listbox } from '~/ui/lib/Listbox'
@@ -33,6 +34,8 @@ const computeStart: Record<RangeKey, (now: DateValue) => DateValue> = {
3334
last30Days: (now) => now.subtract({ days: 30 }),
3435
}
3536

37+
const tz = getLocalTimeZone()
38+
3639
// Limitations:
3740
// - list of presets is hard-coded
3841
// - initial preset can't be "custom"
@@ -54,7 +57,7 @@ export function useDateTimeRangePicker({
5457
maxValue?: DateValue | undefined
5558
items?: { label: string; value: RangeKeyAll }[]
5659
}) {
57-
const now = useMemo(() => getNow(getLocalTimeZone()), [])
60+
const now = useMemo(() => getNow(tz), [])
5861

5962
const start = computeStart[initialPreset](now)
6063
const end = now
@@ -64,7 +67,7 @@ export function useDateTimeRangePicker({
6467

6568
const onRangeChange = (newPreset: RangeKeyAll) => {
6669
if (newPreset !== 'custom') {
67-
const now = getNow(getLocalTimeZone())
70+
const now = getNow(tz)
6871
const newStartTime = computeStart[newPreset](now)
6972
setRange({ start: newStartTime, end: now })
7073
}
@@ -81,10 +84,13 @@ export function useDateTimeRangePicker({
8184
items,
8285
}
8386

87+
const [startTime] = useDebounce(range.start.toDate(tz), 400)
88+
const [endTime] = useDebounce(range.end.toDate(tz), 400)
89+
8490
return {
85-
startTime: range.start.toDate(getLocalTimeZone()),
86-
endTime: range.end.toDate(getLocalTimeZone()),
87-
preset: preset,
91+
startTime,
92+
endTime,
93+
preset,
8894
onRangeChange: onRangeChange,
8995
dateTimeRangePicker: <DateTimeRangePicker {...props} />,
9096
}

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"ts-pattern": "^5.6.2",
7272
"tslib": "^2.7.0",
7373
"tunnel-rat": "^0.1.2",
74+
"use-debounce": "^10.0.4",
7475
"uuid": "^10.0.0",
7576
"zod": "^3.23.8",
7677
"zustand": "^5.0.3"

0 commit comments

Comments
 (0)