Skip to content

Commit 58070fe

Browse files
committed
fix: serialize feedback date ranges in state
Nuxt useState is serialized through the payload, so the state value should stay JSON-safe. Store ISO strings there and expose Date objects through a computed wrapper so consumers keep the same API without relying on Date instances surviving hydration. The default range also uses the same startOfDay/endOfDay normalization as setDateRange. That keeps the first "Last 30 days" window aligned with picker and preset updates instead of using raw timestamps. Docs: https://nuxt.com/docs/4.x/getting-started/state-management Docs: https://nuxt.com/docs/api/composables/use-state
1 parent 060762a commit 58070fe

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

app/composables/useDateRange.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@ export interface DateRange {
55
end: Date
66
}
77

8+
interface SerializedDateRange {
9+
start: string
10+
end: string
11+
}
12+
813
export function useDateRange() {
9-
const dateRange = useState<DateRange>('feedback-date-range', () => ({
10-
start: subDays(new Date(), 30),
11-
end: new Date()
14+
const serializedDateRange = useState<SerializedDateRange>('feedback-date-range', () => {
15+
const end = endOfDay(new Date())
16+
return {
17+
start: startOfDay(subDays(end, 30)).toISOString(),
18+
end: end.toISOString()
19+
}
20+
})
21+
22+
const dateRange = computed<DateRange>(() => ({
23+
start: new Date(serializedDateRange.value.start),
24+
end: new Date(serializedDateRange.value.end)
1225
}))
1326

1427
const setDateRange = (range: DateRange) => {
15-
dateRange.value = {
16-
start: startOfDay(range.start),
17-
end: endOfDay(range.end)
28+
serializedDateRange.value = {
29+
start: startOfDay(range.start).toISOString(),
30+
end: endOfDay(range.end).toISOString()
1831
}
1932
}
2033

0 commit comments

Comments
 (0)