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
16 changes: 11 additions & 5 deletions app/components/form/fields/DateTimeRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import { getLocalTimeZone, now as getNow, type DateValue } from '@internationalized/date'
import { useMemo, useState } from 'react'
import { useDebounce } from 'use-debounce'

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

const tz = getLocalTimeZone()

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

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

const onRangeChange = (newPreset: RangeKeyAll) => {
if (newPreset !== 'custom') {
const now = getNow(getLocalTimeZone())
const now = getNow(tz)
const newStartTime = computeStart[newPreset](now)
setRange({ start: newStartTime, end: now })
}
Expand All @@ -81,10 +84,13 @@ export function useDateTimeRangePicker({
items,
}

const [startTime] = useDebounce(range.start.toDate(tz), 400)
const [endTime] = useDebounce(range.end.toDate(tz), 400)

return {
startTime: range.start.toDate(getLocalTimeZone()),
endTime: range.end.toDate(getLocalTimeZone()),
preset: preset,
startTime,
endTime,
preset,
onRangeChange: onRangeChange,
dateTimeRangePicker: <DateTimeRangePicker {...props} />,
}
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"ts-pattern": "^5.6.2",
"tslib": "^2.7.0",
"tunnel-rat": "^0.1.2",
"use-debounce": "^10.0.4",
"uuid": "^10.0.0",
"zod": "^3.23.8",
"zustand": "^5.0.3"
Expand Down
Loading