"use client"; import * as React from "react"; import { toast } from "sonner"; import { HttpCodeBlock } from "@/components/http-code-block"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { Skeleton } from "@/components/ui/skeleton"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { api } from "@/lib/api"; import type { FindingTrafficDetail, TrafficDetail } from "@/lib/types"; export function TrafficEvidenceViewer({ findingId, bindingId, contextTask, onClose, }: { findingId: string; bindingId: string | null; contextTask?: string; onClose: () => void; }) { const [detail, setDetail] = React.useState(null); const [error, setError] = React.useState(""); const [busy, setBusy] = React.useState(false); React.useEffect(() => { if (!bindingId) return; let active = true; setDetail(null); setError(""); api .findingTrafficDetail(findingId, bindingId, contextTask) .then((d) => { if (active) setDetail(d); }) .catch((e: Error) => { if (active) setError(e.message); }); return () => { active = false; }; }, [findingId, bindingId, contextTask]); async function more(side: "request" | "response") { if (!detail || !bindingId) return; setBusy(true); try { const next = await api.findingTrafficBody(findingId, bindingId, side, detail[side].next_offset, contextTask); setDetail((current) => current?.binding.id === bindingId ? { ...current, [side]: { ...next, content: current[side].content + next.content } } : current, ); } catch (e) { toast.error((e as Error).message); } finally { setBusy(false); } } return ( { if (!open) onClose(); }} > 流量证据 #{bindingId} {detail?.binding.snapshot.url ?? "查看绑定时保存的请求与响应"} {error ? ( {error} ) : detail ? ( 请求 Request 响应 Response {(["request", "response"] as const).map((side) => (
正文 {detail[side].total.toLocaleString()} 字节{detail[side].truncated ? " · 当前为预览" : ""}
{detail[side].truncated && !detail[side].binary ? ( ) : null}
))}
) : ( )}
); } export function CapturedTrafficViewer({ id, onClose }: { id: string | null; onClose: () => void }) { const [detail, setDetail] = React.useState(null); const [error, setError] = React.useState(""); React.useEffect(() => { if (!id) return; let active = true; setDetail(null); setError(""); api .trafficExchange(id) .then((d) => { if (active) setDetail(d); }) .catch((e: Error) => { if (active) setError(e.message); }); return () => { active = false; }; }, [id]); return ( { if (!open) onClose(); }} > 预览流量 流量 ID:{id}。绑定时会保存完整正文。 {error ? ( {error} ) : detail ? ( 请求 Request 响应 Response ) : ( )} ); }