import { useReducer, useEffect, useRef, useCallback } from "react"; import type { HighlightsData } from "../shared/types"; import { getHighlights } from "../services/reportService"; import { useReportsPeriod } from "./useReportsPeriod"; interface State { data: HighlightsData | null; windowDays: 30 | 60 | 90; isLoading: boolean; error: string | null; } type Action = | { type: "SET_LOADING"; payload: boolean } | { type: "SET_DATA"; payload: HighlightsData } | { type: "SET_ERROR"; payload: string } | { type: "SET_WINDOW_DAYS"; payload: 30 | 60 | 90 }; const initialState: State = { data: null, windowDays: 30, isLoading: false, error: null, }; function reducer(state: State, action: Action): State { switch (action.type) { case "SET_LOADING": return { ...state, isLoading: action.payload }; case "SET_DATA": return { ...state, data: action.payload, isLoading: false, error: null }; case "SET_ERROR": return { ...state, error: action.payload, isLoading: false }; case "SET_WINDOW_DAYS": return { ...state, windowDays: action.payload }; default: return state; } } export function useHighlights() { const { from, to } = useReportsPeriod(); const [state, dispatch] = useReducer(reducer, initialState); const fetchIdRef = useRef(0); const fetch = useCallback(async (windowDays: 30 | 60 | 90, referenceDate: string) => { const id = ++fetchIdRef.current; dispatch({ type: "SET_LOADING", payload: true }); try { const data = await getHighlights(windowDays, referenceDate); if (id !== fetchIdRef.current) return; dispatch({ type: "SET_DATA", payload: data }); } catch (e) { if (id !== fetchIdRef.current) return; dispatch({ type: "SET_ERROR", payload: e instanceof Error ? e.message : String(e) }); } }, []); useEffect(() => { fetch(state.windowDays, to); }, [fetch, state.windowDays, to]); const setWindowDays = useCallback((d: 30 | 60 | 90) => { dispatch({ type: "SET_WINDOW_DAYS", payload: d }); }, []); return { ...state, setWindowDays, from, to }; }