2025-12-08 10:59:46 +00:00
|
|
|
import { useEffect, useReducer, type ReactNode } from "react";
|
2025-11-27 10:43:56 +00:00
|
|
|
import { CameraFeedContext } from "../context/CameraFeedContext";
|
|
|
|
|
import { initialState, reducer } from "../reducers/cameraFeedReducer";
|
2025-12-08 10:59:46 +00:00
|
|
|
import { useBlackBoard } from "../../hooks/useBlackBoard";
|
|
|
|
|
import type { CameraFeedState } from "../../types/types";
|
2025-11-27 10:43:56 +00:00
|
|
|
|
|
|
|
|
export const CameraFeedProvider = ({ children }: { children: ReactNode }) => {
|
2025-12-08 10:59:46 +00:00
|
|
|
const { blackboardMutation } = useBlackBoard();
|
2025-11-27 10:43:56 +00:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
|
|
2025-12-08 10:59:46 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchBlackBoardData = async () => {
|
|
|
|
|
const result = await blackboardMutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "cameraFeed",
|
|
|
|
|
});
|
|
|
|
|
if (!result?.result || typeof result.result === "string") return;
|
|
|
|
|
const cameraFeedData: CameraFeedState = result.result;
|
|
|
|
|
const recontructedState = {
|
|
|
|
|
...cameraFeedData,
|
|
|
|
|
paintedCells: {
|
|
|
|
|
A: new Map(cameraFeedData.paintedCells.A),
|
|
|
|
|
B: new Map(cameraFeedData.paintedCells.B),
|
|
|
|
|
C: new Map(cameraFeedData.paintedCells.C),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
dispatch({ type: "SET_CAMERA_FEED_DATA", cameraState: recontructedState });
|
|
|
|
|
};
|
|
|
|
|
fetchBlackBoardData();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-11-27 10:43:56 +00:00
|
|
|
return <CameraFeedContext.Provider value={{ state, dispatch }}>{children}</CameraFeedContext.Provider>;
|
|
|
|
|
};
|