import { memo, useEffect, useMemo, useState } from "react"; import { useTx } from "@/platform/i18n"; import { call, commands, type JsResponseBody } from "@/platform/ipc"; import { Button, Checkbox, IconButton, Input, T } from "@/ui"; import { addRequestBreakpoint } from "./eventBreakpoints"; import { clearNetwork, filterRows, NETWORK_CATEGORIES, useJsNetwork, type NetworkCategory, type NetworkRow, } from "./network"; import { useRefused } from "./refusals"; import styles from "./Network.module.css"; const NO_ROWS: NetworkRow[] = []; /** What a request breakpoint looks for to stop at the next request like `url`: its path. */ export function requestPattern(url: string): string { try { return new URL(url).pathname; } catch { return url; } } export function formatSize(bytes: number | null): string { if (bytes == null) return ""; if (bytes < 1000) return `${bytes} B`; return bytes < 1_000_000 ? `${(bytes / 1000).toFixed(1)} kB` : `${(bytes / 1_000_000).toFixed(1)} MB`; } export function formatDuration(ms: number | null): string { if (ms == null) return ""; return ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(2)} s`; } const Row = memo(function Row({ row, selected, onSelect, }: { row: NetworkRow; selected: boolean; onSelect: (key: string) => void; }) { return (
onSelect(row.key)} title={row.url} > {row.name} {row.error ?? row.status ?? ""} {row.method} {row.type} {formatSize(row.size)} {formatDuration(row.durationMs)}
); }); function Headers({ title, headers }: { title: string; headers: Record }) { const names = Object.keys(headers).sort((a, b) => a.localeCompare(b)); if (!names.length) return null; return ( <>
{title}
{names.map((name) => (
{name} {headers[name]}
))} ); } type Body = { state: "loading" } | { state: "gone" } | { state: "loaded"; body: JsResponseBody }; /** The body of one finished request; mounted per request (`key`), so it starts out loading. */ function ResponseBody({ session, requestId }: { session: number; requestId: string }) { const [body, setBody] = useState({ state: "loading" }); useEffect(() => { let current = true; call(commands.debugJsResponseBody(session, requestId)).then( (loaded) => current && setBody({ state: "loaded", body: loaded }), // The session ended, or the browser no longer keeps the response. () => current && setBody({ state: "gone" }), ); return () => { current = false; }; }, [session, requestId]); if (body.state === "loading") return null; if (body.state === "gone") return (
Response body is no longer available
); if (body.body.base64Encoded) return (
Binary content
); return ( <> {body.body.truncated && (
Showing the first 1 MB
)}
{body.body.body}
); } function Detail({ session, row }: { session: number; row: NetworkRow }) { const tx = useTx(); return (
{row.url}
{[row.method, row.error ?? row.status, row.mimeType].filter((part) => part != null && part !== "").join(" · ")}
{row.initiator && (
{tx("Requested by")} {row.initiator.function || tx("(anonymous)")} — {row.initiator.url}: {row.initiator.line + 1}
)}
{row.done && !row.error && ( <>
{tx("Response")}
)}
); } /** * Loads the page again. Recording starts while the page is already loading, so this is how * the document and its first scripts get into the list. */ function reloadPage(session: number) { void call(commands.debugEvaluateIn(session, null, "location.reload()", "repl")).catch(() => undefined); } /** The requests of the page debugged by `session`. */ export function NetworkView({ session }: { session: number }) { const tx = useTx(); const rows = useJsNetwork((s) => s.rows[session] ?? NO_ROWS); const [query, setQuery] = useState(""); const [category, setCategory] = useState("all"); const [mine, setMine] = useState(false); const [selectedKey, setSelectedKey] = useState(null); const shown = useMemo(() => filterRows(rows, query, category, mine), [rows, query, category, mine]); const selected = rows.find((r) => r.key === selectedKey) ?? null; const notReported = useRefused(session, "network") != null; return (
setQuery(e.target.value)} placeholder="Filter by URL" aria-label="Filter requests" />
{NETWORK_CATEGORIES.map((c) => ( ))}
reloadPage(session)} /> clearNetwork(session)} /> {shown.length === rows.length ? rows.length : `${shown.length} / ${rows.length}`}
{tx("Name")} {tx("Status")} {tx("Method")} {tx("Type")} {tx("Size")} {tx("Time")}
{shown.length === 0 && (
{rows.length ? tx("No request matches the filter") : notReported ? tx("This version of the JavaScript debugger does not report a page's requests.") : tx("No requests yet. Reload the page to see the requests of its load.")}
)} {shown.map((row) => ( ))}
{selected && }
); }