2026-03-09 10:36:03 +08:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { ChartConfiguration } from "chart.js/auto";
|
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
|
import { useChart } from "@/hooks/useChart";
|
|
|
|
|
|
import { useDashboardStore, useHistoryData } from "@/hooks/useDashboardStore";
|
2026-03-10 04:45:40 +08:00
|
|
|
|
import { useI18n } from "@/hooks/useI18n";
|
2026-03-09 10:36:03 +08:00
|
|
|
|
import { getHistorySummary } from "@/lib/dashboard-utils";
|
|
|
|
|
|
|
|
|
|
|
|
function HistoryChart() {
|
|
|
|
|
|
const store = useDashboardStore();
|
2026-03-10 04:45:40 +08:00
|
|
|
|
const { locale } = useI18n();
|
2026-03-09 10:36:03 +08:00
|
|
|
|
const { data } = useHistoryData();
|
|
|
|
|
|
const summary = useMemo(
|
|
|
|
|
|
() => getHistorySummary(data, store.selectedDetail?.local_date),
|
|
|
|
|
|
[data, store.selectedDetail?.local_date],
|
|
|
|
|
|
);
|
|
|
|
|
|
const hasMgm =
|
|
|
|
|
|
store.selectedCity === "ankara" &&
|
|
|
|
|
|
summary.mgms.some((value) => value != null);
|
|
|
|
|
|
|
|
|
|
|
|
const canvasRef = useChart(
|
|
|
|
|
|
() => {
|
|
|
|
|
|
const datasets: NonNullable<ChartConfiguration<"line">["data"]>["datasets"] = [
|
|
|
|
|
|
{
|
|
|
|
|
|
backgroundColor: "rgba(248, 113, 113, 0.1)",
|
|
|
|
|
|
borderColor: "#f87171",
|
|
|
|
|
|
borderWidth: 2,
|
|
|
|
|
|
data: summary.actuals,
|
2026-03-10 04:45:40 +08:00
|
|
|
|
label: locale === "en-US" ? "Observed High" : "实测最高温",
|
2026-03-09 10:36:03 +08:00
|
|
|
|
pointBackgroundColor: "#f87171",
|
|
|
|
|
|
pointBorderColor: "#fff",
|
2026-03-10 04:45:40 +08:00
|
|
|
|
pointHoverRadius: 7,
|
|
|
|
|
|
pointRadius: 5,
|
2026-03-09 10:36:03 +08:00
|
|
|
|
tension: 0.2,
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
|
|
borderColor: "#34d399",
|
|
|
|
|
|
borderDash: [5, 4],
|
|
|
|
|
|
borderWidth: 2,
|
|
|
|
|
|
data: summary.debs,
|
2026-03-10 04:45:40 +08:00
|
|
|
|
label: locale === "en-US" ? "DEB Fusion" : "DEB 融合",
|
|
|
|
|
|
pointHoverRadius: 6,
|
|
|
|
|
|
pointRadius: 4,
|
2026-03-09 10:36:03 +08:00
|
|
|
|
tension: 0.2,
|
|
|
|
|
|
},
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
if (hasMgm) {
|
|
|
|
|
|
datasets.push({
|
|
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
|
|
borderColor: "#fb923c",
|
|
|
|
|
|
borderWidth: 2,
|
|
|
|
|
|
data: summary.mgms,
|
2026-03-10 04:45:40 +08:00
|
|
|
|
label: locale === "en-US" ? "MGM Official Forecast" : "MGM 官方预报",
|
|
|
|
|
|
pointHoverRadius: 6,
|
|
|
|
|
|
pointRadius: 4,
|
2026-03-09 10:36:03 +08:00
|
|
|
|
tension: 0.2,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
data: {
|
|
|
|
|
|
datasets,
|
|
|
|
|
|
labels: summary.dates,
|
|
|
|
|
|
},
|
|
|
|
|
|
options: {
|
|
|
|
|
|
interaction: { intersect: false, mode: "index" },
|
|
|
|
|
|
maintainAspectRatio: false,
|
|
|
|
|
|
plugins: {
|
|
|
|
|
|
legend: {
|
|
|
|
|
|
labels: {
|
2026-03-10 04:45:40 +08:00
|
|
|
|
boxHeight: 12,
|
|
|
|
|
|
boxWidth: 34,
|
2026-03-09 10:36:03 +08:00
|
|
|
|
color: "#94a3b8",
|
2026-03-10 04:45:40 +08:00
|
|
|
|
font: { family: "Inter", size: 14 },
|
|
|
|
|
|
padding: 18,
|
2026-03-09 10:36:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
tooltip: {
|
|
|
|
|
|
backgroundColor: "rgba(15, 23, 42, 0.9)",
|
|
|
|
|
|
borderColor: "rgba(255, 255, 255, 0.1)",
|
|
|
|
|
|
borderWidth: 1,
|
2026-03-10 04:45:40 +08:00
|
|
|
|
bodyFont: { family: "Inter", size: 13 },
|
|
|
|
|
|
titleFont: { family: "Inter", size: 13, weight: 600 },
|
2026-03-09 10:36:03 +08:00
|
|
|
|
callbacks: {
|
|
|
|
|
|
label: (ctx) => `${ctx.dataset.label}: ${ctx.parsed.y?.toFixed(1)}°`,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
responsive: true,
|
|
|
|
|
|
scales: {
|
|
|
|
|
|
x: {
|
|
|
|
|
|
grid: { color: "rgba(255,255,255,0.04)" },
|
2026-03-10 04:45:40 +08:00
|
|
|
|
ticks: {
|
|
|
|
|
|
color: "#64748b",
|
|
|
|
|
|
font: { family: "Inter", size: 12 },
|
|
|
|
|
|
padding: 8,
|
|
|
|
|
|
},
|
2026-03-09 10:36:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
y: {
|
|
|
|
|
|
grid: { color: "rgba(255,255,255,0.04)" },
|
2026-03-10 04:45:40 +08:00
|
|
|
|
ticks: {
|
|
|
|
|
|
color: "#64748b",
|
|
|
|
|
|
font: { family: "Inter", size: 12 },
|
|
|
|
|
|
padding: 8,
|
|
|
|
|
|
},
|
2026-03-09 10:36:03 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
type: "line",
|
|
|
|
|
|
} satisfies ChartConfiguration<"line">;
|
|
|
|
|
|
},
|
2026-03-10 04:45:40 +08:00
|
|
|
|
[hasMgm, summary, locale],
|
2026-03-09 10:36:03 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!summary.recentData.length) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="history-chart-wrapper">
|
|
|
|
|
|
<canvas ref={canvasRef} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function HistoryModal() {
|
|
|
|
|
|
const store = useDashboardStore();
|
2026-03-10 04:45:40 +08:00
|
|
|
|
const { t } = useI18n();
|
2026-03-09 10:36:03 +08:00
|
|
|
|
const { data, error, isLoading, isOpen } = useHistoryData();
|
|
|
|
|
|
const summary = useMemo(
|
|
|
|
|
|
() => getHistorySummary(data, store.selectedDetail?.local_date),
|
|
|
|
|
|
[data, store.selectedDetail?.local_date],
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (!isOpen) return null;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div
|
|
|
|
|
|
className="modal-overlay"
|
|
|
|
|
|
role="dialog"
|
|
|
|
|
|
aria-modal="true"
|
|
|
|
|
|
aria-labelledby="history-modal-title"
|
|
|
|
|
|
onClick={(event) => {
|
|
|
|
|
|
if (event.target === event.currentTarget) {
|
|
|
|
|
|
store.closeHistory();
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
|
|
|
|
|
>
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<div className="modal-content history-modal">
|
2026-03-09 10:36:03 +08:00
|
|
|
|
<div className="modal-header">
|
|
|
|
|
|
<h2 id="history-modal-title">
|
2026-03-10 04:45:40 +08:00
|
|
|
|
{t("history.title", { city: store.selectedCity?.toUpperCase() || "" })}
|
2026-03-09 10:36:03 +08:00
|
|
|
|
</h2>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="modal-close"
|
2026-03-10 04:45:40 +08:00
|
|
|
|
aria-label={t("history.closeAria")}
|
2026-03-09 10:36:03 +08:00
|
|
|
|
onClick={store.closeHistory}
|
|
|
|
|
|
>
|
2026-03-10 04:45:40 +08:00
|
|
|
|
×
|
2026-03-09 10:36:03 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="modal-body">
|
|
|
|
|
|
<div className="history-stats">
|
|
|
|
|
|
{isLoading ? (
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span style={{ color: "var(--text-muted)" }}>{t("history.loading")}</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
) : error ? (
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span style={{ color: "var(--accent-red)" }}>{t("history.error")}</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
) : !summary.recentData.length ? (
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span style={{ color: "var(--text-muted)" }}>{t("history.empty")}</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<div className="h-stat-card">
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span className="label">{t("history.hitRate")}</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
<span className="val">
|
|
|
|
|
|
{summary.hitRate != null ? `${summary.hitRate}%` : "--"}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="h-stat-card">
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span className="label">{t("history.mae")}</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
<span className="val">
|
|
|
|
|
|
{summary.debMae != null ? `${summary.debMae}°` : "--"}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div className="h-stat-card">
|
2026-03-10 04:45:40 +08:00
|
|
|
|
<span className="label">{t("history.sample")}</span>
|
|
|
|
|
|
<span className="val">
|
|
|
|
|
|
{t("history.sampleDays", { count: summary.settledCount })}
|
|
|
|
|
|
</span>
|
2026-03-09 10:36:03 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{!isLoading && !error && <HistoryChart />}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|