import React, { useReducer, useRef } from "react"; import { useMap } from "@vis.gl/react-google-maps"; import reducer, { useDrawingManagerEvents, useOverlaySnapshots } from "@/components/map-component/undo-redo"; import { DrawingActionKind } from "@/types"; interface Props { drawingManager: google.maps.drawing.DrawingManager | null; } export const UndoRedoControl = ({ drawingManager }: Props) => { const map = useMap(); const [state, dispatch] = useReducer(reducer, { past: [], now: [], future: [], }); // We need this ref to prevent infinite loops in certain cases. // For example when the radius of circle is set via code (and not by user interaction) // the radius_changed event gets triggered again. This would cause an infinite loop. // This solution can be improved by comparing old vs. new values. For now we turn // off the "updating" when snapshot changes are applied back to the overlays. const overlaysShouldUpdateRef = useRef(false); useDrawingManagerEvents(drawingManager, overlaysShouldUpdateRef, dispatch); useOverlaySnapshots(map, state, overlaysShouldUpdateRef); return (
); };