Open History to see the last commit
1import { useLiveQuery } from "@tanstack/react-db";
2import {
3 ChevronDownIcon,
4 ChevronRightIcon,
5 LoaderIcon,
6 Trash2Icon,
7} from "lucide-react";
8import { useState } from "react";
9import {
10 type ApiRequest,
11 apiRequestsCollection,
12} from "@/collections/apiRequests";
13import { cn } from "@/lib/utils";
14import { Badge } from "./ui/badge";
15import { Button } from "./ui/button";
16import {
17 Collapsible,
18 CollapsibleContent,
19 CollapsibleTrigger,
20} from "./ui/collapsible";
21import { ScrollArea } from "./ui/scroll-area";
22
23async function clearRequests() {
24 const ids = (await apiRequestsCollection.toArrayWhenReady()).map(
25 ({ id }) => id,
26 );
27 apiRequestsCollection.delete(ids);
28}
29
30function formatDuration(ms: number | null): string {
31 if (ms === null) {
32 return "...";
33 }
34 if (ms < 1000) {
35 return `${Math.round(ms)}ms`;
36 }
37 return `${(ms / 1000).toFixed(2)}s`;
38}
39
40function JsonViewer({ data, label }: { data: unknown; label: string }) {
41 const [isOpen, setIsOpen] = useState(false);
42
43 if (data === undefined || data === null) {
44 return null;
45 }
46
47 return (
48 <Collapsible open={isOpen} onOpenChange={setIsOpen}>
49 <CollapsibleTrigger className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors">
50 {isOpen ? (
51 <ChevronDownIcon className="h-3 w-3" />
52 ) : (
53 <ChevronRightIcon className="h-3 w-3" />
54 )}
55 {label}
56 </CollapsibleTrigger>
57 <CollapsibleContent>
58 <pre className="mt-1 whitespace-pre-wrap p-2 bg-muted/50 rounded text-xs overflow-x-auto max-h-40 overflow-y-auto">
59 {JSON.stringify(data, null, 2)}
60 </pre>
61 </CollapsibleContent>
62 </Collapsible>
63 );
64}
65
66function RequestItem({ request }: { request: ApiRequest }) {
67 const [isExpanded, setIsExpanded] = useState(false);
68 const isPending = request.status === "pending";
69
70 return (
71 <div className="border-b border-border last:border-b-0">
72 <button
73 type="button"
74 onClick={() => setIsExpanded(!isExpanded)}
75 className="w-full p-3 hover:bg-muted/50 transition-colors text-left"
76 >
77 <div className="flex items-center gap-2">
78 <Badge
79 variant="outline"
80 className={cn(
81 `font-mono text-[10px] px-1.5 py-0`,
82 request.method === "GET"
83 ? "bg-blue-100 text-blue-700 border-blue-700/30 dark:bg-blue-500/20 dark:text-blue-400 dark:border-blue-500/30"
84 : request.method === "POST"
85 ? "bg-green-200 text-green-700 border-green-400 dark:bg-green-500/20 dark:text-green-400 dark:border-green-500/30"
86 : request.method === "PATCH"
87 ? "bg-yellow-200 text-yellow-700 border-yellow-700/30 dark:bg-yellow-500/20 dark:text-yellow-400 dark:border-yellow-500/30"
88 : request.method === "DELETE"
89 ? "bg-red-100 text-red-500 border-red-400 dark:bg-red-500/20 dark:text-red-400 dark:border-red-500/30"
90 : "bg-gray-200 text-gray-600 border-gray-400 dark:bg-gray-500/20 dark:text-gray-400 dark:border-gray-500/30",
91 )}
92 >
93 {request.method}
94 </Badge>
95 <span className="flex-1 font-mono text-xs truncate">
96 {request.pathname}
97 </span>
98 {isPending ? (
99 <LoaderIcon className="h-3 w-3 text-orange-400 animate-spin" />
100 ) : (
101 <span
102 className={cn(
103 "text-xs font-mono",
104 request.status === "pending"
105 ? "text-orange-400 dark:text-orange-300"
106 : request.status >= 200 && request.status < 300
107 ? "text-green-500 dark:text-green-300"
108 : request.status >= 400
109 ? "text-destructive"
110 : "text-yellow-500 dark:text-yellow-400",
111 )}
112 >
113 {request.status}
114 </span>
115 )}
116 <span className="text-xs text-muted-foreground font-mono">
117 {formatDuration(request.duration)}
118 </span>
119 {isExpanded ? (
120 <ChevronDownIcon className="h-3 w-3 text-muted-foreground" />
121 ) : (
122 <ChevronRightIcon className="h-3 w-3 text-muted-foreground" />
123 )}
124 </div>
125 </button>
126 {isExpanded && (
127 <div className="px-3 pb-3 space-y-2">
128 <div className="text-xs text-muted-foreground">
129 {new Date(request.timestamp).toLocaleTimeString()}
130 </div>
131 <JsonViewer data={request.requestBody} label="Request Body" />
132 {!isPending && (
133 <JsonViewer data={request.responseBody} label="Response Body" />
134 )}
135 </div>
136 )}
137 </div>
138 );
139}
140
141export function ApiRequestsPanel() {
142 const { data: requests } = useLiveQuery(
143 (q) =>
144 q
145 .from({
146 apiRequest: apiRequestsCollection,
147 })
148 .orderBy(({ apiRequest }) => apiRequest.timestamp, "desc"),
149 [],
150 );
151
152 return (
153 <div className="flex flex-col h-full max-h-full overflow-hidden bg-background">
154 {/* Header */}
155 <div className="shrink-0 flex items-center justify-between px-3 py-2 border-b bg-muted/30">
156 <div className="flex items-center gap-2">
157 <h2 className="text-sm font-medium">API Requests</h2>
158 <Badge variant="secondary" className="text-[10px] px-1.5 py-0">
159 {requests.length}
160 </Badge>
161 </div>
162 <Button
163 variant="ghost"
164 size="icon"
165 onClick={clearRequests}
166 disabled={requests.length === 0}
167 >
168 <Trash2Icon />
169 <span className="sr-only">Clear requests</span>
170 </Button>
171 </div>
172
173 {/* Request list */}
174 <ScrollArea className="flex-1 min-h-0">
175 {requests.length === 0 ? (
176 <div className="flex flex-col items-center justify-center h-full p-4 text-center">
177 <p className="text-sm text-muted-foreground">No requests yet</p>
178 <p className="text-xs text-muted-foreground mt-1">
179 API requests will appear here
180 </p>
181 </div>
182 ) : (
183 <div>
184 {requests.map((request) => (
185 <RequestItem key={request.id} request={request} />
186 ))}
187 </div>
188 )}
189 </ScrollArea>
190 </div>
191 );
192}