import type { ColourData, PaintedCell, Region } from "../../../../types/types"; import ColourPicker from "./ColourPicker"; import { useCameraFeedContext } from "../../../../app/context/CameraFeedContext"; import { useColourDectection } from "../../hooks/useColourDetection"; import { useBlackBoard } from "../../../../hooks/useBlackBoard"; import { toast } from "sonner"; type RegionSelectorProps = { regions: Region[]; selectedRegionIndex: number; mode: string; cameraFeedID: "A" | "B" | "C"; isResetAllModalOpen: boolean; handleClose: () => void; setIsResetModalOpen: React.Dispatch>; }; const RegionSelector = ({ regions, selectedRegionIndex, mode, cameraFeedID, isResetAllModalOpen, setIsResetModalOpen, }: RegionSelectorProps) => { const { colourMutation } = useColourDectection(); const { state, dispatch } = useCameraFeedContext(); const { blackboardMutation } = useBlackBoard(); const paintedCells = state.paintedCells[cameraFeedID]; const handleChange = (e: { target: { value: string } }) => { dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID: cameraFeedID, mode: e.target.value } }); }; const handleResetRegion = () => { dispatch({ type: "RESET_PAINTED_CELLS", payload: { cameraFeedID: cameraFeedID, paintedCells: new Map() }, }); }; const handleModeChange = (newMode: string) => { dispatch({ type: "CHANGE_MODE", payload: { cameraFeedID: cameraFeedID, mode: newMode } }); }; const handleRegionSelect = (index: number) => { dispatch({ type: "SET_SELECTED_REGION_INDEX", payload: index }); }; const handleRegionColourChange = (index: number, newColour: string) => { const regionName = regions[index].name; dispatch({ type: "SET_SELECTED_REGION_COLOUR", payload: { cameraFeedID: cameraFeedID, regionName: regionName, newColour: newColour }, }); }; const handleAddRegionClick = () => { const regionName = `Bay ${regions.length + 1}`; dispatch({ type: "ADD_NEW_REGION", payload: { cameraFeedID: cameraFeedID, regionName: regionName, brushColour: "#ffffff" }, }); }; const handleRemoveClick = () => { dispatch({ type: "REMOVE_REGION", payload: { cameraFeedID: cameraFeedID, regionName: regions[selectedRegionIndex].name }, }); }; const openResetModal = () => { if (isResetAllModalOpen) return; setIsResetModalOpen(true); }; const handleSaveclick = () => { const regions: ColourData[] = []; const test = Array.from(paintedCells.entries()); const region1 = test.filter(([, cell]) => cell.region.name === "Bay 1"); const region2 = test.filter(([, cell]) => cell.region.name === "Bay 2"); const region3 = test.filter(([, cell]) => cell.region.name === "Bay 3"); const region4 = test.filter(([, cell]) => cell.region.name === "Bay 4"); const region5 = test.filter(([, cell]) => cell.region.name === "Bay 5"); const region1Data = { id: 1, cells: region1.map(([key]) => [parseInt(key.split("-")[1]), parseInt(key.split("-")[0])]), }; const region2Data = { id: 2, cells: region2.map(([key]) => [parseInt(key.split("-")[1]), parseInt(key.split("-")[0])]), }; const region3Data = { id: 3, cells: region3.map(([key]) => [parseInt(key.split("-")[1]), parseInt(key.split("-")[0])]), }; const region4Data = { id: 4, cells: region4.map(([key]) => [parseInt(key.split("-")[1]), parseInt(key.split("-")[0])]), }; const region5Data = { id: 5, cells: region5.map(([key]) => [parseInt(key.split("-")[1]), parseInt(key.split("-")[0])]), }; if (region1Data.cells.length > 0) { regions.push(region1Data); } if (region2Data.cells.length > 0) { regions.push(region2Data); } if (region3Data.cells.length > 0) { regions.push(region3Data); } if (region4Data.cells.length > 0) { regions.push(region4Data); } if (region5Data.cells.length > 0) { regions.push(region5Data); } colourMutation.mutate({ cameraFeedID, regions: regions }); // Convert Map to plain object for blackboard const serializableState = { ...state, paintedCells: { A: Array.from(state.paintedCells.A.entries()), B: Array.from(state.paintedCells.B.entries()), C: Array.from(state.paintedCells.C.entries()), }, }; blackboardMutation.mutate({ operation: "INSERT", path: `cameraFeed`, value: serializableState }); toast.success("Region data saved successfully!"); }; return (

Tools

Bay Select

<> {regions?.map((region, idx) => { const isSelected = selectedRegionIndex === idx; const inputId = `region-${idx}`; return ( ); })}

Actions

); }; export default RegionSelector;