Open History to see the last commit
1import { Trash2, X } from "lucide-react";
2
3import { clearRequestLog, useRequestLog } from "@/api/request-log";
4import { useNetworkConfig } from "@/api/network-config";
5
6import { setInspectorOpen, useIsInspectorOpen } from "./inspector-store";
7import { formatLatency } from "./latency";
8import { RequestLog, usePendingRequestCount } from "./network-controls";
9
10export const REQUEST_INSPECTOR_ID = "request-inspector";
11
12/**
13 * The request inspector as a drawer under the app (wide screens): every call
14 * to the fake API, live. Opened from the browser toolbar or from the guide.
15 */
16export function RequestInspectorDrawer() {
17 const open = useIsInspectorOpen();
18 const count = useRequestLog().length;
19 const pending = usePendingRequestCount();
20 const { latencyMs } = useNetworkConfig();
21
22 return (
23 <section
24 id={REQUEST_INSPECTOR_ID}
25 aria-label="Request inspector"
26 data-spotlight="request-inspector"
27 hidden={!open}
28 className="flex h-[min(42%,22rem)] min-h-40 shrink-0 flex-col border-t bg-background text-foreground"
29 >
30 <header className="flex h-9 shrink-0 items-center gap-2 border-b px-3">
31 <h2 className="text-[13px] font-semibold">Requests</h2>
32 <span className="text-[12px] text-muted-foreground tabular-nums">
33 {count} {count === 1 ? "request" : "requests"}
34 {pending > 0 && `, ${pending} pending`} · latency {formatLatency(latencyMs)}
35 </span>
36 <div className="ml-auto flex items-center gap-0.5">
37 <DrawerButton label="Clear the list" onClick={clearRequestLog} disabled={count === 0}>
38 <Trash2 />
39 </DrawerButton>
40 <DrawerButton label="Close the request inspector" onClick={() => setInspectorOpen(false)}>
41 <X />
42 </DrawerButton>
43 </div>
44 </header>
45 <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain">
46 <RequestLog />
47 </div>
48 </section>
49 );
50}
51
52function DrawerButton({
53 label,
54 onClick,
55 disabled,
56 children,
57}: {
58 label: string;
59 onClick: () => void;
60 disabled?: boolean;
61 children: React.ReactNode;
62}) {
63 return (
64 <button
65 type="button"
66 aria-label={label}
67 title={label}
68 onClick={onClick}
69 disabled={disabled}
70 className="grid size-7 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground disabled:opacity-40 [&_svg]:size-3.5"
71 >
72 {children}
73 </button>
74 );
75}