import { useReducer, useCallback } from "react"; import { useReportsPeriod } from "./useReportsPeriod"; export type CompareMode = "mom" | "yoy" | "budget"; interface State { mode: CompareMode; isLoading: boolean; error: string | null; } type Action = | { type: "SET_MODE"; payload: CompareMode } | { type: "SET_LOADING"; payload: boolean } | { type: "SET_ERROR"; payload: string }; const initialState: State = { mode: "mom", isLoading: false, error: null, }; function reducer(state: State, action: Action): State { switch (action.type) { case "SET_MODE": return { ...state, mode: action.payload }; case "SET_LOADING": return { ...state, isLoading: action.payload }; case "SET_ERROR": return { ...state, error: action.payload, isLoading: false }; default: return state; } } export function useCompare() { const { from, to } = useReportsPeriod(); const [state, dispatch] = useReducer(reducer, initialState); const setMode = useCallback((m: CompareMode) => { dispatch({ type: "SET_MODE", payload: m }); }, []); // Issue #73 will fetch via reportService.getCompareMonthOverMonth / ...YearOverYear return { ...state, setMode, from, to }; }