fix(web): distinguish unavailable and stale noise status

This commit is contained in:
李云周
2026-11-23 10:32:16 +08:00
parent 38c7477c51
commit 07df390b8e
3 changed files with 58 additions and 20 deletions
@@ -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<NoiseStatus | null>(null);
const [loading, setLoading] = React.useState(false);
const [reading, setReading] = React.useState<NoiseReading>({ 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 (
<Card className="mb-4 break-inside-avoid md:mb-6">
<CardHeader>
@@ -95,11 +113,11 @@ export function NoiseProfileCard() {
</Label>
<Select
value={status?.profile ?? ""}
disabled={!status || saving}
disabled={reading.state !== "ready" || saving}
onValueChange={(v) => save(v as NoiseProfileName)}
>
<SelectTrigger id="noise-profile" className="w-48 shrink-0">
<SelectValue placeholder={t("Loading…")} />
<SelectValue placeholder={loading ? t("Loading…") : t("Noise status unavailable")} />
</SelectTrigger>
<SelectContent>
{profiles.map((p) => (
@@ -110,10 +128,22 @@ export function NoiseProfileCard() {
</SelectContent>
</Select>
</div>
<p className="text-muted-foreground text-xs">{current.limits}</p>
{reading.state === "error" && (
<p className="text-destructive text-xs" role="alert">
{t("Failed to load the noise counters: {error}", { error: reading.message })}
</p>
)}
{status && reading.state !== "ready" && (
<p className="text-muted-foreground text-xs" role="status">
{t("Showing the last successfully loaded noise profile and counters.")}
</p>
)}
{current && <p className="text-muted-foreground text-xs">{current.limits}</p>}
<div className="flex items-center justify-between gap-4">
<span className="font-medium text-sm">{t("Hosts, current minute")}</span>
<Button variant="outline" size="sm" onClick={refresh} disabled={loading}>
<span className="font-medium text-sm">
{status && reading.state !== "ready" ? t("Last available host counters") : t("Hosts, current minute")}
</span>
<Button variant="outline" size="sm" onClick={refresh} disabled={loading || saving}>
<RefreshCwIcon data-icon="inline-start" className={loading ? "animate-spin" : undefined} />
{t("Refresh")}
</Button>
@@ -145,11 +175,11 @@ export function NoiseProfileCard() {
))}
</TableBody>
</Table>
) : (
) : reading.state === "ready" ? (
<p className="text-muted-foreground text-sm">
{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.")}
</p>
)}
) : null}
</CardContent>
</Card>
);
+4
View File
@@ -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",
+4
View File
@@ -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": "侧边栏",