Open History to see the last commit
1import { ArrowDownUp } from "lucide-react";
2import { useId } from "react";
3
4import { Switch } from "@/components/ui/switch";
5import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
6import { useNetworkConfig } from "@/api/network-config";
7import { cn } from "@/lib/utils";
8
9import { LATENCY_PRESETS, formatLatency } from "./latency";
10
11/*
12 * Latency and failure controls for the fake API, plus the request inspector.
13 * They show the fake API's settings (`api/network-config.ts`) but stay
14 * disabled until phase 4 lets them change it and lists `api/request-log.ts`. They're shared by the
15 * app sheet (small screens) and, later, the desktop browser toolbar's
16 * controls slot.
17 */
18
19/** Phase 4 makes these interactive. */
20const NETWORK_CONTROLS_READY = false;
21
22export function LatencyControl({ className }: { className?: string }) {
23 const { latencyMs } = useNetworkConfig();
24 return (
25 <ToggleGroup
26 type="single"
27 variant="outline"
28 size="sm"
29 spacing={0}
30 value={String(latencyMs)}
31 disabled={!NETWORK_CONTROLS_READY}
32 aria-label="API latency"
33 className={className}
34 >
35 {LATENCY_PRESETS.map((ms) => (
36 <ToggleGroupItem key={ms} value={String(ms)} className="flex-1 tabular-nums">
37 {formatLatency(ms)}
38 </ToggleGroupItem>
39 ))}
40 </ToggleGroup>
41 );
42}
43
44export function FailNextRequestControl({ className }: { className?: string }) {
45 const id = useId();
46 const { failNextRequest } = useNetworkConfig();
47 return (
48 <div className={cn("flex items-center justify-between gap-3", className)}>
49 <label htmlFor={id} className="text-sm">
50 Fail the next request
51 </label>
52 <Switch id={id} checked={failNextRequest} disabled={!NETWORK_CONTROLS_READY} />
53 </div>
54 );
55}
56
57/** Every call to the fake API, newest first. Phase 4 renders `useRequestLog()` here. */
58export function RequestLog({ className }: { className?: string }) {
59 return (
60 <div className={cn("flex flex-col", className)}>
61 <div className="grid grid-cols-[3.5rem_1fr_3rem_3.5rem] gap-2 border-b px-3 py-2 text-[11px] font-medium tracking-wide text-muted-foreground uppercase">
62 <span>Method</span>
63 <span>URL</span>
64 <span className="text-right">Status</span>
65 <span className="text-right">Time</span>
66 </div>
67 {/* Phase 4: an <ol> with one row per `useRequestLog()` entry replaces this empty state. */}
68 <div className="flex min-h-40 flex-1 flex-col items-center justify-center gap-1 px-6 pb-8 text-center">
69 <ArrowDownUp className="mb-1 size-5 text-muted-foreground" aria-hidden />
70 <p className="text-sm font-medium">No requests yet</p>
71 <p className="max-w-64 text-[13px] text-muted-foreground">
72 Every call the app makes to the fake API will show up here, with its status and timing.
73 </p>
74 </div>
75 </div>
76 );
77}
78
79export function ComingSoonBadge() {
80 return NETWORK_CONTROLS_READY ? null : (
81 <span className="rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium whitespace-nowrap text-muted-foreground">
82 Coming in phase 4
83 </span>
84 );
85}