Open History to see the last commit
1import { eq, useLiveQuery } from "@tanstack/react-db";
2import {
3 ChevronDownIcon,
4 ChevronRightIcon,
5 LoaderIcon,
6 Trash2Icon,
7} from "lucide-react";
8import { AnimatePresence, motion } from "motion/react";
9import { useState } from "react";
10import {
11 type ApiRequest,
12 apiRequestsCollection,
13} from "@/collections/apiRequests";
14import { userPreferencesCollection } from "@/collections/UserPreferences";
15import { cn } from "@/lib/utils";
16import { HighlightWrapper } from "@/utils/highlight-collection-related-info";
17import { USER_PLACEHOLDER } from "@/utils/USER_PLACEHOLDER_CONSTANT";
18import { Badge } from "./ui/badge";
19import { Button } from "./ui/button";
20import {
21 Collapsible,
22 CollapsibleContent,
23 CollapsibleTrigger,
24} from "./ui/collapsible";
25
26const API_PANEL_WIDTH = "24rem";
27const PANEL_ANIMATION_DURATION = 0.15;
28
29async function clearRequests() {
30 const ids = (await apiRequestsCollection.toArrayWhenReady()).map(
31 ({ id }) => id,
32 );
33 apiRequestsCollection.delete(ids);
34}
35
36function formatDuration(ms: number | null): string {
37 if (ms === null) {
38 return "...";
39 }
40 if (ms < 1000) {
41 return `${Math.round(ms)}ms`;
42 }
43 return `${(ms / 1000).toFixed(2)}s`;
44}
45
46function JsonViewer({
47 data,
48 label,
49 defaultOpen = false,
50}: {
51 data: unknown;
52 label: string;
53 defaultOpen?: boolean;
54}) {
55 const [isOpen, setIsOpen] = useState(defaultOpen);
56
57 if (data === undefined || data === null) {
58 return null;
59 }
60
61 return (
62 <Collapsible open={isOpen} onOpenChange={setIsOpen}>
63 <CollapsibleTrigger className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors">
64 {isOpen ? (
65 <ChevronDownIcon className="h-3 w-3" />
66 ) : (
67 <ChevronRightIcon className="h-3 w-3" />
68 )}
69 {label}
70 </CollapsibleTrigger>
71 <CollapsibleContent>
72 <pre className="mt-1 whitespace-pre-wrap p-2 bg-muted/50 rounded text-xs overflow-x-auto max-h-40 overflow-y-auto">
73 {typeof data === "string" ? data : JSON.stringify(data, null, 2)}
74 </pre>
75 </CollapsibleContent>
76 </Collapsible>
77 );
78}
79
80function RequestItem({ request }: { request: ApiRequest }) {
81 const [isExpanded, setIsExpanded] = useState(false);
82 const isPending = request.status === "pending";
83
84 return (
85 <div className="border-b border-border last:border-b-0 overflow-hidden">
86 <button
87 type="button"
88 onClick={() => setIsExpanded(!isExpanded)}
89 className="w-full p-3 hover:bg-muted/50 transition-colors text-left overflow-hidden"
90 >
91 <div className="flex items-center gap-2 overflow-hidden">
92 <Badge
93 variant="outline"
94 className={cn(
95 `font-mono text-[10px] px-1.5 py-0 shrink-0`,
96 request.method === "GET"
97 ? "bg-blue-100 text-blue-700 border-blue-700/30 dark:bg-blue-500/20 dark:text-blue-400 dark:border-blue-500/30"
98 : request.method === "POST"
99 ? "bg-green-200 text-green-700 border-green-400 dark:bg-green-500/20 dark:text-green-400 dark:border-green-500/30"
100 : request.method === "PATCH"
101 ? "bg-yellow-200 text-yellow-700 border-yellow-700/30 dark:bg-yellow-500/20 dark:text-yellow-400 dark:border-yellow-500/30"
102 : request.method === "DELETE"
103 ? "bg-red-100 text-red-500 border-red-400 dark:bg-red-500/20 dark:text-red-400 dark:border-red-500/30"
104 : "bg-gray-200 text-gray-600 border-gray-400 dark:bg-gray-500/20 dark:text-gray-400 dark:border-gray-500/30",
105 )}
106 >
107 {request.method}
108 </Badge>
109 <span className="flex-1 font-mono text-xs truncate min-w-0 max-w-full overflow-hidden">
110 {request.pathname}
111 <span className="text-muted-foreground">{request.search}</span>
112 </span>
113 {isPending ? (
114 <LoaderIcon className="h-3 w-3 text-orange-400 animate-spin shrink-0" />
115 ) : (
116 <span
117 className={cn(
118 "text-xs font-mono shrink-0",
119 request.status === "pending"
120 ? "text-orange-400 dark:text-orange-300"
121 : request.status >= 200 && request.status < 300
122 ? "text-green-500 dark:text-green-300"
123 : request.status >= 400
124 ? "text-destructive"
125 : "text-yellow-500 dark:text-yellow-400",
126 )}
127 >
128 {request.status}
129 </span>
130 )}
131 <span className="text-xs text-muted-foreground font-mono shrink-0">
132 {formatDuration(request.duration)}
133 </span>
134 {isExpanded ? (
135 <ChevronDownIcon className="h-3 w-3 text-muted-foreground shrink-0" />
136 ) : (
137 <ChevronRightIcon className="h-3 w-3 text-muted-foreground shrink-0" />
138 )}
139 </div>
140 </button>
141 {isExpanded && (
142 <div className="px-3 pb-3 space-y-2">
143 <div className="text-xs text-muted-foreground">
144 {new Date(request.timestamp).toLocaleTimeString()}
145 </div>
146 <JsonViewer data={request.requestBody} label="Request Body" />
147 <JsonViewer
148 data={request.search || null}
149 label="Search"
150 defaultOpen={!!request.search}
151 />
152 {!isPending && (
153 <JsonViewer data={request.responseBody} label="Response Body" />
154 )}
155 </div>
156 )}
157 </div>
158 );
159}
160
161export function ApiRequestsPanel() {
162 const { data: requests } = useLiveQuery(
163 (q) =>
164 q
165 .from({
166 apiRequest: apiRequestsCollection,
167 })
168 .orderBy(({ apiRequest }) => apiRequest.timestamp, "desc"),
169 [],
170 );
171
172 const { data: userPreferences } = useLiveQuery((q) =>
173 q
174 .from({
175 userPreferences: userPreferencesCollection,
176 })
177 .where(({ userPreferences }) =>
178 eq(userPreferences.id, USER_PLACEHOLDER.id),
179 )
180 .findOne(),
181 );
182
183 const isApiPanelOpen = userPreferences?.networkPanel === "open";
184
185 return (
186 <AnimatePresence initial={false}>
187 {isApiPanelOpen && (
188 <motion.div
189 initial={{ width: 0 }}
190 animate={{ width: API_PANEL_WIDTH }}
191 exit={{ width: 0 }}
192 style={{ overflow: "clip" }}
193 className="shrink-0 h-full border-l border-border"
194 transition={{ duration: PANEL_ANIMATION_DURATION }}
195 >
196 <div
197 className="flex flex-col h-full max-h-full overflow-hidden bg-background"
198 style={{ width: API_PANEL_WIDTH }}
199 >
200 <HighlightWrapper highlightId="networkPanel_panel">
201 <motion.div
202 className="flex flex-col h-full overflow-hidden"
203 initial={{ opacity: 0 }}
204 animate={{ opacity: 1 }}
205 exit={{ opacity: 0 }}
206 transition={{
207 duration: PANEL_ANIMATION_DURATION,
208 delay: PANEL_ANIMATION_DURATION,
209 }}
210 >
211 {/* Header */}
212 <div className="shrink-0 flex items-center justify-between px-3 py-2 border-b bg-muted/30">
213 <div className="flex items-center gap-2">
214 <h2 className="text-sm font-medium">API Requests</h2>
215 <Badge
216 variant="secondary"
217 className="text-[10px] px-1.5 py-0"
218 >
219 {requests.length}
220 </Badge>
221 </div>
222 <Button
223 variant="ghost"
224 size="icon"
225 onClick={clearRequests}
226 disabled={requests.length === 0}
227 >
228 <Trash2Icon />
229 <span className="sr-only">Clear requests</span>
230 </Button>
231 </div>
232
233 {/* Request list */}
234 <div className="flex-1 min-h-0 overflow-y-auto overflow-x-hidden">
235 {requests.length === 0 ? (
236 <div className="flex flex-col items-center justify-center h-full p-4 text-center">
237 <p className="text-sm text-muted-foreground">
238 No requests yet
239 </p>
240 <p className="text-xs text-muted-foreground mt-1">
241 API requests will appear here
242 </p>
243 </div>
244 ) : (
245 <div>
246 {requests.map((request) => (
247 <RequestItem key={request.id} request={request} />
248 ))}
249 </div>
250 )}
251 </div>
252 </motion.div>
253 </HighlightWrapper>
254 </div>
255 </motion.div>
256 )}
257 </AnimatePresence>
258 );
259}