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