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

app-sheet.tsx

118 lines3.8 KB

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