From 063befa65f0935cd732e3ae1e7e1f84da256f777 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Fri, 25 Sep 2026 13:51:00 +0800 Subject: [PATCH 1/2] security: header counts the whole window from core, broken down by outcome The header said "100+" once the log's first page was full, because it counted the rows the log had loaded. It now shows SecurityEventsPage.total, the number of matches in the log's window across all pages, followed by by_outcome (cut off, refused, replaced, recorded; zero counts hidden) with the same dot colours as the log's Action column. - Loading more pages leaves the numbers alone; new matches count up (AnimatedNumber in the log's scope); switching the range jumps to the new values. - While the log loads, a skeleton holds the place; if it fails, the log below shows the error and the header leaves the count out. - The count and its breakdown wrap together on narrow windows. - English says "1 match" in the singular. Co-Authored-By: Claude Opus 5.5 --- src/security/SecurityPage.i18n.tsx | 24 +++++++++--- src/security/SecurityPage.tsx | 59 +++++++++++++++++++----------- src/security/labels.test.ts | 10 ++++- src/security/labels.tsx | 6 +++ src/security/useSecurityLog.ts | 5 ++- 5 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/security/SecurityPage.i18n.tsx b/src/security/SecurityPage.i18n.tsx index 367b15d5..3652309e 100644 --- a/src/security/SecurityPage.i18n.tsx +++ b/src/security/SecurityPage.i18n.tsx @@ -1,5 +1,6 @@ import type { ReactNode } from "react"; import { messages } from "@/i18n"; +import type { SecurityOutcome } from "@/types"; /** 英文句中的「Since 9/8」要小写开头 */ const lower = (s: string) => s.charAt(0).toLowerCase() + s.slice(1); @@ -16,14 +17,21 @@ export const securityPageText = messages( off: "项关闭", }, /** - * 页头上日志那段时间里命中了几次。`n` 是画好的数字;读到的是前若干条时 - * 带「+」。自定义区间的名字本身是「9/8 至今」,不再加「内」 + * 页头上日志那段时间里一共命中了几次。`count` 是条数(英文按它分单复数), + * `n` 是画好的数字。自定义区间的名字本身是「9/8 至今」,不再加「内」 */ - hits: (n: ReactNode, range: string, custom: boolean) => ( + hits: (_count: number, n: ReactNode, range: string, custom: boolean) => ( <> {custom ? range : `${range}内`} {n} 次命中 ), + /** 页头上各做了什么:「1 已切断」。和日志「处置」一栏同一组词 */ + outcomes: { + cut: "已切断", + blocked: "已拒绝", + replaced: "已替换", + recorded: "仅记录", + } satisfies Record, /** 标签上的名字。和源列表、规则表里的名字一致 */ tabs: { redact: "出站脱敏", @@ -47,11 +55,17 @@ export const securityPageText = messages( observe: "observing", off: "off", }, - hits: (n: ReactNode, range: string, custom: boolean) => ( + hits: (count: number, n: ReactNode, range: string, custom: boolean) => ( <> - {n} {custom ? `matches ${lower(range)}` : `matches in the last ${range}`} + {n} {count === 1 ? "match" : "matches"} {custom ? lower(range) : `in the last ${range}`} ), + outcomes: { + cut: "cut off", + blocked: "refused", + replaced: "replaced", + recorded: "recorded", + }, // 英文名长,标签用短名;全名在每一项的档位那一块上 tabs: { redact: "Redaction", diff --git a/src/security/SecurityPage.tsx b/src/security/SecurityPage.tsx index bb1f11da..777ba627 100644 --- a/src/security/SecurityPage.tsx +++ b/src/security/SecurityPage.tsx @@ -20,7 +20,7 @@ import { } from "@/types"; import { api, hasAction, hasCustom, type CustomGuard, type RuleSave } from "./api"; import { GuardSkeleton, GuardTab, type RuleActions } from "./GuardTab"; -import { modeTone, viewName } from "./labels"; +import { modeTone, OUTCOMES, outcomeTone, viewName } from "./labels"; import { securityLabelsText } from "./labels.i18n"; import { LogTab, type LogActions } from "./LogTab"; import { OutputLimitTab } from "./OutputLimitTab"; @@ -70,8 +70,8 @@ const ruleKey = (guard: RuleGuard, r: SecurityRuleView) => `${guard}/${r.custom * MCP 页,不在这里:这一页只管经过网关的请求。 * * 页头一行是全貌:几项在拦截、几项在观察、几项关着(状态点和标签上的同色), - * 以及日志那段时间里命中了几次。日志在第一个标签:开着一项防护却不知道它查到 - * 了什么,等于没开。 + * 以及日志那段时间里一共命中了几次、各做了什么。日志在第一个标签:开着一项防护 + * 却不知道它查到了什么,等于没开。 * * **改动先画出来再写**:启停规则、换档都是先改界面,写完给一个带「撤销」的 * 提示;几处连着改时一个接一个写(每一次写都要带上一次写完的版本号)。 @@ -93,7 +93,7 @@ export default function SecurityPage({ const lt = useText(securityLabelsText); const [tab, setTab] = useState("log"); const detail = useResource("security-detail", () => api.detail(), { deps: [configVersion] }); - // 日志的区间放在这一层:页头的命中次数和日志是同一份 + // 日志的区间放在这一层:页头的命中次数和日志是同一次读取 const [range, setRange] = useRange("tw-security-range", "1d"); const log = useSecurityLog(range, tick); const [dialog, setDialog] = useState(null); @@ -352,8 +352,12 @@ export default function SecurityPage({ } /** - * 页头的一行:几项防护各在哪一档(和标签上的点同色),日志那段时间里命中了 - * 几次。命中次数就是日志读到的条数;读到的是前若干条时写「100+」,不估。 + * 页头的一行:几项防护各在哪一档(和标签上的点同色);日志那段时间里一共命中了 + * 几次、各做了什么(和日志「处置」一栏同色)。 + * + * **条数是 core 按整段时间数的**(`total`、`by_outcome`),不是日志读到了几条: + * 日志往下翻多少页,这几个数都不变;来了新记录、日志重读时跟着走。换了区间 + * 直接落到新值,不从旧区间的数滚过去(`scope`)。 */ function Summary({ detail, @@ -371,6 +375,31 @@ function Summary({ if (!detail && loading) return ; const counts: Record = { enforce: 0, observe: 0, off: 0 }; if (detail) for (const g of GUARDS) counts[detail[g].mode] += 1; + // 日志还在读(开页、换了没读过的区间):先占住数字的位置;读不到时日志那里说 + const hits = page ? ( + // 总数和各做了什么是一句话:窗口窄、一行放不下时整句换到下一行,不从中间断开 + + {/* 不能是 flex:flex 会吞掉数字两边的空格 */} + + {t.hits( + page.total, + , + range.label, + range.custom === true, + )} + + {OUTCOMES.filter((o) => page.by_outcome[o] > 0).map((o) => ( + } + value={} + label={t.outcomes[o]} + /> + ))} + + ) : log.r.loading ? ( + + ) : null; return ( <> {detail && @@ -379,22 +408,8 @@ function Summary({ .map((m) => ( } value={counts[m]} label={t.modes[m]} /> ))} - {detail && page && } - {page && ( - // 不能是 flex:flex 会吞掉数字两边的空格 - - {t.hits( - `${Math.round(n)}${page.more ? "+" : ""}`} - className="font-medium text-foreground" - />, - range.label, - range.custom === true, - )} - - )} + {detail && hits && } + {hits} ); } diff --git a/src/security/labels.test.ts b/src/security/labels.test.ts index 5c1636f9..6aaf98bf 100644 --- a/src/security/labels.test.ts +++ b/src/security/labels.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; import { setLang } from "@/i18n"; -import { clock, dayHead, dayKey, modeTone, outcomeTone } from "./labels"; +import type { SecurityOutcomeCounts } from "@/types"; +import { clock, dayHead, dayKey, modeTone, OUTCOMES, outcomeTone } from "./labels"; /** 2026-09-25(周五)16:42:07,本地时区 */ const NOW = new Date(2026, 8, 25, 16, 42, 7).getTime(); @@ -61,4 +62,11 @@ describe("状态的颜色", () => { expect(outcomeTone("replaced")).toBe("ok"); expect(outcomeTone("recorded")).toBe("warn"); }); + + /** 页头按这个先后列各做法的条数:四种各一次,红的在前 */ + it("处置的先后", () => { + const all: SecurityOutcomeCounts = { recorded: 0, replaced: 0, cut: 0, blocked: 0 }; + expect([...OUTCOMES].sort()).toEqual(Object.keys(all).sort()); + expect(OUTCOMES.map(outcomeTone)).toEqual(["error", "error", "ok", "warn"]); + }); }); diff --git a/src/security/labels.tsx b/src/security/labels.tsx index 6b32aaf5..6e9d86ce 100644 --- a/src/security/labels.tsx +++ b/src/security/labels.tsx @@ -26,6 +26,12 @@ export function outcomeTone(action: SecurityOutcome): StatusTone { return action === "cut" || action === "blocked" ? "error" : action === "replaced" ? "ok" : "warn"; } +/** + * 几种处置一起列时的先后(页头):先说改变了请求结局的切断、拒绝,再说替换、 + * 仅记录。和规则「拦截时」的选项同一个先后。 + */ +export const OUTCOMES: readonly SecurityOutcome[] = ["cut", "blocked", "replaced", "recorded"]; + /** * 一条规则叫什么。 * diff --git a/src/security/useSecurityLog.ts b/src/security/useSecurityLog.ts index 2275af8b..47e7a0c6 100644 --- a/src/security/useSecurityLog.ts +++ b/src/security/useSecurityLog.ts @@ -21,8 +21,9 @@ export interface SecurityLog { /** * 安全日志:一段时间里的命中,按时间倒序,一页一页往下翻。 * - * **页头和日志用同一份。**页头的「N 次命中」就是日志里读出来的条数,两处 - * 不会对不上;换页再回来先画缓存(`useResource`),后台再读。 + * **页头和日志用同一次读取。**每一页都带着整段时间的总数和各做法的条数 + * (`total`、`by_outcome`),页头说的是这两样;日志列的是其中最新的若干条, + * 往下翻才读更早的。换页再回来先画缓存(`useResource`),后台再读。 * * **换了区间从第一页读起;只是来了新记录,就把已经翻出来的几页一起重读。** * 用户往下翻了三页正看着,一条新命中进来就把列表弹回第一页,等于把他翻过的 From 93a1ea9a94a008a35b6770b9d2ae995fe4e714f6 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Fri, 25 Sep 2026 13:51:00 +0800 Subject: [PATCH 2/2] security: one heading per day while dropped rows fade out When a reload brings back a whole page of newer entries, the rows that dropped off stay in place while they fade out and the new rows are listed after them, so the same day was split in two, with duplicate React keys on the day headings. Group the rows by day instead of by adjacency. Co-Authored-By: Claude Opus 5.5 --- src/security/LogTab.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/security/LogTab.tsx b/src/security/LogTab.tsx index 5836db84..99b9cbb4 100644 --- a/src/security/LogTab.tsx +++ b/src/security/LogTab.tsx @@ -142,13 +142,22 @@ function LogTable({ const lt = useText(securityLabelsText); const shown = usePresentList(events, (e) => e.id); - // 按天分组。列表本来就是倒序,同一天的挨在一起 + /* + 按天分组。列表本来就是倒序,同一天的挨在一起 —— 除了淡出的那一小会儿: + 一下子来了一整页新记录时,读回来的全是新的,旧的几行留在原处淡出,新行 + 排在它们后面,同一天会被隔成两段。按天归拢,一天只有一个标题。 + */ const days: { key: string; at: number; rows: typeof shown }[] = []; + const byKey = new Map(); for (const s of shown) { const key = dayKey(s.item.at_ms); - const last = days[days.length - 1]; - if (last && last.key === key) last.rows.push(s); - else days.push({ key, at: s.item.at_ms, rows: [s] }); + const day = byKey.get(key); + if (day) day.rows.push(s); + else { + const next = { key, at: s.item.at_ms, rows: [s] }; + byKey.set(key, next); + days.push(next); + } } /**