From 51adf517c1713dd30857314ca9d3567158e05795 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Tue, 22 Sep 2026 22:28:01 +0800 Subject: [PATCH] fix(traffic): arrow keys follow the rows on screen when grouped by session The keyboard cursor was an index into `rows`, the filtered and sorted list. In the grouped view that is not what is on screen: sessions are ordered by their newest request, a session lists its requests oldest first, and requests in collapsed sessions are not rendered. So Down moved the highlight up within a session, jumped between sessions and landed on rows that were not in the table; with the sessions collapsed, which is how the grouped view opens, it highlighted nothing at all. - The cursor remembers which row it is on (a request id, or a session header) instead of a position. New requests arriving at the top, another sort, or a session expanding above no longer move the highlight to a different request. - Up/Down walk the rows in the order they are drawn (`lines` in grouping.ts, next to the grouping it mirrors), skipping requests in collapsed sessions. A cursor left inside a session that was then collapsed continues from where that row was. - Session headers are rows too: Enter opens the session, Right expands it, Left collapses it, and Left on a request inside a session moves to its header, as in a Finder list view. Clicking a header puts the cursor there, as clicking a request already did. - Header rows get the same scroll margins as request rows, so they stop below the pinned header and never scroll the table sideways; the margins now sit on the table for every body row. - The effect that pulled an index back when the list got shorter is gone: a request that leaves the list simply loses the highlight, and the next key press starts from the first row. Co-Authored-By: Claude Opus 5 --- src/App.tsx | 105 +++++++++++++++++++++++------------ src/traffic/RequestTable.tsx | 64 ++++++++++++--------- src/traffic/SessionRow.tsx | 2 + src/traffic/grouping.test.ts | 84 +++++++++++++++++++++++++++- src/traffic/grouping.ts | 77 +++++++++++++++++++++++++ 5 files changed, 267 insertions(+), 65 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index ec93dc5a..3e428156 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -39,7 +39,15 @@ import { RequestTable } from "./traffic/RequestTable"; import { SessionPanel } from "./traffic/SessionPanel"; import { useSessions } from "./traffic/useSessions"; import { useCoreEvent } from "./useCoreEvent"; -import { groupAt, groupBySession } from "./traffic/grouping"; +import { + groupAt, + groupBySession, + isAt, + lines, + step, + visible, + type Cursor, +} from "./traffic/grouping"; import Dashboard from "./Dashboard"; import RequestDrawer from "./RequestDrawer"; import type { CoreStatus, Overview, SessionDetail } from "./types"; @@ -144,6 +152,13 @@ function surfaceOf(section: string | null): Surface { } } +/** 有就去掉,没有就加上。展开、收起一个组走它 */ +function toggled(s: Set, id: string): Set { + const next = new Set(s); + if (!next.delete(id)) next.add(id); + return next; +} + /** lucide 的图标类型。尺寸走 `size`,颜色走 `currentColor`。 */ type SourceIcon = LucideIcon; const SOURCES: { @@ -406,12 +421,15 @@ export default function App() { /** 菜单栏里点了「全部提醒…」几次。每点一次,工具栏上的提醒就打开一次 */ const [noticesAsked, setNoticesAsked] = useState(0); /** - * 键盘选中的那一行。 + * 键盘选中的那一行:一条请求,或者归组时的一个组头。 * - * **`-1` 表示还没用过键盘。**一进页面就高亮第一行,会让用户以为 + * **`null` 表示还没用过键盘。**一进页面就高亮第一行,会让用户以为 * 那一行有什么特别。 + * + * **记的是哪一行,不是第几行。**新请求从顶上进来、换了排序、展开收起一个 + * 组,行的位置都会变;记下标的话,高亮会跳到别的行上去。 */ - const [cursor, setCursor] = useState(-1); + const [cursor, setCursor] = useState(null); /** 流量页的滚动层。键盘选中一行之后,在它里面找到那一行滚进视野 */ const listRef = useRef(null); const [tab, setTab] = useState("dashboard"); @@ -562,6 +580,10 @@ export default function App() { * * **「用鼠标一行行点太慢」**,而 Requests 是主战场。 * + * `↑↓` 按屏幕上的顺序一行一行走,`Enter` 打开。归组时组头也是一行:`Enter` + * 打开那次会话,`→` 展开、`←` 收起;在组里的请求上按 `←` 回到组头 —— 和访达 + * 列表视图里的三角一样。 + * * 两个边界:在输入框里打字时不接管方向键(否则光标动不了); * 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。请求和会话的 * 抽屉都算:选中的行会滚进视野,漏掉一个,背后的列表就跟着光标滚走了。 @@ -617,36 +639,55 @@ export default function App() { return; } if (open !== null || openSession !== null) return; + /* + **选中的那一行要一直看得见。**方向键自带的滚动上面拦掉了,不补的话 + 按过可视区的下沿,高亮就跑到屏幕外面去了。只滚刚好够的距离 + (`nearest`);让开吸顶的表头、不横着滚,靠的是行上的 scroll-margin, + 见 `RequestTable`。光标到了头、没动,也照样滚:用滚轮翻走之后,按一下 + 方向键就回到选中的那一行。 + */ + const reveal = (c: Cursor) => + listRef.current + ?.querySelector( + c.kind === "request" + ? `[data-row="${c.id}"]` + : `[data-session="${CSS.escape(c.id)}"]`, + ) + ?.scrollIntoView({ block: "nearest" }); + // 表里从上到下的每一行。**按它走,不按 `rows`**:归组之后两者不一样,见 `lines` + const ls = lines(rows, groups, openGroups); + const here = ls.find((l) => isAt(l, cursor)); if (e.key === "ArrowDown" || e.key === "ArrowUp") { e.preventDefault(); - const step = e.key === "ArrowDown" ? 1 : -1; - // 第一次按方向键从第一行开始,而不是从「上一行」跳到末尾 - const next = - cursor < 0 ? 0 : Math.max(0, Math.min(rows.length - 1, cursor + step)); + const next = step(ls, cursor, e.key === "ArrowDown" ? 1 : -1); setCursor(next); - /* - **选中的那一行要一直看得见。**方向键自带的滚动上面拦掉了,不补的话 - 按过可视区的下沿,高亮就跑到屏幕外面去了。只滚刚好够的距离 - (`nearest`);让开吸顶的表头、不横着滚,靠的是行上的 scroll-margin, - 见 `Row`。光标到了头、没动,也照样滚:用滚轮翻走之后,按一下方向键 - 就回到选中的那一行。 - - **按 id 找行,不按下标。**归组时表里的顺序不是 `rows` 的顺序,折起来 - 的组里的行也不在表里 —— 那时找不到,就不滚。 - */ - const id = rows[next]?.id; - if (id !== undefined) - listRef.current - ?.querySelector(`[data-row="${id}"]`) - ?.scrollIntoView({ block: "nearest" }); - } else if (e.key === "Enter" && cursor >= 0 && rows[cursor]) { + if (next) reveal(next); + } else if ( + here?.kind === "session" && + ((e.key === "ArrowRight" && !here.open) || (e.key === "ArrowLeft" && here.open)) + ) { + e.preventDefault(); + const id = here.id; + setOpenGroups((prev) => toggled(prev, id)); + } else if ( + e.key === "ArrowLeft" && + here?.kind === "request" && + here.shown && + here.under !== null + ) { e.preventDefault(); - setOpen(rows[cursor].id); + const head: Cursor = { kind: "session", id: here.under }; + setCursor(head); + reveal(head); + } else if (e.key === "Enter" && here && visible(here)) { + e.preventDefault(); + if (here.kind === "request") setOpen(here.id); + else setOpenSession(here.id); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); - }, [tab, rows, cursor, open, openSession]); + }, [tab, rows, groups, openGroups, cursor, open, openSession]); // 换页时把抽屉关掉 —— 它是请求页的东西,留在别的页上没有意义, // 而那时 Esc 也不接管了(键盘那个 effect 只在请求页挂) @@ -654,12 +695,6 @@ export default function App() { if (tab !== "requests") setOpen(null); }, [tab]); - // 列表变短了(超上限丢老的)时把光标收回来 —— 指着一行不存在的 - // 记录,`Enter` 会什么都不发生,而用户不知道为什么 - useEffect(() => { - setCursor((c) => (c >= rows.length ? rows.length - 1 : c)); - }, [rows.length]); - /** * 守护状态。**推过来的,不是问出来的。** * @@ -1544,11 +1579,7 @@ export default function App() { openGroups={openGroups} selectedSession={openSession} onToggleGroup={(id) => - setOpenGroups((prev) => { - const next = new Set(prev); - if (!next.delete(id)) next.add(id); - return next; - }) + setOpenGroups((prev) => toggled(prev, id)) } onOpenSession={(id) => { // 一次只开一样:请求详情和会话详情共用那一栏 diff --git a/src/traffic/RequestTable.tsx b/src/traffic/RequestTable.tsx index 507d6f2a..fd578c8e 100644 --- a/src/traffic/RequestTable.tsx +++ b/src/traffic/RequestTable.tsx @@ -21,7 +21,7 @@ import { } from "@/ui/table"; import type { RequestRow } from "@/types"; import type { Filter, SortDir, SortKey } from "@/requestTable"; -import { sortWithin, type Group } from "./grouping"; +import { sortWithin, type Cursor, type Group } from "./grouping"; import { SessionRow } from "./SessionRow"; /** @@ -59,8 +59,8 @@ export function RequestTable({ onToggleGroup: (id: string) => void; onOpenSession: (id: string) => void; showClient: boolean; - /** 键盘选中的那一行在 `rows` 里的下标 */ - cursor: number; + /** 键盘选中的那一行:一条请求,或者一个组头 */ + cursor: Cursor | null; /** 刚到的那几行,短暂高亮 */ fresh: Set; /** 「今天」是哪一天。**按日取整传进来**,否则每次重画都可能跨过零点 */ @@ -68,13 +68,29 @@ export function RequestTable({ sortKey: SortKey; sortDir: SortDir; onSort: (k: SortKey) => void; - onCursor: (i: number) => void; + onCursor: (c: Cursor) => void; onOpen: (id: number) => void; onFilter: (f: (prev: Filter) => Filter) => void; }) { const t = useText(appText); return ( - +
{/* **表头必须钉住。**这张表滚两屏之后就没有列名了,而并排的 两列毫秒数,不看列名根本分不出哪个是首字节哪个是总耗时 —— @@ -254,6 +270,9 @@ function BodySkeleton({ widths }: { widths: string[] }) { * 过滤和排序在两个形态下都照常生效:它们作用在进到这里之前的 `rows` * 上,归组只是把同一批行重新摆一遍。**组与组之间沿用表头的排序,组内 * 永远按时间正序** —— 一次任务的第 1 轮到第 47 轮是有顺序的。 + * + * 键盘按 `lines`(grouping.ts)给出的顺序走,那是照着这里的摆法写的:这里 + * 改了怎么摆,那边要跟着改。 */ function RequestRows({ rows, @@ -276,16 +295,17 @@ function RequestRows({ openGroups: Set; showClient: boolean; today: number; - cursor: number; + cursor: Cursor | null; fresh: Set; selectedSession: string | null; onOpen: (id: number) => void; - onCursor: (i: number) => void; + onCursor: (c: Cursor) => void; onFilter: (f: (prev: Filter) => Filter) => void; onToggleGroup: (id: string) => void; onOpenSession: (id: string) => void; }) { - const at = rows[cursor]?.id; + const atRow = cursor?.kind === "request" ? cursor.id : null; + const atHead = cursor?.kind === "session" ? cursor.id : null; const one = (r: RequestRow, prev: RequestRow | undefined, indent: boolean) => ( onCursor(rows.indexOf(r))} + onSelect={() => onCursor({ kind: "request", id: r.id })} onFilter={onFilter} /> ); @@ -322,9 +342,14 @@ function RequestRows({ g={g} open={open} showClient={showClient} - selected={selectedSession === g.id} + // 右边开着的那次会话,或者键盘停在这个组头上 + selected={selectedSession === g.id || atHead === g.id} onToggle={() => onToggleGroup(g.id!)} - onOpen={() => onOpenSession(g.id!)} + onOpen={() => { + // 点组头和点请求行一样,键盘接着从这一行往下走 + onCursor({ kind: "session", id: g.id! }); + onOpenSession(g.id!); + }} /> {open && inside.map((r, i) => one(r, inside[i - 1], true))} @@ -445,21 +470,6 @@ function Row({ }} className={ "cursor-pointer border-b border-neutral-100 hover:bg-neutral-50 dark:border-neutral-900 dark:hover:bg-neutral-900 " + - /* - 这两样是给键盘导航的 `scrollIntoView` 用的(见 App)。 - - **上边让出吸顶的表头**,不然往上翻时这一行停在表头 - 底下。36px 是表头的高:排序钮 24px,加「状态」那一格 - 的 `py-1.5`。 - - **左右各放出一整屏宽,横向就不会滚。**窗口窄、表横着 - 滚的时候,一行横跨整张表,总有一截在视野外,`nearest` - 会为它横着滚:Chromium 把表推开 20px,左右边距没了; - WebKit 从最右一下跳回最左。只放出外面那块的 `px-5` - 在 WebKit 里不够 —— 它把滚动宽度向上取整,滚到最右时 - 行边差零点几像素够不着可视区的右沿。 - */ - "scroll-mt-9 scroll-mx-[100vw] " + (selected ? "bg-neutral-100 dark:bg-neutral-800" : fresh diff --git a/src/traffic/SessionRow.tsx b/src/traffic/SessionRow.tsx index c479bf1c..0fe38024 100644 --- a/src/traffic/SessionRow.tsx +++ b/src/traffic/SessionRow.tsx @@ -57,6 +57,8 @@ export function SessionRow({ return ( ({ @@ -63,3 +71,77 @@ describe("按会话归组", () => { expect(groupBySession([], [])).toEqual([]); }); }); + +describe("键盘在表里怎么走", () => { + // 时间倒序,和表默认的排序一样:a 组三条、b 组一条,还有一条无主的 + const rows = [ + row(5, 500, "a"), + row(4, 400, "b"), + row(3, 300, "a"), + row(2, 200), + row(1, 100, "a"), + ]; + const groups = groupBySession(rows, []); + const req = (id: number): Cursor => ({ kind: "request", id }); + const ses = (id: string): Cursor => ({ kind: "session", id }); + /** 从 `at` 起按 `n` 下,每一下落在哪 */ + const walk = (ls: ReturnType, at: Cursor | null, dir: 1 | -1, n: number) => { + const out: (Cursor | null)[] = []; + for (let i = 0; i < n; i++) out.push((at = step(ls, at, dir))); + return out; + }; + + it("平表就是 rows 的顺序,每一行都看得见", () => { + const ls = lines(rows, undefined, new Set()); + expect(ls.map((l) => l.id)).toEqual([5, 4, 3, 2, 1]); + expect(ls.every((l) => l.kind === "request" && l.shown && l.under === null)).toBe(true); + }); + + it("归组时和表里摆的一样:组头,组内按时间正序,折起来的组里的行不算看得见", () => { + const ls = lines(rows, groups, new Set(["a"])); + expect(ls.map((l) => `${l.kind === "session" ? "组" : ""}${l.id}`)).toEqual([ + "组a", "1", "3", "5", "组b", "4", "2", + ]); + expect(ls.find((l) => l.id === 4)).toMatchObject({ shown: false, under: "b" }); + expect(ls.find((l) => l.id === 2)).toMatchObject({ shown: true, under: null }); + }); + + it("↓ 按屏幕上的顺序走,跳过折起来的组里的行,到了底停在原地", () => { + // 按 rows 走的话是 5 → 4 → 3:在 a 组里往上跳,再跳进折着的 b 组 + const ls = lines(rows, groups, new Set(["a"])); + expect(walk(ls, null, 1, 7)).toEqual([ + ses("a"), req(1), req(3), req(5), ses("b"), req(2), req(2), + ]); + }); + + it("↑ 反过来走,到了顶停在原地", () => { + const ls = lines(rows, groups, new Set(["a"])); + expect(walk(ls, req(2), -1, 6)).toEqual([ + ses("b"), req(5), req(3), req(1), ses("a"), ses("a"), + ]); + }); + + it("还没用过键盘、或者选中的那条已经不在表里,从第一行开始,↑ 也是", () => { + const ls = lines(rows, groups, new Set()); + expect(step(ls, null, 1)).toEqual(ses("a")); + expect(step(ls, null, -1)).toEqual(ses("a")); + expect(step(ls, req(99), 1)).toEqual(ses("a")); + }); + + it("选中的那条被折进了组里:从它的位置接着走", () => { + // 先选中 4,再把 b 折起来 + const ls = lines(rows, groups, new Set()); + expect(step(ls, req(4), 1)).toEqual(req(2)); + expect(step(ls, req(4), -1)).toEqual(ses("b")); + }); + + it("折进最后一组、往下已经没有了,就退回反方向最近的那一行", () => { + const tail = [row(2, 200), row(1, 100, "a")]; + const ls = lines(tail, groupBySession(tail, []), new Set()); + expect(step(ls, req(1), 1)).toEqual(ses("a")); + }); + + it("一行都看不见时没有落点", () => { + expect(step(lines([], [], new Set()), null, 1)).toBeNull(); + }); +}); diff --git a/src/traffic/grouping.ts b/src/traffic/grouping.ts index 577f96c6..7358b370 100644 --- a/src/traffic/grouping.ts +++ b/src/traffic/grouping.ts @@ -81,3 +81,80 @@ export function sortWithin(g: Group): RequestRow[] { export function failedIn(g: Group): number { return g.rows.filter((r) => r.state === "failed").length; } + +/** 键盘选中的那一行:一条请求,或者归组时的一个组头 */ +export type Cursor = { kind: "request"; id: number } | { kind: "session"; id: string }; + +/** + * 表里的一行。`under` 是它挂在哪个组头下面(平表、无主的请求是 `null`); + * `shown` 为假的是折起来的组里的请求 —— 它们不在表里。 + */ +export type Line = + | { kind: "session"; id: string; open: boolean } + | { kind: "request"; id: number; under: string | null; shown: boolean }; + +/** + * 表里从上到下有哪些行,和 `RequestRows` 摆的一样。 + * + * **键盘按这个顺序走,不按 `rows`。**平表两者相同;归组之后不一样:组按最新 + * 的一条排,组内按时间正序,折起来的组里的请求不在表里。按 `rows` 走的话, + * ↓ 在组内是往上跳的,会跳到别的组去,还会停在看不见的行上。 + * + * 折起来的组里的请求也列出来(`shown: false`):光标可能正停在那儿 —— 先选中, + * 再把组折起来 —— 那时要知道它在哪,才能从那儿接着走。 + */ +export function lines( + rows: RequestRow[], + groups: Group[] | undefined, + open: Set, +): Line[] { + if (!groups) + return rows.map((r) => ({ kind: "request", id: r.id, under: null, shown: true })); + const out: Line[] = []; + for (const g of groups) { + if (g.id === null) { + const r = g.rows[0]; + if (r) out.push({ kind: "request", id: r.id, under: null, shown: true }); + continue; + } + const isOpen = open.has(g.id); + out.push({ kind: "session", id: g.id, open: isOpen }); + for (const r of sortWithin(g)) + out.push({ kind: "request", id: r.id, under: g.id, shown: isOpen }); + } + return out; +} + +/** 这一行是不是光标指着的那一行 */ +export const isAt = (l: Line, c: Cursor | null): boolean => + c !== null && l.kind === c.kind && l.id === c.id; + +/** 看得见的行:组头,和不在折起来的组里的请求 */ +export const visible = (l: Line): boolean => l.kind === "session" || l.shown; + +const toCursor = (l: Line): Cursor => + l.kind === "session" ? { kind: "session", id: l.id } : { kind: "request", id: l.id }; + +/** + * 按一下 ↑(`-1`)或 ↓(`1`)之后,光标落在哪一行。 + * + * **只落在看得见的行上。**还没用过键盘、或者选中的那一条已经不在表里了(被筛掉、 + * 超了上限),从第一行开始 —— ↑ 也是,从「上一行」跳到末尾没有意义。到了头再 + * 按,停在原地。 + * + * 选中的那条被折进了组里时,从它所在的位置往那个方向接着找;那个方向上已经没有 + * 了,就退回反方向最近的那一行。按了键,总得看见光标在哪。一行都看不见时是 `null`。 + */ +export function step(ls: Line[], at: Cursor | null, dir: 1 | -1): Cursor | null { + const i = ls.findIndex((l) => isAt(l, at)); + if (i < 0) { + const first = ls.find(visible); + return first ? toCursor(first) : null; + } + for (let j = i + dir; j >= 0 && j < ls.length; j += dir) + if (visible(ls[j]!)) return toCursor(ls[j]!); + if (visible(ls[i]!)) return toCursor(ls[i]!); + for (let j = i - dir; j >= 0 && j < ls.length; j -= dir) + if (visible(ls[j]!)) return toCursor(ls[j]!); + return null; +}