"use client"; import { useEffect, useState } from "react"; import Map from "@/components/Map"; import Editor from "@/components/Editor"; import BackgroundLayersPanel from "@/components/BackgroundLayersPanel"; import { ApiError } from "@/api/http"; import { fetchGeometriesByBBox, saveGeometryBatchChanges } from "@/api/geometries"; import { FeatureCollection, useEditorState, } from "@/lib/useEditorState"; import { BackgroundLayerId, BackgroundLayerVisibility, DEFAULT_BACKGROUND_LAYER_VISIBILITY, HIDDEN_BACKGROUND_LAYER_VISIBILITY, } from "@/lib/backgroundLayers"; const EMPTY_FC: FeatureCollection = { type: "FeatureCollection", features: [] }; export default function Page() { const [mode, setMode] = useState<"idle" | "draw" | "select" | "add-point">("idle"); const [initialData, setInitialData] = useState(EMPTY_FC); const [isSaving, setIsSaving] = useState(false); const [backgroundVisibility, setBackgroundVisibility] = useState( () => ({ ...DEFAULT_BACKGROUND_LAYER_VISIBILITY }) ); const editor = useEditorState(initialData); useEffect(() => { async function loadInitial() { try { const data = await fetchGeometriesByBBox({ minLng: -180, minLat: -90, maxLng: 180, maxLat: 90, }); setInitialData(data); } catch (err) { console.error("Load initial data failed", err); } } loadInitial(); }, []); const handleSave = async () => { const payload = editor.buildPayload(); if (!payload.length) return; setIsSaving(true); try { await saveGeometryBatchChanges(payload); editor.clearChanges(); } catch (err) { if (err instanceof ApiError) { console.error("Save failed", err.body); return; } console.error("Save error", err); } finally { setIsSaving(false); } }; const handleToggleBackgroundLayer = (id: BackgroundLayerId) => { setBackgroundVisibility((prev) => ({ ...prev, [id]: !prev[id], })); }; const handleShowAllBackgroundLayers = () => { setBackgroundVisibility({ ...DEFAULT_BACKGROUND_LAYER_VISIBILITY }); }; const handleHideAllBackgroundLayers = () => { setBackgroundVisibility({ ...HIDDEN_BACKGROUND_LAYER_VISIBILITY }); }; return (
); }