2022-02-23 07:47:59 +01:00
|
|
|
import create from 'zustand';
|
|
|
|
import produce from 'immer';
|
2022-03-02 06:13:15 +01:00
|
|
|
import app from './app';
|
2023-03-09 01:37:43 +01:00
|
|
|
import { parseDateRange } from 'lib/date';
|
2022-02-23 07:47:59 +01:00
|
|
|
|
|
|
|
const store = create(() => ({}));
|
|
|
|
|
2023-03-09 01:37:43 +01:00
|
|
|
export function getWebsiteDateRange(websiteId) {
|
|
|
|
return store.getState()?.[websiteId];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setWebsiteDateRange(websiteId, value) {
|
2022-02-23 07:47:59 +01:00
|
|
|
store.setState(
|
|
|
|
produce(state => {
|
|
|
|
if (!state[websiteId]) {
|
|
|
|
state[websiteId] = {};
|
|
|
|
}
|
|
|
|
|
2022-03-02 06:13:15 +01:00
|
|
|
let dateRange = value;
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
const { locale } = app.getState();
|
2023-03-09 01:37:43 +01:00
|
|
|
dateRange = parseDateRange(value, locale);
|
2022-03-02 06:13:15 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 07:47:59 +01:00
|
|
|
state[websiteId].dateRange = { ...dateRange, modified: Date.now() };
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default store;
|