Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 68 additions & 37 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -144,6 +152,13 @@ function surfaceOf(section: string | null): Surface {
}
}

/** 有就去掉,没有就加上。展开、收起一个组走它 */
function toggled(s: Set<string>, id: string): Set<string> {
const next = new Set(s);
if (!next.delete(id)) next.add(id);
return next;
}

/** lucide 的图标类型。尺寸走 `size`,颜色走 `currentColor`。 */
type SourceIcon = LucideIcon;
const SOURCES: {
Expand Down Expand Up @@ -406,12 +421,15 @@ export default function App() {
/** 菜单栏里点了「全部提醒…」几次。每点一次,工具栏上的提醒就打开一次 */
const [noticesAsked, setNoticesAsked] = useState(0);
/**
* 键盘选中的那一行。
* 键盘选中的那一行:一条请求,或者归组时的一个组头。
*
* **`-1` 表示还没用过键盘。**一进页面就高亮第一行,会让用户以为
* **`null` 表示还没用过键盘。**一进页面就高亮第一行,会让用户以为
* 那一行有什么特别。
*
* **记的是哪一行,不是第几行。**新请求从顶上进来、换了排序、展开收起一个
* 组,行的位置都会变;记下标的话,高亮会跳到别的行上去。
*/
const [cursor, setCursor] = useState(-1);
const [cursor, setCursor] = useState<Cursor | null>(null);
/** 流量页的滚动层。键盘选中一行之后,在它里面找到那一行滚进视野 */
const listRef = useRef<HTMLDivElement>(null);
const [tab, setTab] = useState<Surface>("dashboard");
Expand Down Expand Up @@ -562,6 +580,10 @@ export default function App() {
*
* **「用鼠标一行行点太慢」**,而 Requests 是主战场。
*
* `↑↓` 按屏幕上的顺序一行一行走,`Enter` 打开。归组时组头也是一行:`Enter`
* 打开那次会话,`→` 展开、`←` 收起;在组里的请求上按 `←` 回到组头 —— 和访达
* 列表视图里的三角一样。
*
* 两个边界:在输入框里打字时不接管方向键(否则光标动不了);
* 抽屉开着时 `↑↓` 也不动,那时用户在看详情而不是挑行。请求和会话的
* 抽屉都算:选中的行会滚进视野,漏掉一个,背后的列表就跟着光标滚走了。
Expand Down Expand Up @@ -617,49 +639,62 @@ 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 只在请求页挂)
useEffect(() => {
if (tab !== "requests") setOpen(null);
}, [tab]);

// 列表变短了(超上限丢老的)时把光标收回来 —— 指着一行不存在的
// 记录,`Enter` 会什么都不发生,而用户不知道为什么
useEffect(() => {
setCursor((c) => (c >= rows.length ? rows.length - 1 : c));
}, [rows.length]);

/**
* 守护状态。**推过来的,不是问出来的。**
*
Expand Down Expand Up @@ -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) => {
// 一次只开一样:请求详情和会话详情共用那一栏
Expand Down
64 changes: 37 additions & 27 deletions src/traffic/RequestTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -59,22 +59,38 @@ export function RequestTable({
onToggleGroup: (id: string) => void;
onOpenSession: (id: string) => void;
showClient: boolean;
/** 键盘选中的那一行在 `rows` 里的下标 */
cursor: number;
/** 键盘选中的那一行:一条请求,或者一个组头 */
cursor: Cursor | null;
/** 刚到的那几行,短暂高亮 */
fresh: Set<number>;
/** 「今天」是哪一天。**按日取整传进来**,否则每次重画都可能跨过零点 */
today: number;
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 (
<Table className="tw-num" scroll={false}>
<Table
/*
表体的每一行都可能被键盘选中、`scrollIntoView` 进视野(见 App 的
键盘导航):请求行,和归组时的组头。

**上边让出吸顶的表头**,不然往上翻时这一行停在表头底下。36px
是表头的高:排序钮 24px,加「状态」那一格的 `py-1.5`。

**左右各放出一整屏宽,横向就不会滚。**窗口窄、表横着滚的时候,
一行横跨整张表,总有一截在视野外,`nearest` 会为它横着滚:
Chromium 把表推开 20px,左右边距没了;WebKit 从最右一下跳回最左。
只放出外面那块的 `px-5` 在 WebKit 里不够 —— 它把滚动宽度向上
取整,滚到最右时行边差零点几像素够不着可视区的右沿。
*/
className="tw-num [&_tbody_tr]:scroll-mt-9 [&_tbody_tr]:scroll-mx-[100vw]"
scroll={false}
>
{/*
**表头必须钉住。**这张表滚两屏之后就没有列名了,而并排的
两列毫秒数,不看列名根本分不出哪个是首字节哪个是总耗时 ——
Expand Down Expand Up @@ -254,6 +270,9 @@ function BodySkeleton({ widths }: { widths: string[] }) {
* 过滤和排序在两个形态下都照常生效:它们作用在进到这里之前的 `rows`
* 上,归组只是把同一批行重新摆一遍。**组与组之间沿用表头的排序,组内
* 永远按时间正序** —— 一次任务的第 1 轮到第 47 轮是有顺序的。
*
* 键盘按 `lines`(grouping.ts)给出的顺序走,那是照着这里的摆法写的:这里
* 改了怎么摆,那边要跟着改。
*/
function RequestRows({
rows,
Expand All @@ -276,28 +295,29 @@ function RequestRows({
openGroups: Set<string>;
showClient: boolean;
today: number;
cursor: number;
cursor: Cursor | null;
fresh: Set<number>;
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) => (
<Row
key={r.id}
r={r}
prev={prev}
showClient={showClient}
today={today}
selected={at === r.id}
selected={atRow === r.id}
fresh={fresh.has(r.id)}
indent={indent}
onOpen={onOpen}
onSelect={() => onCursor(rows.indexOf(r))}
onSelect={() => onCursor({ kind: "request", id: r.id })}
onFilter={onFilter}
/>
);
Expand All @@ -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))}
</Fragment>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/traffic/SessionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export function SessionRow({

return (
<TableRow
// 键盘选中组头时 App 按它找到这一行、滚进视野
data-session={g.id ?? undefined}
onClick={onOpen}
className={
"cursor-pointer border-b border-neutral-100 bg-neutral-50/60 font-medium dark:border-neutral-900 dark:bg-neutral-900/40 " +
Expand Down
Loading
Loading