umami/hooks/useDateRange.js

25 lines
756 B
JavaScript
Raw Normal View History

2020-09-01 00:02:32 +02:00
import { useSelector } from 'react-redux';
import { parseISO } from 'date-fns';
2020-09-01 00:02:32 +02:00
import { getDateRange } from 'lib/date';
import { getItem } from 'lib/web';
2020-09-01 00:02:32 +02:00
export default function useDateRange(websiteId, defaultDateRange = '24hour') {
const globalDefault = getItem('umami.date-range');
let globalDateRange;
if (typeof globalDefault === 'string') {
globalDateRange = getDateRange(globalDefault);
2020-09-19 17:48:45 +02:00
} else if (typeof globalDefault === 'object') {
globalDateRange = {
...globalDefault,
startDate: parseISO(globalDefault.startDate),
endDate: parseISO(globalDefault.endDate),
};
}
2020-09-01 00:02:32 +02:00
return useSelector(
state =>
state.websites[websiteId]?.dateRange || globalDateRange || getDateRange(defaultDateRange),
2020-09-01 00:02:32 +02:00
);
}