mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-01 04:24:03 +01:00
Keep the proxy app fast with many flows
The app held every flow in memory and redid all of its work for each new one: the sidebar mounted every domain and path node (its tree scrolled inside a container that never bounded it), every tree row looked up its atoms by scanning every atom the family had made, and the layout, detail pane and list rows re-rendered per flow. At 3,000 flows each new one cost about 90 ms of main thread, more than the traffic left time for. The app now holds a window of the newest 2,000 flows, applies new ones in batches, and loads older ones in pages from the database on scrolling to the top, through a new list_flows RPC that pages in SQL. The database keeps every flow. Also adds clear_flows: an RPC, POST /flows/clear on the control API, and a Clear button in the header that Cmd+K opens, confirming first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4abc882e13
commit
1a994f7532
19 files changed
+855
-216
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import { Dropdown, type DropdownItem, IconButton } from "@yaakapp-internal/ui";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { useRpcMutation } from "../hooks/useRpcMutation";
|
||||
import { flowTotalAtom } from "../lib/store";
|
||||
|
||||
/** Deletes every flow, once confirmed in its menu, which its hotkey opens too */
|
||||
export function ClearFlowsButton() {
|
||||
const total = useAtomValue(flowTotalAtom);
|
||||
const clear = useRpcMutation("clear_flows");
|
||||
|
||||
const items: DropdownItem[] = [
|
||||
{
|
||||
type: "content",
|
||||
label: (
|
||||
<div className="px-1.5 py-1 max-w-[17rem]">
|
||||
<div className="text-sm text-text">
|
||||
{total === 1 ? "Clear 1 flow?" : `Clear all ${total} flows?`}
|
||||
</div>
|
||||
<div className="text-xs text-text-subtle mt-0.5">
|
||||
{clear.error != null ? (
|
||||
<span className="text-danger">{clear.error.message}</span>
|
||||
) : (
|
||||
"They are deleted from disk with their bodies, for good."
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{ type: "separator" },
|
||||
{
|
||||
label: "Clear",
|
||||
icon: "trash",
|
||||
color: "danger",
|
||||
onSelect: () => clear.mutate({}),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<Dropdown items={items} hotKeyAction="flows_clear">
|
||||
<IconButton
|
||||
className="pointer-events-auto"
|
||||
icon="trash"
|
||||
title="Clear flows"
|
||||
size="sm"
|
||||
iconColor="secondary"
|
||||
/>
|
||||
</Dropdown>
|
||||
);
|
||||
}
|
||||
@@ -17,21 +17,15 @@ import {
|
||||
Tabs,
|
||||
} from "@yaakapp-internal/ui";
|
||||
import classNames from "classnames";
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
import { useAtomValue } from "jotai";
|
||||
import type { CSSProperties } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
import { selectedFlowIdAtom } from "../lib/atoms";
|
||||
import { selectedFlowAtom } from "../lib/atoms";
|
||||
import { findHeader, mimeEssence } from "../lib/body";
|
||||
import { statusReason } from "../lib/statusReason";
|
||||
import { flowsAtom } from "../lib/store";
|
||||
import { EmptyState, FlowBodyViewer, useFlowBody } from "./FlowBodyViewer";
|
||||
import { FlowHeaders, FlowQuery, queryParams } from "./FlowHeaders";
|
||||
|
||||
const selectedFlowAtom = atom((get) => {
|
||||
const id = get(selectedFlowIdAtom);
|
||||
return id == null ? null : (get(flowsAtom).find((f) => f.id === id) ?? null);
|
||||
});
|
||||
|
||||
const TAB_RESPONSE = "response";
|
||||
const TAB_REQUEST = "request";
|
||||
const TAB_HEADERS = "headers";
|
||||
@@ -60,6 +54,8 @@ export function FlowDetail({ style, sideBySide }: { style?: CSSProperties; sideB
|
||||
<Keys keys={["⌘", "⇧", "P"]} />
|
||||
<span>Stop the proxy</span>
|
||||
<Keys keys={["⌘", "⇧", "S"]} />
|
||||
<span>Clear flows</span>
|
||||
<Keys keys={["⌘", "K"]} />
|
||||
</div>
|
||||
</EmptyState>
|
||||
) : (
|
||||
|
||||
@@ -3,34 +3,48 @@ import { formatSize } from "@yaakapp-internal/lib/formatSize";
|
||||
import type { Flow } from "@yaakapp-internal/proxy-lib";
|
||||
import { formatMillis, HttpMethodTagRaw, HttpStatusTagRaw, Icon } from "@yaakapp-internal/ui";
|
||||
import classNames from "classnames";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import type { CSSProperties, KeyboardEvent } from "react";
|
||||
import { useAtom, useAtomValue, useSetAtom } from "jotai";
|
||||
import type { CSSProperties, KeyboardEvent, WheelEvent } from "react";
|
||||
import { memo, useCallback, useLayoutEffect, useRef, useState } from "react";
|
||||
import { selectedFlowIdAtom } from "../lib/atoms";
|
||||
import { selectedFlowAtom, selectedFlowIdAtom } from "../lib/atoms";
|
||||
import { shortContentType } from "../lib/body";
|
||||
import { compareFlows } from "../lib/flowWindow";
|
||||
import { countNewFlows } from "../lib/follow";
|
||||
import { flowsAtom } from "../lib/store";
|
||||
import { followAtom, loadOlderFlowsAtom, newestStartedAtAtom, oldestFlowAtom } from "../lib/store";
|
||||
import { filteredExchangesAtom } from "./Sidebar";
|
||||
|
||||
const ROW_HEIGHT = 24;
|
||||
const BOTTOM_PADDING = 6;
|
||||
/** Rows from the top at which older flows start loading */
|
||||
const LOAD_OLDER_ROWS = 20;
|
||||
|
||||
interface Props {
|
||||
flows: Flow[];
|
||||
sideBySide: boolean;
|
||||
style?: CSSProperties;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/** Follows new traffic until a flow is selected or the list is scrolled, then holds */
|
||||
export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
const [selectedId, setSelectedId] = useAtom(selectedFlowIdAtom);
|
||||
/**
|
||||
* Follows new traffic until a flow is selected or the list is scrolled, then holds. Scrolling to
|
||||
* the top loads older flows from the database.
|
||||
*/
|
||||
export function FlowList({ sideBySide, style, className }: Props) {
|
||||
const flows = useAtomValue(filteredExchangesAtom);
|
||||
const selectedId = useAtomValue(selectedFlowIdAtom);
|
||||
const setSelectedFlow = useSetAtom(selectedFlowAtom);
|
||||
const loadOlder = useSetAtom(loadOlderFlowsAtom);
|
||||
const oldestFlow = useAtomValue(oldestFlowAtom);
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const ignoreScroll = useRef(false);
|
||||
const [following, setFollowing] = useState(true);
|
||||
const [following, setFollowing] = useAtom(followAtom);
|
||||
const [heldAt, setHeldAt] = useState<string | null>(null);
|
||||
// The newest flow across all traffic, not just this list, so a filter change keeps the hold
|
||||
const allFlows = useAtomValue(flowsAtom);
|
||||
const newestStartedAt = allFlows[allFlows.length - 1]?.startedAt ?? null;
|
||||
const newestStartedAt = useAtomValue(newestStartedAtAtom);
|
||||
// Read by the callbacks below, which stay the same across new traffic so rows don't re-render
|
||||
const latest = useRef({ flows, following, newestStartedAt });
|
||||
useLayoutEffect(() => {
|
||||
latest.current = { flows, following, newestStartedAt };
|
||||
});
|
||||
|
||||
const virtualizer = useVirtualizer({
|
||||
count: flows.length,
|
||||
@@ -52,20 +66,37 @@ export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
}, []);
|
||||
|
||||
const hold = useCallback(() => {
|
||||
if (!following) return;
|
||||
if (!latest.current.following) return;
|
||||
setFollowing(false);
|
||||
setHeldAt(newestStartedAt);
|
||||
}, [following, newestStartedAt]);
|
||||
setHeldAt(latest.current.newestStartedAt);
|
||||
}, [setFollowing]);
|
||||
|
||||
const follow = useCallback(() => {
|
||||
setFollowing(true);
|
||||
setHeldAt(null);
|
||||
setScrollTop(Number.MAX_SAFE_INTEGER);
|
||||
}, [setScrollTop]);
|
||||
}, [setFollowing, setScrollTop]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (following) setScrollTop(Number.MAX_SAFE_INTEGER);
|
||||
}, [following, flows.length, setScrollTop]);
|
||||
}, [following, flows, setScrollTop]);
|
||||
|
||||
// Older flows that loaded in above push the rows down; scroll by as much, so they stay put.
|
||||
const anchor = useRef<Flow | null>(null);
|
||||
useLayoutEffect(() => {
|
||||
const previous = anchor.current;
|
||||
anchor.current = oldestFlow;
|
||||
if (following || previous == null || oldestFlow == null) return;
|
||||
if (compareFlows(oldestFlow, previous) >= 0) return;
|
||||
const added = flows.findIndex((f) => compareFlows(f, previous) >= 0);
|
||||
const el = scrollRef.current;
|
||||
if (added > 0 && el != null) setScrollTop(el.scrollTop + added * ROW_HEIGHT);
|
||||
}, [oldestFlow, flows, following, setScrollTop]);
|
||||
|
||||
const loadOlderHeld = useCallback(() => {
|
||||
hold();
|
||||
void loadOlder();
|
||||
}, [hold, loadOlder]);
|
||||
|
||||
const handleScroll = useCallback(() => {
|
||||
if (ignoreScroll.current) {
|
||||
@@ -80,14 +111,23 @@ export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
setHeldAt(null);
|
||||
} else {
|
||||
hold();
|
||||
if (el.scrollTop < LOAD_OLDER_ROWS * ROW_HEIGHT) void loadOlder();
|
||||
}
|
||||
}, [hold]);
|
||||
}, [hold, loadOlder, setFollowing]);
|
||||
|
||||
// A list too short to scroll still loads older flows on scrolling up.
|
||||
const handleWheel = useCallback(
|
||||
(e: WheelEvent<HTMLDivElement>) => {
|
||||
if (e.deltaY < 0 && e.currentTarget.scrollTop <= 0) loadOlderHeld();
|
||||
},
|
||||
[loadOlderHeld],
|
||||
);
|
||||
|
||||
const select = useCallback(
|
||||
(index: number) => {
|
||||
const flow = flows[index];
|
||||
const flow = latest.current.flows[index];
|
||||
if (flow == null) return;
|
||||
setSelectedId(flow.id);
|
||||
setSelectedFlow(flow);
|
||||
hold();
|
||||
const el = scrollRef.current;
|
||||
if (el == null) return;
|
||||
@@ -97,7 +137,7 @@ export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
setScrollTop(top + ROW_HEIGHT - el.clientHeight);
|
||||
}
|
||||
},
|
||||
[flows, hold, setSelectedId, setScrollTop],
|
||||
[hold, setSelectedFlow, setScrollTop],
|
||||
);
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
@@ -107,9 +147,10 @@ export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
e.preventDefault();
|
||||
const index = flows.findIndex((f) => f.id === selectedId);
|
||||
if (index === -1) select(e.key === "ArrowDown" ? 0 : flows.length - 1);
|
||||
else if (index === 0 && e.key === "ArrowUp") loadOlderHeld();
|
||||
else select(e.key === "ArrowDown" ? index + 1 : index - 1);
|
||||
},
|
||||
[flows, select, selectedId],
|
||||
[flows, loadOlderHeld, select, selectedId],
|
||||
);
|
||||
|
||||
const newCount = following ? 0 : countNewFlows(flows, heldAt);
|
||||
@@ -146,6 +187,7 @@ export function FlowList({ flows, sideBySide, style, className }: Props) {
|
||||
aria-label="Flows"
|
||||
tabIndex={0}
|
||||
onScroll={handleScroll}
|
||||
onWheel={handleWheel}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="group/flows h-full overflow-y-auto px-1.5 outline-hidden"
|
||||
>
|
||||
|
||||
@@ -5,14 +5,14 @@ import { useState } from "react";
|
||||
import { useLocalStorage } from "react-use";
|
||||
import { flowLayoutAtom } from "../lib/atoms";
|
||||
import { getOsType } from "../lib/tauri";
|
||||
import { ClearFlowsButton } from "./ClearFlowsButton";
|
||||
import { FlowDetail } from "./FlowDetail";
|
||||
import { FlowList } from "./FlowList";
|
||||
import { ProxyStatus } from "./ProxyStatus";
|
||||
import { filteredExchangesAtom, selectedSidebarLabelAtom, Sidebar } from "./Sidebar";
|
||||
import { filteredCountAtom, selectedSidebarLabelAtom, Sidebar } from "./Sidebar";
|
||||
|
||||
export function ProxyLayout() {
|
||||
const os = getOsType();
|
||||
const exchanges = useAtomValue(filteredExchangesAtom);
|
||||
const title = useAtomValue(selectedSidebarLabelAtom);
|
||||
const [flowLayout, setFlowLayout] = useAtom(flowLayoutAtom);
|
||||
const [sidebarWidth, setSidebarWidth] = useLocalStorage("sidebar_width", 250);
|
||||
@@ -61,12 +61,11 @@ export function ProxyLayout() {
|
||||
className="pointer-events-none flex items-center justify-center gap-2 text-sm px-2 min-w-0"
|
||||
>
|
||||
<span className="truncate">{title}</span>
|
||||
<span className="text-text-subtlest shrink-0">
|
||||
{exchanges.length === 1 ? "1 flow" : `${exchanges.length} flows`}
|
||||
</span>
|
||||
<FlowCount />
|
||||
</div>
|
||||
<div className="flex items-center gap-1 pr-1">
|
||||
<ProxyStatus />
|
||||
<ClearFlowsButton />
|
||||
<IconButton
|
||||
className="pointer-events-auto"
|
||||
icon={flowLayout === "horizontal" ? "columns_2" : "rows_2"}
|
||||
@@ -127,12 +126,7 @@ export function ProxyLayout() {
|
||||
layout={flowLayout}
|
||||
defaultRatio={0.55}
|
||||
firstSlot={({ style, orientation }) => (
|
||||
<FlowList
|
||||
flows={exchanges}
|
||||
sideBySide={orientation === "horizontal"}
|
||||
style={style}
|
||||
className="pt-1"
|
||||
/>
|
||||
<FlowList sideBySide={orientation === "horizontal"} style={style} className="pt-1" />
|
||||
)}
|
||||
secondSlot={({ style, orientation }) => (
|
||||
<FlowDetail style={style} sideBySide={orientation === "horizontal"} />
|
||||
@@ -142,3 +136,11 @@ export function ProxyLayout() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Its own component, so new traffic re-renders the count rather than the whole layout */
|
||||
function FlowCount() {
|
||||
const count = useAtomValue(filteredCountAtom);
|
||||
return (
|
||||
<span className="text-text-subtlest shrink-0">{count === 1 ? "1 flow" : `${count} flows`}</span>
|
||||
);
|
||||
}
|
||||
@@ -4,13 +4,14 @@ import { selectedIdsFamily, Tree } from "@yaakapp-internal/ui";
|
||||
import { atom, useAtomValue } from "jotai";
|
||||
import { atomFamily } from "jotai-family";
|
||||
import { useCallback } from "react";
|
||||
import { flowsAtom } from "../lib/store";
|
||||
import { flowsAtom, flowTotalAtom } from "../lib/store";
|
||||
|
||||
/** A node in the sidebar tree — either a domain or a path segment. */
|
||||
export type SidebarItem = {
|
||||
id: string;
|
||||
label: string;
|
||||
exchangeIds: string[];
|
||||
/** Flows held under this node; for the Domains folder, every flow in the database */
|
||||
count: number;
|
||||
};
|
||||
|
||||
const collapsedAtom = atomFamily((_treeId: string) => atom<Record<string, boolean>>({}));
|
||||
@@ -19,37 +20,32 @@ export const SIDEBAR_TREE_ID = "proxy-sidebar";
|
||||
|
||||
const sidebarTreeAtom = atom<TreeNode<SidebarItem>>((get) => {
|
||||
const exchanges = get(flowsAtom);
|
||||
return buildTree(exchanges);
|
||||
return buildTree(exchanges, get(flowTotalAtom));
|
||||
});
|
||||
|
||||
/** The sidebar nodes selected, or null when the list shows all traffic */
|
||||
const selectedNodeIdsAtom = atom((get) => {
|
||||
const selectedIds = get(selectedIdsFamily(SIDEBAR_TREE_ID));
|
||||
// Nothing selected, or a node holding everything → show all
|
||||
if (selectedIds.length === 0 || selectedIds.includes("root") || selectedIds.includes("domains")) {
|
||||
return null;
|
||||
}
|
||||
return new Set(selectedIds);
|
||||
});
|
||||
|
||||
/** Exchanges filtered by the currently selected sidebar node(s). */
|
||||
export const filteredExchangesAtom = atom((get) => {
|
||||
const exchanges = get(flowsAtom);
|
||||
const tree = get(sidebarTreeAtom);
|
||||
const selectedIds = get(selectedIdsFamily(SIDEBAR_TREE_ID));
|
||||
|
||||
// Nothing selected or root selected → show all
|
||||
if (selectedIds.length === 0 || selectedIds.includes("root")) {
|
||||
return exchanges;
|
||||
}
|
||||
|
||||
// Collect exchange IDs from all selected nodes
|
||||
const allowedIds = new Set<string>();
|
||||
const nodeMap = new Map<string, SidebarItem>();
|
||||
collectNodes(tree, nodeMap);
|
||||
|
||||
for (const selectedId of selectedIds) {
|
||||
const node = nodeMap.get(selectedId);
|
||||
if (node) {
|
||||
for (const id of node.exchangeIds) {
|
||||
allowedIds.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return exchanges.filter((ex) => allowedIds.has(ex.id));
|
||||
const selected = get(selectedNodeIdsAtom);
|
||||
if (selected == null) return exchanges;
|
||||
return exchanges.filter((ex) => nodeIdsOf(ex).some((id) => selected.has(id)));
|
||||
});
|
||||
|
||||
/** Flows in the database for all traffic, or those held under the selected nodes */
|
||||
export const filteredCountAtom = atom((get) =>
|
||||
get(selectedNodeIdsAtom) == null ? get(flowTotalAtom) : get(filteredExchangesAtom).length,
|
||||
);
|
||||
|
||||
/** The selected sidebar node's name, for the header */
|
||||
export const selectedSidebarLabelAtom = atom((get) => {
|
||||
const selectedId = get(selectedIdsFamily(SIDEBAR_TREE_ID))[0];
|
||||
@@ -58,13 +54,37 @@ export const selectedSidebarLabelAtom = atom((get) => {
|
||||
return "All traffic";
|
||||
});
|
||||
|
||||
function collectNodes(node: TreeNode<SidebarItem>, map: Map<string, SidebarItem>) {
|
||||
map.set(node.item.id, node.item);
|
||||
for (const child of node.children ?? []) {
|
||||
collectNodes(child, map);
|
||||
const nodeIdsByFlow = new WeakMap<Flow, string[]>();
|
||||
|
||||
/**
|
||||
* The ids of the nodes a flow sits under, its domain first and then each path segment's, parsed
|
||||
* once per flow rather than on every change to the list.
|
||||
*/
|
||||
function nodeIdsOf(ex: Flow): string[] {
|
||||
let ids = nodeIdsByFlow.get(ex);
|
||||
if (ids != null) return ids;
|
||||
let hostname: string;
|
||||
let segments: string[];
|
||||
try {
|
||||
const url = new URL(ex.url);
|
||||
hostname = url.host;
|
||||
segments = url.pathname.split("/").filter(Boolean);
|
||||
} catch {
|
||||
hostname = ex.url;
|
||||
segments = [];
|
||||
}
|
||||
ids = [`domain:${hostname}`];
|
||||
let path = `path:${hostname}`;
|
||||
for (const seg of segments) {
|
||||
path += `/${seg}`;
|
||||
ids.push(path);
|
||||
}
|
||||
nodeIdsByFlow.set(ex, ids);
|
||||
return ids;
|
||||
}
|
||||
|
||||
const collator = new Intl.Collator();
|
||||
|
||||
/**
|
||||
* Build a domain → path-segment trie from a flat list of exchanges.
|
||||
*
|
||||
@@ -80,8 +100,8 @@ function collectNodes(node: TreeNode<SidebarItem>, map: Map<string, SidebarItem>
|
||||
* /123
|
||||
* /orders
|
||||
*/
|
||||
function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
const root: SidebarItem = { id: "root", label: "All Traffic", exchangeIds: [] };
|
||||
function buildTree(exchanges: Flow[], total: number): TreeNode<SidebarItem> {
|
||||
const root: SidebarItem = { id: "root", label: "All Traffic", count: 0 };
|
||||
const rootNode: TreeNode<SidebarItem> = {
|
||||
item: root,
|
||||
parent: null,
|
||||
@@ -94,54 +114,26 @@ function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
type TrieNode = {
|
||||
id: string;
|
||||
label: string;
|
||||
exchangeIds: string[];
|
||||
count: number;
|
||||
children: Map<string, TrieNode>;
|
||||
};
|
||||
|
||||
const domainMap = new Map<string, TrieNode>();
|
||||
|
||||
for (const ex of exchanges) {
|
||||
let hostname: string;
|
||||
let segments: string[];
|
||||
try {
|
||||
const url = new URL(ex.url);
|
||||
hostname = url.host;
|
||||
segments = url.pathname.split("/").filter(Boolean);
|
||||
} catch {
|
||||
hostname = ex.url;
|
||||
segments = [];
|
||||
}
|
||||
|
||||
// Get or create domain node
|
||||
let domainNode = domainMap.get(hostname);
|
||||
if (!domainNode) {
|
||||
domainNode = {
|
||||
id: `domain:${hostname}`,
|
||||
label: hostname,
|
||||
exchangeIds: [],
|
||||
children: new Map(),
|
||||
};
|
||||
domainMap.set(hostname, domainNode);
|
||||
}
|
||||
domainNode.exchangeIds.push(ex.id);
|
||||
|
||||
// Walk path segments
|
||||
let current = domainNode;
|
||||
const pathSoFar: string[] = [];
|
||||
for (const seg of segments) {
|
||||
pathSoFar.push(seg);
|
||||
let child = current.children.get(seg);
|
||||
if (!child) {
|
||||
child = {
|
||||
id: `path:${hostname}/${pathSoFar.join("/")}`,
|
||||
label: `/${seg}`,
|
||||
exchangeIds: [],
|
||||
children: new Map(),
|
||||
};
|
||||
current.children.set(seg, child);
|
||||
const ids = nodeIdsOf(ex);
|
||||
let current: TrieNode | undefined;
|
||||
for (const id of ids) {
|
||||
const siblings = current == null ? domainMap : current.children;
|
||||
let node = siblings.get(id);
|
||||
if (node == null) {
|
||||
// The domain, or the path's last segment
|
||||
const label = current == null ? id.slice("domain:".length) : id.slice(id.lastIndexOf("/"));
|
||||
node = { id, label, count: 0, children: new Map() };
|
||||
siblings.set(id, node);
|
||||
}
|
||||
child.exchangeIds.push(ex.id);
|
||||
current = child;
|
||||
node.count++;
|
||||
current = node;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +147,7 @@ function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
item: {
|
||||
id: trie.id,
|
||||
label: trie.label,
|
||||
exchangeIds: trie.exchangeIds,
|
||||
count: trie.count,
|
||||
},
|
||||
parent,
|
||||
depth,
|
||||
@@ -163,7 +155,7 @@ function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
draggable: false,
|
||||
};
|
||||
const sortedChildren = [...trie.children.values()].sort((a, b) =>
|
||||
a.label.localeCompare(b.label),
|
||||
collator.compare(a.label, b.label),
|
||||
);
|
||||
for (const child of sortedChildren) {
|
||||
node.children?.push(toTreeNode(child, node, depth + 1));
|
||||
@@ -172,16 +164,15 @@ function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
}
|
||||
|
||||
// Add a "Domains" folder between root and domain nodes
|
||||
const allExchangeIds = exchanges.map((ex) => ex.id);
|
||||
const domainsFolder: TreeNode<SidebarItem> = {
|
||||
item: { id: "domains", label: "Domains", exchangeIds: allExchangeIds },
|
||||
item: { id: "domains", label: "Domains", count: total },
|
||||
parent: rootNode,
|
||||
depth: 1,
|
||||
children: [],
|
||||
draggable: false,
|
||||
};
|
||||
|
||||
const sortedDomains = [...domainMap.values()].sort((a, b) => a.label.localeCompare(b.label));
|
||||
const sortedDomains = [...domainMap.values()].sort((a, b) => collator.compare(a.label, b.label));
|
||||
for (const domain of sortedDomains) {
|
||||
domainsFolder.children?.push(toTreeNode(domain, domainsFolder, 2));
|
||||
}
|
||||
@@ -192,7 +183,7 @@ function buildTree(exchanges: Flow[]): TreeNode<SidebarItem> {
|
||||
}
|
||||
|
||||
function ItemInner({ item }: { item: SidebarItem }) {
|
||||
const count = item.exchangeIds.length;
|
||||
const count = item.count;
|
||||
return (
|
||||
<div className="flex items-center gap-2 w-full min-w-0">
|
||||
<span className="truncate">{item.label}</span>
|
||||
@@ -205,23 +196,20 @@ export function Sidebar() {
|
||||
const tree = useAtomValue(sidebarTreeAtom);
|
||||
const treeId = SIDEBAR_TREE_ID;
|
||||
|
||||
const getItemKey = useCallback(
|
||||
(item: SidebarItem) => `${item.id}:${item.exchangeIds.length}`,
|
||||
[],
|
||||
);
|
||||
const getItemKey = useCallback((item: SidebarItem) => `${item.id}:${item.count}`, []);
|
||||
|
||||
return (
|
||||
<aside className="x-theme-sidebar bg-surface h-full w-full min-w-0 overflow-y-auto border-r border-border-subtle">
|
||||
<div className="pt-2">
|
||||
<Tree
|
||||
treeId={treeId}
|
||||
collapsedAtom={collapsedAtom(treeId)}
|
||||
className="px-2 pb-10"
|
||||
root={tree}
|
||||
getItemKey={getItemKey}
|
||||
ItemInner={ItemInner}
|
||||
/>
|
||||
</div>
|
||||
// The tree scrolls itself, in a row of bounded height, so its virtualizer mounts only the rows
|
||||
// in view rather than every domain and path.
|
||||
<aside className="x-theme-sidebar bg-surface h-full w-full min-w-0 grid grid-rows-[minmax(0,1fr)] border-r border-border-subtle">
|
||||
<Tree
|
||||
treeId={treeId}
|
||||
collapsedAtom={collapsedAtom(treeId)}
|
||||
className="px-2 pt-2 pb-10"
|
||||
root={tree}
|
||||
getItemKey={getItemKey}
|
||||
ItemInner={ItemInner}
|
||||
/>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
import type { Flow } from "@yaakapp-internal/proxy-lib";
|
||||
import type { SplitLayoutLayout } from "@yaakapp-internal/ui";
|
||||
import { atom } from "jotai";
|
||||
import { atomWithStorage } from "jotai/utils";
|
||||
|
||||
export const selectedFlowIdAtom = atom<string | null>(null);
|
||||
/** The selected flow as last seen, kept once the window has dropped it so the detail stays */
|
||||
export const selectedFlowAtom = atom<Flow | null>(null);
|
||||
|
||||
export const selectedFlowIdAtom = atom((get) => get(selectedFlowAtom)?.id ?? null);
|
||||
|
||||
/** The list follows new traffic, rather than being held where it was scrolled or selected */
|
||||
export const followingAtom = atom(true);
|
||||
|
||||
/** Stacked by default; the header's toggle switches to side by side */
|
||||
export const flowLayoutAtom = atomWithStorage<SplitLayoutLayout>("flow_layout", "vertical");
|
||||
@@ -0,0 +1,131 @@
|
||||
import type { Flow, ModelPayload } from "@yaakapp-internal/proxy-lib";
|
||||
import { describe, expect, test } from "vite-plus/test";
|
||||
import {
|
||||
applyChanges,
|
||||
emptyWindow,
|
||||
type FlowWindow,
|
||||
prependOlder,
|
||||
trimWindow,
|
||||
windowFromPage,
|
||||
} from "./flowWindow";
|
||||
|
||||
/** A flow started at `second` past 10:00, with an id that sorts the same way */
|
||||
function flow(second: number, extra: Partial<Flow> = {}): Flow {
|
||||
const s = String(second).padStart(2, "0");
|
||||
return { id: `fl_${s}`, startedAt: `2026-10-28T10:00:${s}`, ...extra } as Flow;
|
||||
}
|
||||
|
||||
const created = (f: Flow): ModelPayload => ({
|
||||
model: f,
|
||||
change: { type: "upsert", created: true },
|
||||
});
|
||||
const updated = (f: Flow): ModelPayload => ({
|
||||
model: f,
|
||||
change: { type: "upsert", created: false },
|
||||
});
|
||||
const ids = (win: FlowWindow) => win.flows.map((f) => f.id);
|
||||
|
||||
describe("windowFromPage", () => {
|
||||
test("holds the newest page oldest first", () => {
|
||||
const win = windowFromPage([flow(3), flow(2), flow(1)], 3, 10);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_02", "fl_03"]);
|
||||
expect(win).toMatchObject({ hasOlder: true, total: 10 });
|
||||
});
|
||||
|
||||
test("knows there is nothing older when the page came back short", () => {
|
||||
expect(windowFromPage([flow(1)], 3, 1).hasOlder).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyChanges", () => {
|
||||
test("appends new traffic and counts it", () => {
|
||||
const win = applyChanges(windowFromPage([flow(2), flow(1)], 10, 2), [
|
||||
created(flow(3)),
|
||||
created(flow(4)),
|
||||
]);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_02", "fl_03", "fl_04"]);
|
||||
expect(win.total).toBe(4);
|
||||
});
|
||||
|
||||
test("puts a slow flow where it started", () => {
|
||||
const win = applyChanges(windowFromPage([flow(5), flow(1)], 10, 2), [created(flow(3))]);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_03", "fl_05"]);
|
||||
});
|
||||
|
||||
test("orders flows that started together by id", () => {
|
||||
const a = flow(2, { id: "fl_a" });
|
||||
const b = flow(2, { id: "fl_b" });
|
||||
const win = applyChanges(emptyWindow, [created(b), created(a)]);
|
||||
expect(ids(win)).toEqual(["fl_a", "fl_b"]);
|
||||
});
|
||||
|
||||
test("replaces a changed flow in place without counting it again", () => {
|
||||
const saved = flow(2, { savedRequestId: "rq_1" });
|
||||
const win = applyChanges(windowFromPage([flow(3), flow(2), flow(1)], 10, 3), [updated(saved)]);
|
||||
expect(win.flows[1]).toBe(saved);
|
||||
expect(win.total).toBe(3);
|
||||
});
|
||||
|
||||
test("replaces a flow the first page already had, without counting it again", () => {
|
||||
const win = applyChanges(windowFromPage([flow(2), flow(1)], 10, 2), [created(flow(2))]);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_02"]);
|
||||
expect(win.total).toBe(2);
|
||||
});
|
||||
|
||||
test("leaves a flow older than the window to paging, but counts it", () => {
|
||||
const win = applyChanges(windowFromPage([flow(5), flow(4)], 2, 9), [created(flow(1))]);
|
||||
expect(ids(win)).toEqual(["fl_04", "fl_05"]);
|
||||
expect(win.total).toBe(10);
|
||||
});
|
||||
|
||||
test("keeps an older flow when the window already holds everything", () => {
|
||||
const win = applyChanges(windowFromPage([flow(5), flow(4)], 10, 2), [created(flow(1))]);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_04", "fl_05"]);
|
||||
});
|
||||
|
||||
test("returns the same window for no changes", () => {
|
||||
const win = windowFromPage([flow(1)], 10, 1);
|
||||
expect(applyChanges(win, [])).toBe(win);
|
||||
});
|
||||
});
|
||||
|
||||
describe("trimWindow", () => {
|
||||
const many = (n: number) =>
|
||||
windowFromPage(
|
||||
Array.from({ length: n }, (_, i) => flow(n - i)),
|
||||
1000,
|
||||
n,
|
||||
);
|
||||
|
||||
test("leaves the window alone within the slack", () => {
|
||||
const win = many(7);
|
||||
expect(trimWindow(win, 4, 3)).toBe(win);
|
||||
});
|
||||
|
||||
test("drops the oldest back to the size, an even number of them", () => {
|
||||
const even = trimWindow(many(8), 4, 3);
|
||||
expect(ids(even)).toEqual(["fl_05", "fl_06", "fl_07", "fl_08"]);
|
||||
expect(even).toMatchObject({ hasOlder: true, total: 8 });
|
||||
// Five over rounds up to six, so the list's stripes stay on the same flows.
|
||||
expect(ids(trimWindow(many(9), 4, 3))).toEqual(["fl_07", "fl_08", "fl_09"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("prependOlder", () => {
|
||||
test("adds an older page before the oldest flow held", () => {
|
||||
const win = prependOlder(windowFromPage([flow(5), flow(4)], 2, 5), [flow(3), flow(2)], 2);
|
||||
expect(ids(win)).toEqual(["fl_02", "fl_03", "fl_04", "fl_05"]);
|
||||
expect(win.hasOlder).toBe(true);
|
||||
});
|
||||
|
||||
test("knows it reached the start when a page comes back short", () => {
|
||||
const win = prependOlder(windowFromPage([flow(5), flow(4)], 2, 3), [flow(1)], 2);
|
||||
expect(ids(win)).toEqual(["fl_01", "fl_04", "fl_05"]);
|
||||
expect(win.hasOlder).toBe(false);
|
||||
});
|
||||
|
||||
test("skips flows the window already has", () => {
|
||||
const win = prependOlder(windowFromPage([flow(5), flow(4)], 2, 5), [flow(4), flow(3)], 2);
|
||||
expect(ids(win)).toEqual(["fl_03", "fl_04", "fl_05"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
import type { Flow, ModelPayload } from "@yaakapp-internal/proxy-lib";
|
||||
|
||||
/** Flows the window keeps while it follows new traffic */
|
||||
export const WINDOW_SIZE = 2000;
|
||||
/**
|
||||
* Flows past `WINDOW_SIZE` before the oldest are dropped, so they go a few hundred at a time
|
||||
* rather than one per new flow, which would shift every row of the list each time.
|
||||
*/
|
||||
export const WINDOW_SLACK = 500;
|
||||
/** Older flows loaded per page on scrolling to the top */
|
||||
export const PAGE_SIZE = 500;
|
||||
|
||||
/**
|
||||
* The flows the app holds in memory: the newest in the database, oldest first, ordered by start
|
||||
* and then id as `list_flows` orders them. The database keeps every flow; older ones load back
|
||||
* in pages.
|
||||
*/
|
||||
export interface FlowWindow {
|
||||
flows: Flow[];
|
||||
/** The database has flows older than the first one here */
|
||||
hasOlder: boolean;
|
||||
/** Flows in the database */
|
||||
total: number;
|
||||
}
|
||||
|
||||
export const emptyWindow: FlowWindow = { flows: [], hasOlder: false, total: 0 };
|
||||
|
||||
type FlowKey = Pick<Flow, "startedAt" | "id">;
|
||||
|
||||
export function compareFlows(a: FlowKey, b: FlowKey): number {
|
||||
if (a.startedAt !== b.startedAt) return a.startedAt < b.startedAt ? -1 : 1;
|
||||
if (a.id !== b.id) return a.id < b.id ? -1 : 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** The window from the newest page of `list_flows`, which comes newest first */
|
||||
export function windowFromPage(page: Flow[], limit: number, total: number): FlowWindow {
|
||||
return { flows: [...page].reverse(), hasOlder: page.length >= limit, total };
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds new flows where they belong and replaces changed ones. New traffic almost always lands at
|
||||
* the end, so this is an append. A flow that started before the oldest one held, while older ones
|
||||
* are still in the database, is left for paging to load, so the window never has a gap.
|
||||
*/
|
||||
export function applyChanges(win: FlowWindow, changes: ModelPayload[]): FlowWindow {
|
||||
if (changes.length === 0) return win;
|
||||
const flows = win.flows.slice();
|
||||
let total = win.total;
|
||||
for (const { model, change } of changes) {
|
||||
const i = insertionIndex(flows, model);
|
||||
const existing = flows[i]?.id === model.id;
|
||||
if (change.type === "delete") {
|
||||
if (existing) flows.splice(i, 1);
|
||||
total = Math.max(0, total - 1);
|
||||
} else if (existing) {
|
||||
flows[i] = model;
|
||||
} else {
|
||||
if (change.created) total++;
|
||||
const first = flows[0];
|
||||
if (win.hasOlder && first != null && compareFlows(model, first) < 0) continue;
|
||||
flows.splice(i, 0, model);
|
||||
}
|
||||
}
|
||||
return { ...win, flows, total };
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops the oldest flows once there are `slack` more than `size`, back down to `size`. Always an
|
||||
* even number of them, so the list's alternating rows keep their shade.
|
||||
*/
|
||||
export function trimWindow(win: FlowWindow, size = WINDOW_SIZE, slack = WINDOW_SLACK): FlowWindow {
|
||||
if (win.flows.length <= size + slack) return win;
|
||||
let drop = win.flows.length - size;
|
||||
drop += drop % 2;
|
||||
return { ...win, flows: win.flows.slice(drop), hasOlder: true };
|
||||
}
|
||||
|
||||
/** Adds a page of older flows from `list_flows`, newest first, before the oldest one held */
|
||||
export function prependOlder(win: FlowWindow, page: Flow[], limit: number): FlowWindow {
|
||||
const first = win.flows[0];
|
||||
const older = page.filter((f) => first == null || compareFlows(f, first) < 0).reverse();
|
||||
return { ...win, flows: [...older, ...win.flows], hasOlder: page.length >= limit };
|
||||
}
|
||||
|
||||
/** Where `flow` is, or would go, in `flows` */
|
||||
function insertionIndex(flows: FlowKey[], flow: FlowKey): number {
|
||||
// New traffic goes at the end; skip the search for it.
|
||||
const last = flows[flows.length - 1];
|
||||
if (last == null || compareFlows(last, flow) < 0) return flows.length;
|
||||
let lo = 0;
|
||||
let hi = flows.length;
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi) >>> 1;
|
||||
const item = flows[mid];
|
||||
if (item != null && compareFlows(item, flow) < 0) lo = mid + 1;
|
||||
else hi = mid;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
@@ -1,12 +1,120 @@
|
||||
import { createModelStore } from "@yaakapp-internal/model-store";
|
||||
import type { Flow } from "@yaakapp-internal/proxy-lib";
|
||||
import type { ModelPayload } from "@yaakapp-internal/proxy-lib";
|
||||
import { atom, type createStore } from "jotai";
|
||||
import { followingAtom, selectedFlowAtom } from "./atoms";
|
||||
import {
|
||||
applyChanges,
|
||||
emptyWindow,
|
||||
type FlowWindow,
|
||||
PAGE_SIZE,
|
||||
prependOlder,
|
||||
trimWindow,
|
||||
WINDOW_SIZE,
|
||||
windowFromPage,
|
||||
} from "./flowWindow";
|
||||
import { listen, rpc } from "./rpc";
|
||||
|
||||
type ProxyModels = {
|
||||
flow: Flow;
|
||||
};
|
||||
/** How long new flows gather before they reach the list, so a burst renders once */
|
||||
const FLUSH_MS = 100;
|
||||
|
||||
export const { dataAtom, applyChange, replaceAll, listAtom, orderedListAtom } =
|
||||
createModelStore<ProxyModels>(["flow"]);
|
||||
/** The flows in memory: a window onto the database, which keeps them all */
|
||||
export const flowWindowAtom = atom<FlowWindow>(emptyWindow);
|
||||
|
||||
/** Oldest first, so new traffic arrives at the end of the list */
|
||||
export const flowsAtom = orderedListAtom("flow", "startedAt", "asc");
|
||||
export const flowsAtom = atom((get) => get(flowWindowAtom).flows);
|
||||
|
||||
/** Start of the newest flow held, across all traffic */
|
||||
export const newestStartedAtAtom = atom((get) => get(flowsAtom).at(-1)?.startedAt ?? null);
|
||||
|
||||
/** Start and id of the oldest flow held, which loading older flows moves back */
|
||||
export const oldestFlowAtom = atom((get) => get(flowsAtom)[0] ?? null);
|
||||
|
||||
export const hasOlderFlowsAtom = atom((get) => get(flowWindowAtom).hasOlder);
|
||||
|
||||
/** Flows in the database */
|
||||
export const flowTotalAtom = atom((get) => get(flowWindowAtom).total);
|
||||
|
||||
/** Follows new traffic, dropping the older flows scrolling up loaded, or holds the list */
|
||||
export const followAtom = atom(
|
||||
(get) => get(followingAtom),
|
||||
(_get, set, following: boolean) => {
|
||||
set(followingAtom, following);
|
||||
if (following) set(flowWindowAtom, (win) => trimWindow(win));
|
||||
},
|
||||
);
|
||||
|
||||
let loadingOlder = false;
|
||||
|
||||
/** Loads the page of flows before the oldest one held */
|
||||
export const loadOlderFlowsAtom = atom(null, async (get, set) => {
|
||||
const { flows, hasOlder } = get(flowWindowAtom);
|
||||
const first = flows[0];
|
||||
if (loadingOlder || !hasOlder || first == null) return;
|
||||
loadingOlder = true;
|
||||
try {
|
||||
const res = await rpc("list_flows", { before: first.id, limit: PAGE_SIZE });
|
||||
// Dropped when the window moved on meanwhile, trimmed or cleared.
|
||||
set(flowWindowAtom, (win) =>
|
||||
win.flows[0]?.id === first.id ? prependOlder(win, res.flows, PAGE_SIZE) : win,
|
||||
);
|
||||
} catch (err) {
|
||||
console.warn("Failed to load older flows", err);
|
||||
} finally {
|
||||
loadingOlder = false;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Loads the newest flows, then keeps them current with the engine's events. Events gather for
|
||||
* `FLUSH_MS` and apply together, and the window drops its oldest flows while the list follows,
|
||||
* so a new flow costs the same however many the database holds.
|
||||
*/
|
||||
export function syncFlows(store: ReturnType<typeof createStore>) {
|
||||
let pending: ModelPayload[] = [];
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let loaded = false;
|
||||
|
||||
function flush() {
|
||||
timer = null;
|
||||
if (!loaded || pending.length === 0) return;
|
||||
const changes = pending;
|
||||
pending = [];
|
||||
store.set(flowWindowAtom, (win) => {
|
||||
const next = applyChanges(win, changes);
|
||||
return store.get(followingAtom) ? trimWindow(next) : next;
|
||||
});
|
||||
const selected = store.get(selectedFlowAtom);
|
||||
const change = changes.findLast((c) => c.model.id === selected?.id);
|
||||
if (change != null) {
|
||||
store.set(selectedFlowAtom, change.change.type === "delete" ? null : change.model);
|
||||
}
|
||||
}
|
||||
|
||||
listen("model_write", (payload) => {
|
||||
pending.push(payload);
|
||||
if (timer == null) timer = setTimeout(flush, FLUSH_MS);
|
||||
});
|
||||
|
||||
// Loads again rather than only emptying, for a flow written between the delete and the event.
|
||||
listen("flows_cleared", () => {
|
||||
pending = [];
|
||||
store.set(flowWindowAtom, emptyWindow);
|
||||
store.set(selectedFlowAtom, null);
|
||||
store.set(followingAtom, true);
|
||||
load();
|
||||
});
|
||||
|
||||
let loads = 0;
|
||||
function load() {
|
||||
loaded = false;
|
||||
const thisLoad = ++loads;
|
||||
void rpc("list_flows", { limit: WINDOW_SIZE }).then((res) => {
|
||||
// A clear since asked for a newer page.
|
||||
if (thisLoad !== loads) return;
|
||||
store.set(flowWindowAtom, windowFromPage(res.flows, WINDOW_SIZE, res.total));
|
||||
loaded = true;
|
||||
flush();
|
||||
});
|
||||
}
|
||||
|
||||
load();
|
||||
}
|
||||
@@ -6,22 +6,13 @@ import { type ReactNode, StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { ProxyLayout } from "./components/ProxyLayout";
|
||||
import { useHotkeyContextValue } from "./lib/hotkeys";
|
||||
import { listen, rpc } from "./lib/rpc";
|
||||
import { applyChange, dataAtom, replaceAll } from "./lib/store";
|
||||
import { syncFlows } from "./lib/store";
|
||||
import "./main.css";
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
const jotaiStore = createStore();
|
||||
|
||||
// Load initial models from the database
|
||||
void rpc("list_models", {}).then((res) => {
|
||||
jotaiStore.set(dataAtom, (prev) => replaceAll(prev, "flow", res.flows));
|
||||
});
|
||||
|
||||
// Subscribe to model change events from the backend
|
||||
void listen("model_write", (payload) => {
|
||||
jotaiStore.set(dataAtom, (prev) => applyChange(prev, "flow", payload.model, payload.change));
|
||||
});
|
||||
syncFlows(jotaiStore);
|
||||
|
||||
function Hotkeys({ children }: { children: ReactNode }) {
|
||||
return <HotkeyProvider value={useHotkeyContextValue()}>{children}</HotkeyProvider>;
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
"@tanstack/react-virtual": "^3.14.17",
|
||||
"@tauri-apps/api": "^2.12.3",
|
||||
"@tauri-apps/plugin-os": "^2.4.0",
|
||||
"@yaakapp-internal/model-store": "^1.0.0",
|
||||
"@yaakapp-internal/proxy-lib": "^1.0.0",
|
||||
"@yaakapp-internal/theme": "^1.0.0",
|
||||
"@yaakapp-internal/ui": "^1.0.0",
|
||||
|
||||
Reference in new issue
Block a user