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

app-sheet.tsx

140 lines4.7 KB

1import { Gauge } from "lucide-react";
2import { useId } from "react";
3import type { ReactNode } from "react";
4​
5import { clearRequestLog, useRequestLog } from "@/api/request-log";
6import {
7 FailNextRequestControl,
8 LatencyControl,
9 RequestLog,
10 usePendingRequestCount,
11} from "@/components/network/network-controls";
12import { cn } from "@/lib/utils";
13​
14import {
15 setAppSheetView,
16 useAppSheetView,
17 useCloseAppSheet,
18 useIsAppSheetOpen,
19} from "./app-sheet-store";
20import { BottomSheet } from "./bottom-sheet";
21​
22/**
23 * The Notes app on small screens: a sheet over the guide, with the iPhone
24 * layout inside. The header holds Close and a Network button that shows the
25 * latency/failure controls and the request inspector over the app.
26 *
27 * `app` is the slot the (single, always mounted) Notes app is placed in; see
28 * `AppSlot` in the workspace.
29 */
30export function AppSheet({ app }: { app: ReactNode }) {
31 const open = useIsAppSheetOpen();
32 const view = useAppSheetView();
33 const closeAppSheet = useCloseAppSheet();
34 const networkPanelId = useId();
35 const showNetwork = view === "network";
36​
37 return (
38 <BottomSheet
39 open={open}
40 onClose={closeAppSheet}
41 label="Notes app"
42 header={
43 <div className="flex h-11 items-center justify-between px-3 pb-1">
44 <button
45 type="button"
46 aria-expanded={showNetwork}
47 aria-controls={networkPanelId}
48 onClick={() => setAppSheetView(showNetwork ? "app" : "network")}
49 className={cn(
50 "flex h-8 items-center gap-1.5 rounded-full px-3 text-sm font-medium transition-colors",
51 showNetwork
52 ? "bg-foreground text-background"
53 : "text-foreground/80 hover:bg-foreground/5",
54 )}
55 >
56 <Gauge className="size-4" aria-hidden />
57 Network
58 <PendingBadge />
59 </button>
60 <button
61 type="button"
62 onClick={closeAppSheet}
63 className="h-8 rounded-full border border-foreground/15 bg-background px-4 text-sm font-medium shadow-xs transition-colors hover:bg-muted"
64 >
65 Close
66 </button>
67 </div>
68 }
69 >
70 <div className="h-full border-t" inert={showNetwork}>
71 {app}
72 </div>
73 <NetworkPanel id={networkPanelId} open={showNetwork} />
74 </BottomSheet>
75 );
77​
78function PendingBadge() {
79 const pending = usePendingRequestCount();
80 if (pending === 0) return null;
81 return (
82 <span className="grid h-4 min-w-4 place-items-center rounded-full bg-guide-accent px-1 text-[10px] font-semibold text-white tabular-nums">
83 {pending}
84 <span className="sr-only"> pending</span>
85 </span>
86 );
88​
89function NetworkPanel({ id, open }: { id: string; open: boolean }) {
90 const count = useRequestLog().length;
91 return (
92 <section
93 id={id}
94 aria-label="Network"
95 inert={!open}
96 data-state={open ? "open" : "closed"}
97 className={cn(
98 "absolute inset-0 flex flex-col overflow-y-auto overscroll-contain border-t bg-background",
99 "transition-[opacity,translate,visibility] duration-200 ease-out motion-reduce:translate-y-0",
100 "data-[state=closed]:invisible data-[state=closed]:translate-y-3 data-[state=closed]:opacity-0",
101 )}
102 >
103 <div className="space-y-5 px-4 pt-4 pb-[calc(1.5rem+env(safe-area-inset-bottom))]">
104 <header>
105 <h2 className="text-base font-semibold">Network</h2>
106 <p className="mt-0.5 text-[13px] leading-snug text-muted-foreground">
107 Slow the fake API down or make it fail, and watch every request the app makes.
108 </p>
109 </header>
110​
111 <div data-spotlight="network-controls" className="space-y-3">
112 <div className="space-y-2">
113 <h3 className="text-xs font-medium tracking-wide text-muted-foreground uppercase">
114 Latency
115 </h3>
116 <LatencyControl className="w-full" size="default" />
117 </div>
118 <FailNextRequestControl className="rounded-xl border px-3 py-2.5" />
119 </div>
120​
121 <div data-spotlight="request-inspector" className="space-y-2">
122 <div className="flex items-center justify-between">
123 <h3 className="text-xs font-medium tracking-wide text-muted-foreground uppercase">
124 Requests
125 </h3>
126 <button
127 type="button"
128 onClick={clearRequestLog}
129 disabled={count === 0}
130 className="text-[13px] font-medium text-guide-accent disabled:opacity-40"
131 >
132 Clear
133 </button>
134 </div>
135 <RequestLog className="overflow-hidden rounded-xl border" />
136 </div>
137 </div>
138 </section>
139 );