import { intervalToRange } from '@/utils/intervalToRange' import { ComponentChild, createContext } from 'preact' import { StateUpdater, useContext, useMemo, useState } from 'preact/hooks' import { FilterValue } from '../components/Filters' type DashboardContextType = { filter: FilterValue setFilter: StateUpdater } const DashboardContext = createContext(null) export const DashboardContextProvider = ({ children, }: { children: ComponentChild }) => { const [filter, setFilter] = useState(() => { const range = intervalToRange('week', new Date(), new Date()) return { interval: 'week', customFrom: range[0], customTo: range[1] } }) const value = useMemo(() => ({ filter, setFilter }), [filter]) return ( {children} ) } export const useDashboardContext = () => { const ctx = useContext(DashboardContext) if (!ctx) { throw new Error('useDashboardContext used outside of DashboardContext') } return ctx }