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