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-12-17 14:19:23 +00:00
|
|
|
|
|
|
|
|
import { CAMERA_IDS } from "../config/cameraConfig";
|
|
|
|
|
import CameraZoomFetcher from "./CameraZoomFetcher";
|
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-12-09 08:47:21 +00:00
|
|
|
|
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,
|
2025-12-17 14:19:23 +00:00
|
|
|
paintedCells: CAMERA_IDS.reduce(
|
|
|
|
|
(acc, id) => {
|
|
|
|
|
acc[id] = new Map(cameraFeedData.paintedCells[id]);
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
{} as typeof cameraFeedData.paintedCells,
|
|
|
|
|
),
|
2025-12-08 10:59:46 +00:00
|
|
|
};
|
|
|
|
|
dispatch({ type: "SET_CAMERA_FEED_DATA", cameraState: recontructedState });
|
|
|
|
|
};
|
|
|
|
|
fetchBlackBoardData();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-12-17 14:19:23 +00:00
|
|
|
return (
|
|
|
|
|
<CameraFeedContext.Provider value={{ state, dispatch }}>
|
|
|
|
|
{CAMERA_IDS.map((cameraId) => (
|
|
|
|
|
<CameraZoomFetcher key={cameraId} cameraId={cameraId} dispatch={dispatch} />
|
|
|
|
|
))}
|
|
|
|
|
{children}
|
|
|
|
|
</CameraFeedContext.Provider>
|
|
|
|
|
);
|
2025-11-27 10:43:56 +00:00
|
|
|
};
|