import { Check, Map } from "lucide-react"; import { type FocusEvent, useCallback, useEffect, useRef, useState } from "react"; import { cn } from "@/shared/ui/cn"; import { MAP_CONTROL_RADIUS_CLASS_NAME, MAP_CONTROL_SURFACE_CLASS_NAME, MAP_ICON_CELL_RADIUS_CLASS_NAME, MAP_MAJOR_PANEL_RADIUS_CLASS_NAME, MAP_READABLE_RADIUS_CLASS_NAME, MAP_READABLE_SURFACE_CLASS_NAME, MAP_TOOL_PANEL_SURFACE_CLASS_NAME } from "./map-control-styles"; export type BaseLayerOption = { id: string; label: string; description?: string; disabled?: boolean; swatchClassName?: string; }; type BaseLayersControlProps = { options: BaseLayerOption[]; activeLayerId: string; onSelectLayer: (id: string) => void; title?: string; variant?: "floating" | "panel"; className?: string; }; export function BaseLayersControl({ options, activeLayerId, onSelectLayer, title = "底图", variant = "floating", className = "" }: BaseLayersControlProps) { const activeLayer = options.find((option) => option.id === activeLayerId) ?? options[0]; const [isOpen, setIsOpen] = useState(false); const closeTimerRef = useRef(null); const clearCloseTimer = useCallback(() => { if (closeTimerRef.current) { window.clearTimeout(closeTimerRef.current); closeTimerRef.current = null; } }, []); const openControl = useCallback(() => { clearCloseTimer(); setIsOpen(true); }, [clearCloseTimer]); const scheduleClose = useCallback(() => { clearCloseTimer(); closeTimerRef.current = window.setTimeout(() => { setIsOpen(false); closeTimerRef.current = null; }, 260); }, [clearCloseTimer]); const handleBlur = useCallback( (event: FocusEvent) => { if (event.currentTarget.contains(event.relatedTarget)) { return; } scheduleClose(); }, [scheduleClose] ); useEffect(() => clearCloseTimer, [clearCloseTimer]); if (variant === "panel") { return (

{title}

当前 {activeLayer?.label ?? "--"}

{options.map((option) => { const active = option.id === activeLayerId; return ( ); })}
); } return (
{options.map((option) => { const active = option.id === activeLayerId; return ( ); })}
); } type BaseLayerSwatchProps = { option: BaseLayerOption; active: boolean; }; function BaseLayerSwatch({ option, active }: BaseLayerSwatchProps) { return ( {active ? ( ) : null} ); }