"use client"; import clsx from "clsx"; import { useEffect, useMemo, useRef, useState } from "react"; import type { ScanOpportunityRow } from "@/lib/dashboard-types"; import { useLatestPatch, useSseResyncVersion } from "@/hooks/use-sse-patches"; import { Panel } from "@/components/dashboard/scan-terminal/Panel"; import { ModelCurvesSummary } from "@/components/dashboard/scan-terminal/ModelCurvesSummary"; import { TemperatureChartCanvas } from "@/components/dashboard/scan-terminal/TemperatureChartCanvas"; import { TemperatureRunwayDetails } from "@/components/dashboard/scan-terminal/TemperatureRunwayDetails"; import { TemperatureStatsBars } from "@/components/dashboard/scan-terminal/TemperatureStatsBars"; import { rowName } from "@/components/dashboard/scan-terminal/utils"; import { HOURLY_CACHE_TTL_MS, _hourlyCache, buildChartDomain, buildFullDayChartData, buildIntDegreeTicks, buildRunwayPlates, fetchHourlyForecastForCity, getActiveTemperatureSeries, getDebPeakWindowRange, getLiveObservationLabels, getObservationDisplayMetrics, getVisibleTemperatureSeries, isTemperatureSeriesVisibleByDefault, mergePatchIntoHourly, normObs, readSessionCache, seedHourlyForecastFromRow, shouldPollLiveChart, validNumber, type HourlyForecast, } from "@/components/dashboard/scan-terminal/temperature-chart-logic"; export { clearCityDetailCache } from "@/components/dashboard/scan-terminal/temperature-chart-logic"; // ── Main component ───────────────────────────────────────────────────── export function LiveTemperatureThresholdChart({ isEn, row, allRows = [], compact = false, onSearchClick, onMaximize, onClose, isMaximized = false, disableClose = false, isActive = !compact, slotIndex = 0, }: { isEn: boolean; row: ScanOpportunityRow | null; allRows?: ScanOpportunityRow[]; compact?: boolean; onSearchClick?: () => void; onMaximize?: () => void; onClose?: () => void; isMaximized?: boolean; disableClose?: boolean; isActive?: boolean; slotIndex?: number; }) { const [hourly, setHourly] = useState(null); const city = String(row?.city || "").toLowerCase().trim(); const latestPatch = useLatestPatch(city); const resyncVersion = useSseResyncVersion(); const timeframe = "1D"; const [viewMode, setViewMode] = useState<"auto" | "full">("auto"); const [userToggledKeys, setUserToggledKeys] = useState>({}); const [liveTemp, setLiveTemp] = useState(null); const [isHourlyLoading, setIsHourlyLoading] = useState(false); const hasLoadedHourlyDetailRef = useRef(false); const lastPatchAtRef = useRef(Date.now()); const lastAppliedPatchRevisionRef = useRef(0); const [showRunwayDetails, setShowRunwayDetails] = useState(true); const [refAreaLeft, setRefAreaLeft] = useState(null); const [refAreaRight, setRefAreaRight] = useState(null); const [zoomRange, setZoomRange] = useState<[number, number] | null>(null); const [targetResolution, setTargetResolution] = useState("10m"); useEffect(() => { setUserToggledKeys({}); setZoomRange(null); setViewMode("auto"); setShowRunwayDetails(true); setHourly(seedHourlyForecastFromRow(row)); setIsHourlyLoading(Boolean(city)); hasLoadedHourlyDetailRef.current = false; lastPatchAtRef.current = Date.now(); lastAppliedPatchRevisionRef.current = 0; }, [city]); useEffect(() => { if (!city) { setIsHourlyLoading(false); return; } const cacheKey = `${city}:${targetResolution}`; // Check in-memory cache first let cached = _hourlyCache.get(cacheKey); if (!cached) { // Fallback to session cache const sessionEntry = readSessionCache(cacheKey); if (sessionEntry) { cached = sessionEntry; _hourlyCache.set(cacheKey, sessionEntry); } } if (cached && Date.now() - cached.ts < HOURLY_CACHE_TTL_MS) { hasLoadedHourlyDetailRef.current = true; setHourly(cached.data); setIsHourlyLoading(false); return; } if (!hasLoadedHourlyDetailRef.current) { setHourly(seedHourlyForecastFromRow(row)); } setIsHourlyLoading(!hasLoadedHourlyDetailRef.current); let cancelled = false; // Prioritize active slots, stagger/delay background slots to optimize load performance const delay = isActive ? 0 : (slotIndex ? 300 + slotIndex * 250 : 350); const timer = setTimeout(() => { fetchHourlyForecastForCity(city, { resolution: targetResolution }) .then((data) => { if (cancelled || !data) return; hasLoadedHourlyDetailRef.current = true; setHourly(data); }) .catch(() => {}) .finally(() => { if (!cancelled) setIsHourlyLoading(false); }); }, delay); return () => { cancelled = true; clearTimeout(timer); }; }, [city, row, isActive, slotIndex, targetResolution]); useEffect(() => { if (!latestPatch || latestPatch.revision <= lastAppliedPatchRevisionRef.current) return; lastAppliedPatchRevisionRef.current = latestPatch.revision; lastPatchAtRef.current = Date.now(); const tempValue = validNumber(latestPatch.changes.temp); if (tempValue !== null) setLiveTemp(tempValue); setHourly((prev) => mergePatchIntoHourly(prev ?? seedHourlyForecastFromRow(row), latestPatch)); }, [latestPatch, row]); useEffect(() => { if (!resyncVersion || !city) return; let cancelled = false; fetchHourlyForecastForCity(city, { ignoreCache: true, resolution: targetResolution }) .then((data) => { if (cancelled || !data) return; hasLoadedHourlyDetailRef.current = true; setHourly(data); }) .catch(() => {}) .finally(() => { if (!cancelled) setIsHourlyLoading(false); }); return () => { cancelled = true; }; }, [resyncVersion, city, targetResolution]); // ── SSE fallback: only full-fetch if a visible chart has seen no patch for 2 minutes ── useEffect(() => { if (!shouldPollLiveChart({ city, compact, isActive, isMaximized })) return; let cancelled = false; const refreshFullDetail = () => { lastPatchAtRef.current = Date.now(); fetchHourlyForecastForCity(city, { ignoreCache: true }) .then((data) => { if (cancelled || !data) return; hasLoadedHourlyDetailRef.current = true; setHourly(data); }) .catch(() => {}) .finally(() => { if (!cancelled) setIsHourlyLoading(false); }); }; const checkFallback = () => { if (typeof document !== "undefined" && document.visibilityState === "hidden") return; if (Date.now() - lastPatchAtRef.current < 2 * 60_000) return; fetch(`/api/city/${encodeURIComponent(city)}/summary`) .then((res) => (res.ok ? res.json() : null)) .then((payload) => { if (cancelled || !payload) return; const temp = validNumber(payload?.current?.temp); if (temp !== null) setLiveTemp(temp); }) .catch(() => {}); refreshFullDetail(); }; const id = setInterval(checkFallback, 60_000); return () => { cancelled = true; clearInterval(id); }; }, [city, compact, isActive, isMaximized]); const { data, series } = useMemo(() => buildFullDayChartData(row, hourly, isEn), [row, hourly, isEn]); const autoWindowRange = useMemo( () => (viewMode === "auto" ? getDebPeakWindowRange(data, series) : null), [data, series, viewMode], ); const visibleRange = zoomRange ?? autoWindowRange; const visibleRangeKey = visibleRange ? `${visibleRange[0]}:${visibleRange[1]}` : "full"; const zoomedData = useMemo(() => { if (!visibleRange || data.length === 0) return data; const [start, end] = visibleRange; return data.slice(start, end + 1); }, [data, visibleRangeKey]); const nextTargetResolution = useMemo(() => { if (visibleRange && data.length > 0) { const zoomedData = data.slice(visibleRange[0], visibleRange[1] + 1); if (zoomedData.length > 0) { const startTs = zoomedData[0].ts; const endTs = zoomedData[zoomedData.length - 1].ts; if (endTs - startTs <= 2 * 60 * 60 * 1000) { return "1m"; } } } return "10m"; }, [data, visibleRangeKey]); useEffect(() => { if (targetResolution !== nextTargetResolution) { setTargetResolution(nextTargetResolution); } }, [targetResolution, nextTargetResolution]); const tzOffset = row?.tz_offset_seconds ?? 0; const settlementObs = useMemo(() => { let obs = normObs(hourly?.settlementTodayObs || row?.settlement_today_obs || row?.metar_context?.settlement_today_obs, tzOffset, undefined, row?.local_date || null); if (!obs.length && !hourly?.runwayPlateHistory) { const mObs = normObs(hourly?.metarTodayObs || row?.metar_today_obs || row?.metar_context?.today_obs, tzOffset, undefined, row?.local_date || null); if (mObs.length > 0) { obs = mObs; } } return obs; }, [row, hourly, tzOffset]); const runwayPlates = useMemo(() => buildRunwayPlates(hourly?.amos, row, settlementObs), [hourly?.amos, row, settlementObs]); const hasRunwayData = runwayPlates.length > 0; const settlementPlate = useMemo(() => runwayPlates.find((p) => p.isSettlement), [runwayPlates]); const chartSeries = useMemo(() => { return series; }, [series]); const isSeriesVisible = (sKey: string) => { if (userToggledKeys[sKey] !== undefined) { return userToggledKeys[sKey]; } return isTemperatureSeriesVisibleByDefault(city, sKey); }; const activeSeries = useMemo(() => { return getActiveTemperatureSeries( city, chartSeries, userToggledKeys, showRunwayDetails, ); }, [chartSeries, userToggledKeys, city, showRunwayDetails]); const { isShenzhen, metarHeaderLabel, metarHighLabel, runwayHeaderLabel, runwayHighLabel, } = useMemo(() => getLiveObservationLabels(row, hourly), [row, hourly]); const { currentRunwayTemp, observedHighMetar, observedHighRunway } = useMemo( () => getObservationDisplayMetrics(row, hourly, settlementPlate), [row, hourly, settlementPlate], ); const displayRunwayTemp = liveTemp ?? currentRunwayTemp; const wundergroundDailyHigh = validNumber(hourly?.airportCurrent?.max_so_far ?? hourly?.airportPrimary?.max_so_far) ?? null; const localDateStr = row?.local_date || new Date().toISOString().slice(0, 10); const modelSources = (row?.model_cluster_sources && Object.keys(row.model_cluster_sources).length > 0) ? row.model_cluster_sources : (hourly?.multiModelDaily?.[localDateStr]?.models || null); const modelValues = Object.values(modelSources || {}) .map(validNumber) .filter((v): v is number => v !== null); const modelMin = modelValues.length ? Math.min(...modelValues) : (row?.cluster_core_low ?? null); const modelMax = modelValues.length ? Math.max(...modelValues) : (row?.cluster_core_high ?? null); const debVal = validNumber(hourly?.debPrediction) ?? validNumber(row?.deb_prediction) ?? null; const spread = (modelMax !== null && modelMin !== null) ? modelMax - modelMin : null; const spreadLabel = spread === null ? "" : (spread <= 2.0 ? "低分歧" : (spread <= 4.0 ? "中等分歧" : "高分歧")); const spreadLabelEn = spread === null ? "" : (spread <= 2.0 ? "Low" : (spread <= 4.0 ? "Medium" : "High")); const formattedUpdateTime = useMemo(() => { const nowUtc = Date.now(); const cityOffsetMs = (row?.tz_offset_seconds ?? 0) * 1000; const cityNow = new Date(nowUtc + cityOffsetMs + new Date().getTimezoneOffset() * 60_000); const pad = (n: number) => String(n).padStart(2, "0"); const y = cityNow.getFullYear(); const mo = pad(cityNow.getMonth() + 1); const d = pad(cityNow.getDate()); const hh = pad(cityNow.getHours()); const mm = pad(cityNow.getMinutes()); const ss = pad(cityNow.getSeconds()); return `${y}-${mo}-${d} ${hh}:${mm}:${ss}`; }, [row]); const cityThresholds = useMemo(() => { if (!row || !allRows || !allRows.length) return []; const cityKey = String(row.city || "").toLowerCase().trim(); const sameCityRows = allRows.filter( (r) => String(r.city || "").toLowerCase().trim() === cityKey ); const seen = new Set(); const list: { threshold: number; label: string; isBreached: boolean; kind: "gte" | "lte" }[] = []; sameCityRows.forEach((r) => { const t = Number(r.target_threshold ?? r.target_value ?? r.target_lower ?? r.target_upper); if (!Number.isFinite(t) || seen.has(t)) return; seen.add(t); const maxTemp = Number(r.current_max_so_far ?? r.current_temp ?? 0); const q = String(r.market_question || r.target_label || "").toLowerCase(); const kind: "gte" | "lte" = q.includes("below") || q.includes("under") || q.includes("lte") ? "lte" : "gte"; const isBreached = kind === "lte" ? maxTemp > t : maxTemp >= t; list.push({ threshold: t, label: r.target_label || `${t}°C`, isBreached, kind, }); }); return list.sort((a, b) => a.threshold - b.threshold); }, [row, allRows]); const intDegreeTicks = useMemo(() => buildIntDegreeTicks(activeSeries, zoomedData), [activeSeries, zoomedData]); const chartDomain = useMemo( () => buildChartDomain(activeSeries, zoomedData), [activeSeries, zoomedData], ); const subtitle = row ? (isEn ? "Live & Forecast" : "实测与预测") : ""; const panelTitle = row ? (
· {subtitle}
) : isEn ? ( "Temperature Chart" ) : ( "气温图表" ); const timeframeActions = (
{zoomRange && ( )}
{(["auto", "full"] as const).map((mode) => ( ))}
{(onMaximize || onClose) && (
{onMaximize && ( )} {onClose && ( )}
)}
); const handleMouseDown = (e: any) => { if (compact || !e) return; if (typeof e.activeTooltipIndex === "number") { setRefAreaLeft(e.activeTooltipIndex); setRefAreaRight(e.activeTooltipIndex); } }; const handleMouseMove = (e: any) => { if (compact || !e || refAreaLeft === null) return; if (typeof e.activeTooltipIndex === "number") { setRefAreaRight(e.activeTooltipIndex); } }; const handleMouseUp = () => { if (refAreaLeft === null || refAreaRight === null) { setRefAreaLeft(null); setRefAreaRight(null); return; } let leftIdx = refAreaLeft; let rightIdx = refAreaRight; if (leftIdx > rightIdx) { [leftIdx, rightIdx] = [rightIdx, leftIdx]; } if (rightIdx - leftIdx >= 1) { const originalStartIndex = visibleRange ? visibleRange[0] : 0; const newStart = originalStartIndex + leftIdx; const newEnd = originalStartIndex + rightIdx; setZoomRange([newStart, newEnd]); } setRefAreaLeft(null); setRefAreaRight(null); }; return (
{timeframe === "1D" && !compact && ( )} {timeframe === "1D" && !compact && ( )} setZoomRange(null)} isSeriesVisible={isSeriesVisible} onSeriesToggle={(seriesKey) => { setUserToggledKeys((prev) => ({ ...prev, [seriesKey]: !isSeriesVisible(seriesKey), })); }} onShowRunwayDetailsChange={setShowRunwayDetails} />
); } export function __buildTemperatureChartDataForTest( row: ScanOpportunityRow | null, hourly: HourlyForecast, _timeframe = "1D", isEn = false, ) { return buildFullDayChartData(row, hourly, isEn); } export const __isTemperatureSeriesVisibleByDefaultForTest = isTemperatureSeriesVisibleByDefault; export const __getVisibleTemperatureSeriesForTest = getVisibleTemperatureSeries; export const __getActiveTemperatureSeriesForTest = getActiveTemperatureSeries; export const __getDebPeakWindowRangeForTest = getDebPeakWindowRange; export const __getLiveObservationLabelsForTest = getLiveObservationLabels; export const __getObservationDisplayMetricsForTest = getObservationDisplayMetrics; export const __shouldPollLiveChartForTest = shouldPollLiveChart; export const __mergePatchIntoHourlyForTest = mergePatchIntoHourly;