umami/hooks/useDateRange.js

28 lines
922 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
2023-05-18 08:20:06 +02:00
export 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);
2023-05-20 18:02:08 +02:00
const saveDateRange = value => {
if (websiteId) {
setWebsiteDateRange(websiteId, value);
} else {
setItem(DATE_RANGE_CONFIG, value);
setDateRange(value);
}
2023-05-20 18:02:08 +02:00
};
return [dateRange, saveDateRange];
2020-09-01 00:02:32 +02:00
}
2023-05-18 08:20:06 +02:00
export default useDateRange;