"use client"; import * as React from "react"; import { toast } from "sonner"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; 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 { api } from "@/lib/api"; import type { Finding, FindingsPage } from "@/lib/types"; export function LinkTrafficDialog({ trafficIds, onClose, onBound, }: { trafficIds: string[]; onClose: () => void; onBound: () => void; }) { const [query, setQuery] = React.useState(""); const [page, setPage] = React.useState(1); const [data, setData] = React.useState(null); const [selected, setSelected] = React.useState(null); const [error, setError] = React.useState(""); const [busy, setBusy] = React.useState(false); const [loading, setLoading] = React.useState(true); React.useEffect(() => { let active = true; setLoading(true); setError(""); const timer = setTimeout(() => { api .findingsPage({ page, pageSize: 20, 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); }; }, [query, page]); async function save() { if (!selected?.finding_id) return; setBusy(true); setError(""); try { await api.bindFindingTraffic( selected.finding_id, trafficIds.map((traffic_id) => ({ traffic_id })), ); toast.success(`已关联 ${trafficIds.length} 条流量到漏洞 #${selected.finding_id}`); onBound(); onClose(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return ( { if (!open && !busy) onClose(); }} > 关联到漏洞 将所选 {trafficIds.length} 条流量保存为已有漏洞的证据。 查找漏洞 { setQuery(e.target.value); setPage(1); }} /> {error ? ( {error} ) : null}
{(data?.items ?? []).map((f) => ( ))} {!data?.items.length ? (

{loading ? "加载中…" : "没有匹配的漏洞,请先登记漏洞"}

) : null}
第 {page} 页 · 共 {data?.total ?? 0} 条

{selected ? `已选漏洞:#${selected.finding_id} ${selected.name || selected.vulnclass}` : "请选择一个漏洞"}

); }