From 07df390b8e2475e4be2d211971c2d650685fdaa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=91=E5=91=A8?= <154221037+YunzhouLi-hub@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:32:16 +0800 Subject: [PATCH] fix(web): distinguish unavailable and stale noise status --- .../system/settings/noise-profile-card.tsx | 70 +++++++++++++------ web/src/i18n/locales/tr.json | 4 ++ web/src/i18n/locales/zh-CN.json | 4 ++ 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/web/src/app/(main)/system/settings/noise-profile-card.tsx b/web/src/app/(main)/system/settings/noise-profile-card.tsx index ab31521f..feeab0de 100644 --- a/web/src/app/(main)/system/settings/noise-profile-card.tsx +++ b/web/src/app/(main)/system/settings/noise-profile-card.tsx @@ -41,23 +41,39 @@ const profiles: { value: NoiseProfileName; label: string; limits: string }[] = [ }, ]; +type NoiseReading = + | { state: "loading"; status: NoiseStatus | null } + | { state: "ready"; status: NoiseStatus } + | { state: "error"; status: NoiseStatus | null; message: string }; + // NoiseProfileCard sets the target noise profile and shows the per-host // counters of the current minute. export function NoiseProfileCard() { - const [status, setStatus] = React.useState(null); - const [loading, setLoading] = React.useState(false); + const [reading, setReading] = React.useState({ state: "loading", status: null }); const [saving, setSaving] = React.useState(false); + const latestRequest = React.useRef(0); - const refresh = React.useCallback(() => { - setLoading(true); - api - .noiseStatus() - .then(setStatus) - .catch((e) => toast.error(t("Failed to load the noise counters: {error}", { error: (e as Error).message }))) - .finally(() => setLoading(false)); + const refresh = React.useCallback(async () => { + const request = ++latestRequest.current; + setReading(({ status }) => ({ state: "loading", status })); + try { + const status = await api.noiseStatus(); + if (!status || !profiles.some((profile) => profile.value === status.profile) || !Array.isArray(status.hosts)) { + throw new Error(t("Invalid noise status response.")); + } + if (request === latestRequest.current) setReading({ state: "ready", status }); + } catch (error) { + if (request === latestRequest.current) { + const message = error instanceof Error ? error.message : String(error); + setReading(({ status }) => ({ state: "error", status, message })); + } + } }, []); React.useEffect(() => { - refresh(); + void refresh(); + return () => { + latestRequest.current += 1; + }; }, [refresh]); const save = (value: NoiseProfileName) => { @@ -68,13 +84,15 @@ export function NoiseProfileCard() { toast.success( t("Noise profile set to {profile}", { profile: profiles.find((p) => p.value === value)?.label ?? value }), ); - refresh(); + return refresh(); }) .catch((e) => toast.error(t("Save failed: {error}", { error: (e as Error).message }))) .finally(() => setSaving(false)); }; - const current = profiles.find((p) => p.value === status?.profile) ?? profiles[0]; + const { status } = reading; + const loading = reading.state === "loading"; + const current = profiles.find((p) => p.value === status?.profile); return ( @@ -95,11 +113,11 @@ export function NoiseProfileCard() { -

{current.limits}

+ {reading.state === "error" && ( +

+ {t("Failed to load the noise counters: {error}", { error: reading.message })} +

+ )} + {status && reading.state !== "ready" && ( +

+ {t("Showing the last successfully loaded noise profile and counters.")} +

+ )} + {current &&

{current.limits}

}
- {t("Hosts, current minute")} - @@ -145,11 +175,11 @@ export function NoiseProfileCard() { ))} - ) : ( + ) : reading.state === "ready" ? (

- {status?.profile === "off" ? t("Off counts no request.") : t("No paced request yet.")} + {reading.status.profile === "off" ? t("Off counts no request.") : t("No paced request yet.")}

- )} + ) : null} ); diff --git a/web/src/i18n/locales/tr.json b/web/src/i18n/locales/tr.json index 90bfc3b1..aa5a9ccd 100644 --- a/web/src/i18n/locales/tr.json +++ b/web/src/i18n/locales/tr.json @@ -1185,6 +1185,7 @@ "Interval ms": "Aralık (ms)", "Invalid CIDR format, e.g. 192.168.0.0/16": "Geçersiz CIDR biçimi, ör. 192.168.0.0/16", "Invalid headers JSON": "Geçersiz başlık JSON'u", + "Invalid noise status response.": "Gürültü durumu yanıtı geçersiz.", "Invalid parameter JSON Schema": "Geçersiz parametre JSON Schema'sı", "Invalid regex syntax": "Geçersiz regex sözdizimi", "Invalid request": "Geçersiz istek", @@ -1211,6 +1212,7 @@ "Last {count} calls": "Son {count} çağrı", "Last {count} Q&A exchanges verbatim": "Son {count} soru-cevap aynen", "Last activity": "Son etkinlik", + "Last available host counters": "Son alınan ana makine sayaçları", "Last called": "Son çağrı", "Latency": "Gecikme", "Latest details read on send · Up to 10 records": "Gönderimde en güncel ayrıntılar okunur · En fazla 10 kayıt", @@ -1527,6 +1529,7 @@ "Node #{id}": "Düğüm #{id}", "Noise counters per host": "Ana makine başına gürültü sayaçları", "Noise profile set to {profile}": "Gürültü profili {profile} olarak ayarlandı", + "Noise status unavailable": "Gürültü durumu alınamadı", "none": "yok", "None of them is high risk.": "Hiçbiri yüksek riskli değil.", "Not assessed": "Değerlendirilmedi", @@ -2119,6 +2122,7 @@ "shell/command/script/http, {count} / {total} matching; click a card to edit": "shell/command/script/http, {count} / {total} eşleşme; düzenlemek için bir karta tıklayın", "Show more (+{count})": "Daha fazla göster (+{count})", "Showing {count}, scroll down for more": "{count} kayıt gösteriliyor, daha fazlası için aşağı kaydırın", + "Showing the last successfully loaded noise profile and counters.": "Son başarıyla yüklenen gürültü profili ve sayaçlar gösteriliyor.", "Side question": "Yan soru", "Side question panel": "Yan soru paneli", "Sidebar": "Kenar Çubuğu", diff --git a/web/src/i18n/locales/zh-CN.json b/web/src/i18n/locales/zh-CN.json index 5ca5af25..10307ae0 100644 --- a/web/src/i18n/locales/zh-CN.json +++ b/web/src/i18n/locales/zh-CN.json @@ -1185,6 +1185,7 @@ "Interval ms": "间隔 ms", "Invalid CIDR format, e.g. 192.168.0.0/16": "CIDR 格式无效,形如 192.168.0.0/16", "Invalid headers JSON": "headers JSON 格式错误", + "Invalid noise status response.": "请求强度状态响应无效。", "Invalid parameter JSON Schema": "参数 JSON Schema 格式错误", "Invalid regex syntax": "正则表达式语法无效", "Invalid request": "无效请求", @@ -1211,6 +1212,7 @@ "Last {count} calls": "最近 {count} 次调用", "Last {count} Q&A exchanges verbatim": "最近 {count} 轮问答原文", "Last activity": "最近活动", + "Last available host counters": "上次取得的主机统计", "Last called": "最近调用", "Latency": "延迟", "Latest details read on send · Up to 10 records": "发送时读取最新详情 · 最多 10 条", @@ -1527,6 +1529,7 @@ "Node #{id}": "节点 #{id}", "Noise counters per host": "各主机请求强度统计", "Noise profile set to {profile}": "请求强度策略已设为 {profile}", + "Noise status unavailable": "无法读取请求强度状态", "none": "无", "None of them is high risk.": "其中没有高风险项。", "Not assessed": "未评估", @@ -2119,6 +2122,7 @@ "shell/command/script/http, {count} / {total} matching; click a card to edit": "shell/command/script/http,匹配 {count} / {total} 项;点击卡片可编辑", "Show more (+{count})": "显示更多(+{count})", "Showing {count}, scroll down for more": "已显示 {count} 项,向下滚动加载更多", + "Showing the last successfully loaded noise profile and counters.": "正在显示上次成功加载的请求强度策略和统计数据。", "Side question": "旁路提问", "Side question panel": "旁路提问面板", "Sidebar": "侧边栏",