import { useEffect, useReducer, type ReactNode } from "react"; import { IntegrationsContext } from "../IntegrationsContext"; import { useCameraBlackboard } from "../../hooks/useCameraBlackboard"; import { initialState, reducer } from "../reducers/IntegrationsContextReducer"; type IntegrationsProviderType = { children: ReactNode; }; export const IntegrationsProvider = ({ children }: IntegrationsProviderType) => { const [state, dispatch] = useReducer(reducer, initialState); const { mutation } = useCameraBlackboard(); useEffect(() => { let isMounted = true; const fetchData = async () => { try { const result = await mutation.mutateAsync({ operation: "VIEW", path: "sessionStats", }); if (!isMounted) return; const catResult = await mutation.mutateAsync({ operation: "VIEW", path: "CategoryPopup", }); if (!isMounted) return; if (!result?.result || typeof result.result === "string") return; dispatch({ type: "UPDATE", payload: result.result }); dispatch({ type: "NPEDCATENABLED", payload: catResult.result }); } catch (error) { console.error("Error in fetchData:", error); } }; fetchData(); return () => { isMounted = false; }; }, []); return ( {children} ); };