"use client"; import { Check, Copy, Target, TriangleAlert, X } from "lucide-react"; import Image from "next/image"; import { useEffect, useMemo, useRef, useState } from "react"; import { showMapNotice } from "@/features/map/core/components/notice"; import { cn } from "@/lib/utils"; import { SCADA_ICON_PATHS } from "../map/scada"; import type { DetailFeature } from "../types"; import { formatFeaturePropertyValue, getLocalizedFeatureProperties } from "../utils/feature-properties"; type FeatureInsightPanelProps = { feature: DetailFeature | null; onClose: () => void; }; export function FeatureInsightPanel({ feature, onClose }: FeatureInsightPanelProps) { const [copiedScadaUuid, setCopiedScadaUuid] = useState(false); const copyResetTimeoutRef = useRef | null>(null); useEffect(() => () => { if (copyResetTimeoutRef.current) { clearTimeout(copyResetTimeoutRef.current); } }, []); const metrics = useMemo(() => { if (!feature) { return []; } switch (feature.layer) { case "conduits": return [ ["管径", formatFeaturePropertyValue("diameter", feature.properties.diameter)], ["长度", formatFeaturePropertyValue("length", feature.properties.length)], ["粗糙系数", formatFeaturePropertyValue("roughness", feature.properties.roughness)], ["状态", formatFeaturePropertyValue("status", feature.properties.status)] ]; case "junctions": return [ ["最大深度", formatFeaturePropertyValue("max_depth", feature.properties.max_depth)], ["井底高程", formatFeaturePropertyValue("invert_elevation", feature.properties.invert_elevation)], ["对象类型", "检查井"], ["状态", "在线"] ]; case "orifices": return [ ["孔口类型", formatFeaturePropertyValue("orifice_type", feature.properties.orifice_type)], ["断面形状", formatFeaturePropertyValue("shape", feature.properties.shape)], ["流量系数", formatFeaturePropertyValue("discharge_coeff", feature.properties.discharge_coeff)], ["闸门", formatFeaturePropertyValue("gated", feature.properties.gated)] ]; case "pumps": return [ ["状态", formatFeaturePropertyValue("status", feature.properties.status)], ["泵曲线", formatFeaturePropertyValue("pump_curve", feature.properties.pump_curve)], ["启动水深", formatFeaturePropertyValue("startup_depth", feature.properties.startup_depth)], ["停泵水深", formatFeaturePropertyValue("shutoff_depth", feature.properties.shutoff_depth)] ]; case "outfalls": return [ ["排放类型", formatFeaturePropertyValue("outfall_type", feature.properties.outfall_type)], ["井底高程", formatFeaturePropertyValue("invert_elevation", feature.properties.invert_elevation)], ["阶段数据", formatFeaturePropertyValue("stage_data", feature.properties.stage_data)], ["状态", "在线"] ]; case "scada": return [ ["设备类型", formatFeaturePropertyValue("kind", feature.properties.kind)], ["运行状态", formatFeaturePropertyValue("active", feature.properties.active)], ["关联检查井", formatFeaturePropertyValue("junction_id", feature.properties.junction_id)], ["同步时间", formatFeaturePropertyValue("synced_at", feature.properties.synced_at)] ]; } }, [feature]); const propertyEntries = useMemo(() => { if (!feature) { return []; } const properties = feature.layer === "scada" && feature.properties.scada_id === undefined ? { scada_id: feature.id, ...feature.properties } : feature.properties; return getLocalizedFeatureProperties(properties); }, [feature]); const scadaQueryUuid = feature?.layer === "scada" ? getCopyablePropertyValue(feature.properties.scada_id ?? feature.id) : null; const handleCopyScadaUuid = async () => { if (!scadaQueryUuid) { return; } try { if (!navigator.clipboard) { throw new Error("Clipboard API unavailable"); } await navigator.clipboard.writeText(scadaQueryUuid); setCopiedScadaUuid(true); if (copyResetTimeoutRef.current) { clearTimeout(copyResetTimeoutRef.current); } copyResetTimeoutRef.current = setTimeout(() => setCopiedScadaUuid(false), 1500); } catch { showMapNotice({ tone: "error", title: "无法复制 UUID", message: "请手动选择查询 UUID 后复制。" }); } }; return ( ); } function getCopyablePropertyValue(value: unknown) { if (typeof value === "string" && value.trim().length > 0) { return value.trim(); } if (typeof value === "number" || typeof value === "bigint") { return String(value); } return null; } function getScadaPresentation(rawKind: unknown) { const kind = String(rawKind ?? ""); if (kind === "water_quality") return { iconPath: SCADA_ICON_PATHS.waterQuality, sectionClassName: "material-tone-normal border-teal-200", labelClassName: "text-teal-800" }; if (kind === "radar_level") return { iconPath: SCADA_ICON_PATHS.radarLevel, sectionClassName: "material-tone-info border-blue-200", labelClassName: "text-blue-800" }; if (kind === "ultrasonic_flow") return { iconPath: SCADA_ICON_PATHS.ultrasonicFlow, sectionClassName: "material-tone-warning border-orange-200", labelClassName: "text-orange-800" }; return { iconPath: SCADA_ICON_PATHS.unknown, sectionClassName: "surface-control border-transparent", labelClassName: "text-slate-800" }; }