import { useContext, useEffect, useState } from "react"; import { CheckIcon, XIcon } from "lucide-react"; import { HistoryContext } from "@/context/History"; import { SelectionContext } from "@/context/Selection"; import { Button } from "@/components/ui/button"; import { CanvasContext } from "@/context/Canvas"; interface Props { startBlocks: Block[]; startSelectionCoords: CoordinateArray; } function SelectionBar({ startBlocks, startSelectionCoords }: Props) { const { blocks, setBlocks } = useContext(CanvasContext); const { addHistory } = useContext(HistoryContext); const { selectionCoords, selectionLayerBlocks, setSelectionCoords, setSelectionLayerBlocks } = useContext(SelectionContext); const [isVisible, setIsVisible] = useState(false); const confirm = () => { const oldSelectionCoords = [...selectionCoords]; const combinedBlocks = [...blocks, ...selectionLayerBlocks]; const uniqueBlocks = Array.from(new Map(combinedBlocks.map((block) => [`${block.x},${block.y}`, block])).values()); setBlocks(uniqueBlocks); setSelectionLayerBlocks([]); addHistory( "Move Selection", () => { setBlocks(uniqueBlocks); setSelectionCoords(oldSelectionCoords); }, () => { setBlocks(startBlocks); setSelectionCoords(startSelectionCoords); } ); }; const cancel = () => { setBlocks(startBlocks); setSelectionLayerBlocks([]); setSelectionCoords(startSelectionCoords); }; useEffect(() => { setIsVisible(selectionLayerBlocks.length !== 0); }, [selectionLayerBlocks]); return (