"use client"; import * as React from "react"; import { toast } from "sonner"; import { CapturedTrafficViewer } from "@/components/traffic-evidence-viewer"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { api } from "@/lib/api"; import type { FindingTrafficBinding, TrafficResp } from "@/lib/types"; const METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"]; export function TrafficPickerDialog({ findingId, contextTask, bound, onClose, onBound, }: { findingId: string; contextTask?: string; bound: FindingTrafficBinding[]; onClose: () => void; onBound: () => void; }) { const [host, setHost] = React.useState(""); const [method, setMethod] = React.useState("all"); const [query, setQuery] = React.useState(""); const [page, setPage] = React.useState(0); const [data, setData] = React.useState(null); const [selected, setSelected] = React.useState>(() => new Set()); const [preview, setPreview] = React.useState(null); const [loading, setLoading] = React.useState(true); const [busy, setBusy] = React.useState(false); const [error, setError] = React.useState(""); const alreadyBound = new Set(bound.map((b) => b.snapshot.source_traffic_id)); React.useEffect(() => { let active = true; setLoading(true); setError(""); const timer = setTimeout(() => { api .traffic(page, 25, host, method === "all" ? "" : method, query) .then((d) => { if (active) setData(d); }) .catch((e: Error) => { if (active) setError(e.message); }) .finally(() => { if (active) setLoading(false); }); }, 250); return () => { active = false; clearTimeout(timer); }; }, [page, host, method, query]); const rows = data?.exchanges ?? []; const selectable = rows.filter((e) => !alreadyBound.has(e.id)); function toggle(id: string, checked: boolean) { setSelected((previous) => { const next = new Set(previous); if (checked) next.add(id); else next.delete(id); return next; }); } async function save() { setBusy(true); setError(""); try { await api.bindFindingTraffic( findingId, [...selected].map((traffic_id) => ({ traffic_id })), contextTask, ); toast.success(`已绑定 ${selected.size} 条流量`); onBound(); onClose(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return ( <> { if (!open && !busy) onClose(); }} > 绑定流量 筛选并多选请求/响应,已选记录会跨页保留。绑定后可设置用途、说明和顺序。 目标 host { setHost(e.target.value); setPage(0); }} /> 请求方法 关键词 { setQuery(e.target.value); setPage(0); }} /> {error ? ( {error} ) : null}
0 && selectable.every((e) => selected.has(e.id))} onCheckedChange={(checked) => setSelected((previous) => { const next = new Set(previous); for (const e of selectable) { if (checked === true) next.add(e.id); else next.delete(e.id); } return next; }) } /> 方法 / URL 时间 状态码 操作 {rows.map((e) => ( toggle(e.id, checked === true)} /> {e.method} {e.url} {alreadyBound.has(e.id) ? 已绑定 : null} {new Date(e.ts).toLocaleString("zh-CN")} {e.status} ))} {!rows.length ? ( {loading ? "加载中…" : "没有匹配的流量"} ) : null}
已选 {selected.size} 条 · 共 {data?.total ?? 0} 条
第 {page + 1} 页
setPreview(null)} /> ); }