Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

network-controls.tsx

82 lines2.9 KB

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 { cn } from "@/lib/utils";
7​
8import { LATENCY_PRESETS, formatLatency } from "./latency";
9​
10/*
11 * Latency and failure controls for the fake API, plus the request inspector.
12 * They're laid out but inert in phase 1: phase 2 adds the fake API and its
13 * request log, phase 4 connects these controls to it. They're shared by the
14 * app sheet (small screens) and, later, the desktop browser toolbar's
15 * controls slot.
16 */
17​
18/** Phase 1: nothing to control yet. Flip once the fake API exists. */
19const NETWORK_CONTROLS_READY = false;
20​
21export function LatencyControl({ className }: { className?: string }) {
22 return (
23 <ToggleGroup
24 type="single"
25 variant="outline"
26 size="sm"
27 spacing={0}
28 value="0"
29 disabled={!NETWORK_CONTROLS_READY}
30 aria-label="API latency"
31 className={className}
32 >
33 {LATENCY_PRESETS.map((ms) => (
34 <ToggleGroupItem key={ms} value={String(ms)} className="flex-1 tabular-nums">
35 {formatLatency(ms)}
36 </ToggleGroupItem>
37 ))}
38 </ToggleGroup>
39 );
41​
42export function FailNextRequestControl({ className }: { className?: string }) {
43 const id = useId();
44 return (
45 <div className={cn("flex items-center justify-between gap-3", className)}>
46 <label htmlFor={id} className="text-sm">
47 Fail the next request
48 </label>
49 <Switch id={id} checked={false} disabled={!NETWORK_CONTROLS_READY} />
50 </div>
51 );
53​
54/** Every call to the fake API, newest first. Empty until phase 2 records them. */
55export function RequestLog({ className }: { className?: string }) {
56 return (
57 <div className={cn("flex flex-col", className)}>
58 <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">
59 <span>Method</span>
60 <span>URL</span>
61 <span className="text-right">Status</span>
62 <span className="text-right">Time</span>
63 </div>
64 {/* Phase 2: an <ol> with one row per recorded request replaces this empty state. */}
65 <div className="flex min-h-40 flex-1 flex-col items-center justify-center gap-1 px-6 pb-8 text-center">
66 <ArrowDownUp className="mb-1 size-5 text-muted-foreground" aria-hidden />
67 <p className="text-sm font-medium">No requests yet</p>
68 <p className="max-w-64 text-[13px] text-muted-foreground">
69 Every call the app makes to the fake API will show up here, with its status and timing.
70 </p>
71 </div>
72 </div>
73 );
75​
76export function ComingSoonBadge() {
77 return NETWORK_CONTROLS_READY ? null : (
78 <span className="rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium whitespace-nowrap text-muted-foreground">
79 Coming in phase 4
80 </span>
81 );