feature(dashboard): remember last selected range

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-10-23 21:47:59 +02:00
parent 2329982cd1
commit 8c4d157633
3 changed files with 43 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ import {
useQueryState,
} from 'nuqs';
import { getStorageItem, setStorageItem } from '@/utils/storage';
import {
getDefaultIntervalByDates,
getDefaultIntervalByRange,
@@ -13,6 +14,7 @@ import {
} from '@openpanel/constants';
import type { IChartRange } from '@openpanel/validation';
import { mapKeys } from '@openpanel/validation';
import { useEffect } from 'react';
const nuqsOptions = { history: 'push' } as const;
@@ -29,9 +31,19 @@ export function useOverviewOptions() {
'range',
parseAsStringEnum(mapKeys(timeWindows))
.withDefault('7d')
.withOptions(nuqsOptions),
.withOptions({
...nuqsOptions,
clearOnDefault: false,
}),
);
useEffect(() => {
const range = getStorageItem('range', '7d');
if (range !== '7d') {
setRange(range);
}
}, []);
const interval =
getDefaultIntervalByDates(startDate, endDate) ||
getDefaultIntervalByRange(range);
@@ -56,6 +68,7 @@ export function useOverviewOptions() {
if (value !== 'custom') {
setStartDate(null);
setEndDate(null);
setStorageItem('range', value);
}
setRange(value);
},