- New useReportsPeriod hook reads/writes period via ?from=&to=&period= URL params, default civil year, pure resolver exported for tests - New per-domain hooks: useHighlights, useTrends, useCompare, useCategoryZoom (stubs wired to useReportsPeriod, to be fleshed out in #71-#74) - Rewire legacy useReports to consume useReportsPeriod; keep backward-compat state shape (period/customDateFrom/customDateTo) so /reports tabs keep working - Mark useReports @deprecated pending removal in #76 - Tests: 7 new cases covering resolveReportsPeriod defaults, bookmarks, invalid inputs, preset resolution Fixes #70 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
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 };
|
|
}
|