umami/hooks/useDateRange.js

26 lines
898 B
JavaScript
Raw Normal View History

import { parseDateRange } from 'lib/date';
import { setItem } from 'next-basics';
import { DATE_RANGE_CONFIG, DEFAULT_DATE_RANGE } from 'lib/constants';
import useLocale from './useLocale';
import websiteStore, { setWebsiteDateRange } from 'store/websites';
import appStore, { setDateRange } from 'store/app';
2020-09-01 00:02:32 +02:00
2022-03-02 07:02:31 +01:00
export default function useDateRange(websiteId) {
const { locale } = useLocale();
const websiteConfig = websiteStore(state => state[websiteId]?.dateRange);
const defaultConfig = DEFAULT_DATE_RANGE;
const globalConfig = appStore(state => state.dateRange);
const dateRange = parseDateRange(websiteConfig || globalConfig || defaultConfig, locale);
function saveDateRange(value) {
if (websiteId) {
setWebsiteDateRange(websiteId, value);
} else {
setItem(DATE_RANGE_CONFIG, value);
setDateRange(value);
}
}
return [dateRange, saveDateRange];
2020-09-01 00:02:32 +02:00
}