Files
ARTEX-chixuweihu/web/src/app/(main)/function/assets/page.tsx
T

1345 lines
49 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import * as React from "react";
import { useSearchParams } from "next/navigation";
import {
BuildingIcon,
ChevronLeftIcon,
ChevronRightIcon,
GlobeIcon,
KeyRoundIcon,
LayoutTemplateIcon,
LinkIcon,
type LucideIcon,
NetworkIcon,
RefreshCwIcon,
SmartphoneIcon,
Trash2Icon,
} from "lucide-react";
import { toast } from "sonner";
import { AssetDslSearch } from "@/components/asset-dsl-search";
import { IntelligencePanel } from "@/components/intelligence-panel";
import { ScopeTextEditor } from "@/components/scope-text-editor";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Checkbox } from "@/components/ui/checkbox";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} 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 {
Sheet,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { type UITranslate, useUITranslation } from "@/i18n/use-ui-translation";
import { api } from "@/lib/api";
import { parseCompanyScopeText } from "@/lib/company-scope";
import type { Asset, Company, CompanyScopeRule } from "@/lib/types";
import { cn } from "@/lib/utils";
const METHOD_COLOR: Record<string, string> = {
GET: "bg-emerald-100 text-emerald-700",
POST: "bg-blue-100 text-blue-700",
PUT: "bg-amber-100 text-amber-700",
PATCH: "bg-orange-100 text-orange-700",
DELETE: "bg-red-100 text-red-700",
HEAD: "bg-purple-100 text-purple-700",
OPTIONS: "bg-slate-100 text-slate-600",
};
function MethodBadge({ method }: { method: string }) {
const m = method.toUpperCase();
return (
<span
className={cn(
"inline-block rounded px-1.5 py-0.5 font-mono text-[10px] font-semibold leading-none",
METHOD_COLOR[m] ?? "bg-muted text-muted-foreground",
)}
>
{m || "—"}
</span>
);
}
function statusTone(code: number) {
if (code >= 500) return "text-red-500";
if (code >= 400) return "text-amber-500";
if (code >= 300) return "text-blue-500";
if (code >= 200) return "text-emerald-500";
return "text-muted-foreground";
}
const PAGE_SIZES = [25, 50, 100, 200];
const TABS = (ui: UITranslate): { key: string; label: string; icon: LucideIcon }[] => [
{ key: "company", label: ui("screen.409d0719010a46"), icon: BuildingIcon },
{ key: "root_domain", label: ui("screen.bc208f697acff9"), icon: GlobeIcon },
{ key: "ip", label: "IP", icon: NetworkIcon },
{ key: "subdomain", label: ui("screen.4b4788120c34db"), icon: GlobeIcon },
{ key: "app", label: ui("screen.63c73c4730f447"), icon: SmartphoneIcon },
{ key: "service", label: ui("screen.ec309ab207ef7f"), icon: LayoutTemplateIcon },
{ key: "endpoint", label: ui("screen.c80d519245a7e8"), icon: LinkIcon },
];
function AssetsContent() {
const { ui, liveUI } = useUITranslation();
const searchParams = useSearchParams();
const requestedAsset = searchParams.get("asset") ?? "";
const [assetTarget, setAssetTarget] = React.useState<{ id: number; type: string }>();
const [assetTargetError, setAssetTargetError] = React.useState("");
React.useEffect(() => {
let alive = true;
setAssetTarget(undefined);
setAssetTargetError("");
if (!/^[1-9]\d*$/.test(requestedAsset)) return;
api
.searchAssets(`id==${requestedAsset}`, "", 1)
.then((r) => {
if (!alive) return;
const asset = r.assets[0];
if (!asset) {
setAssetTargetError(liveUI("screen.b125c86819e4a9", { v0: requestedAsset }));
return;
}
setAssetTarget({ id: asset.id, type: asset.type });
setTab(asset.type);
setPage(0);
setIntelligenceAsset(null);
})
.catch((e) => {
if (alive) setAssetTargetError((e as Error).message);
});
return () => {
alive = false;
};
}, [requestedAsset, liveUI]);
const [intelligenceAsset, setIntelligenceAsset] = React.useState<number | null>(null);
const [rows, setRows] = React.useState<Asset[]>([]);
const [total, setTotal] = React.useState(0);
const [companies, setCompanies] = React.useState<Company[]>([]);
const [counts, setCounts] = React.useState<Record<string, number>>({});
const [tab, setTab] = React.useState("company");
const [query, setQuery] = React.useState("");
const [loaded, setLoaded] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [page, setPage] = React.useState(0);
const [size, setSize] = React.useState(50);
const [dslError, setDslError] = React.useState("");
const [refreshKey, setRefreshKey] = React.useState(0);
const assetsRequestRef = React.useRef(0);
const [rowsTab, setRowsTab] = React.useState("");
// asset selection & delete
const [selected, setSelected] = React.useState<Set<number>>(new Set());
const [deleteIds, setDeleteIds] = React.useState<number[]>([]);
const [deleteOpen, setDeleteOpen] = React.useState(false);
const [deleting, setDeleting] = React.useState(false);
// company delete
const [companyDeleteTarget, setCompanyDeleteTarget] = React.useState<Company | null>(null);
const [companyDeleteAssets, setCompanyDeleteAssets] = React.useState(false);
const [companyDeleting, setCompanyDeleting] = React.useState(false);
// Companies + per-type counts (tab badges) — loaded on demand, no background polling.
const loadMeta = React.useCallback(() => {
api
.companies()
.then(setCompanies)
.catch(() => {
/* Keep the last successful company snapshot on a transient failure. */
});
api
.assetCounts()
.then(setCounts)
.catch(() => {
/* Keep the last successful counters on a transient failure. */
});
}, []);
// Manual refresh: reload counts/companies and re-fetch the current page.
const refresh = React.useCallback(() => {
loadMeta();
setRefreshKey((k) => k + 1);
setSelected(new Set());
}, [loadMeta]);
const toggleSelect = (id: number) => {
setSelected((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
};
const toggleSelectAll = (ids: number[]) => {
setSelected((prev) => {
const allSelected = ids.every((id) => prev.has(id));
const next = new Set(prev);
if (allSelected) {
ids.forEach((id) => {
next.delete(id);
});
} else {
ids.forEach((id) => {
next.add(id);
});
}
return next;
});
};
const openDelete = (ids: number[]) => {
setDeleteIds(ids);
setDeleteOpen(true);
};
const confirmDelete = async () => {
setDeleting(true);
try {
const res = await api.deleteAssets(deleteIds);
toast.success(ui("screen.1761c4798e77c2", { v0: res.deleted }));
setSelected(new Set());
refresh();
} catch (e) {
toast.error(ui("screen.0580fcee37e020") + String((e as Error)?.message ?? e));
} finally {
setDeleting(false);
setDeleteOpen(false);
}
};
const confirmDeleteCompany = async () => {
if (!companyDeleteTarget) return;
setCompanyDeleting(true);
try {
const res = await api.deleteCompany(companyDeleteTarget.id, companyDeleteAssets);
const msg =
companyDeleteAssets && res.assets_deleted > 0
? ui("screen.7ec8de519162d6", { v0: res.assets_deleted })
: ui("screen.525e545e0d0e2c");
toast.success(msg);
refresh();
} catch (e) {
toast.error(ui("screen.0580fcee37e020") + String((e as Error)?.message ?? e));
} finally {
setCompanyDeleting(false);
setCompanyDeleteTarget(null);
setCompanyDeleteAssets(false);
}
};
React.useEffect(() => {
loadMeta();
}, [loadMeta]);
// Reset query + page + selection when switching tabs
// biome-ignore lint/correctness/useExhaustiveDependencies: tab changes intentionally reset tab-local controls.
React.useEffect(() => {
setQuery(assetTarget?.type === tab ? `id==${assetTarget.id}` : "");
setDslError("");
setSelected(new Set());
setRows([]);
setRowsTab("");
setTotal(0);
setLoaded(false);
}, [tab, assetTarget]);
// biome-ignore lint/correctness/useExhaustiveDependencies: these controls intentionally reset server pagination.
React.useEffect(() => setPage(0), [tab, size, query]);
const dslMode = query.trim() !== "";
// Server-side paginated page loader for the active data tab (company tab excluded).
// biome-ignore lint/correctness/useExhaustiveDependencies: refreshKey is an explicit manual-reload trigger.
React.useEffect(() => {
if (tab === "company") return;
const request = ++assetsRequestRef.current;
const dsl = query.trim();
setLoading(true);
const offset = page * size;
const run = async () => {
try {
const r = dsl ? await api.searchAssets(dsl, tab, size, offset) : await api.assets(tab, size, offset);
if (assetsRequestRef.current !== request) return;
setRows(r.assets);
setRowsTab(tab);
setTotal(r.total);
setDslError("");
} catch (e) {
if (assetsRequestRef.current !== request) return;
setDslError(String((e as Error)?.message ?? e));
setRows([]);
setRowsTab(tab);
setTotal(0);
} finally {
if (assetsRequestRef.current === request) {
setLoading(false);
setLoaded(true);
}
}
};
const tid = setTimeout(run, dsl ? 400 : 0);
return () => {
clearTimeout(tid);
if (assetsRequestRef.current === request) assetsRequestRef.current++;
};
}, [tab, page, size, query, refreshKey]);
React.useEffect(() => {
if (tab === "company") return;
const lastPage = Math.max(0, Math.ceil(total / size) - 1);
if (page > lastPage) setPage(lastPage);
}, [page, size, tab, total]);
const companyById = React.useMemo(() => {
const m = new Map<number, string>();
for (const c of companies) m.set(c.id, c.name);
return m;
}, [companies]);
const companyName = (id?: number) => (id ? (companyById.get(id) ?? "") : "");
const tabCounts: Record<string, number> = { ...counts, company: companies.length };
const totalAssets = Object.values(counts).reduce((a, b) => a + b, 0);
// rows are already the current server-side page.
const tabData = (type: string): Asset[] => (rowsTab === type ? rows : []);
const currentRows = tabData(tab);
const slice = <T,>(list: T[]) => list;
const searchBox = (
<AssetDslSearch
query={query}
onChange={setQuery}
loading={loading}
error={dslError}
count={dslMode ? total : undefined}
/>
);
return (
<div className="flex h-[calc(100vh-6rem)] min-h-0 flex-col gap-4">
{assetTargetError ? (
<p role="alert" className="text-sm text-destructive">
{assetTargetError}
</p>
) : null}
<Dialog
open={intelligenceAsset !== null}
onOpenChange={(open) => {
if (!open) setIntelligenceAsset(null);
}}
>
<DialogContent className="sm:max-w-5xl">
<DialogHeader>
<DialogTitle>
{ui("screen.3fce7d9a221714")}
{intelligenceAsset} {ui("screen.15ec2ed14acbcf")}
</DialogTitle>
<DialogDescription>{ui("screen.7040a4dc910e39")}</DialogDescription>
</DialogHeader>
{intelligenceAsset !== null ? (
<IntelligencePanel
key={intelligenceAsset}
scope="assets"
scopeId={String(intelligenceAsset)}
onNavigate={() => setIntelligenceAsset(null)}
/>
) : null}
</DialogContent>
</Dialog>
<div className="flex items-center justify-between gap-4">
<div>
<h1 className="text-xl font-semibold tracking-tight">{ui("screen.5f45bb826b168f")}</h1>
</div>
<div className="flex items-center gap-3">
<span className="text-sm text-muted-foreground">
{ui("screen.76e547a8fa5463")}
<span className="tabular-nums">{totalAssets}</span> {ui("screen.a5670ed30b06da")}
</span>
{tab !== "company" ? (
<Button
variant="outline"
size="sm"
disabled={selected.size !== 1}
title={ui("screen.22f9f277d1232f")}
onClick={() => setIntelligenceAsset(Array.from(selected)[0])}
>
{ui("screen.66f5c497a338d1")}
</Button>
) : null}
{selected.size > 0 && (
<Button variant="destructive" size="sm" onClick={() => openDelete(Array.from(selected) as number[])}>
<Trash2Icon className="size-3.5" /> {ui("screen.4f879b6a4b1d4e")}
{selected.size})
</Button>
)}
<Button variant="outline" size="sm" onClick={refresh} disabled={loading}>
<RefreshCwIcon className={cn("size-4", loading && "animate-spin")} /> {ui("screen.aee88743413144")}
</Button>
<CompanyDialog onSaved={refresh} />
</div>
</div>
<Tabs value={tab} onValueChange={setTab} className="flex min-h-0 flex-1 flex-col gap-4">
<div className="overflow-x-auto overflow-y-hidden">
<TabsList variant="default">
{TABS(ui).map((t) => (
<TabsTrigger key={t.key} value={t.key}>
<t.icon className="size-3.5" />
{t.label}
<span className="ml-1 tabular-nums text-muted-foreground">{tabCounts[t.key]}</span>
</TabsTrigger>
))}
</TabsList>
</div>
{/* 企业 */}
<TabsContent value="company" className="mt-0 flex min-h-0 flex-1 flex-col">
<Card className="flex min-h-0 flex-1 flex-col overflow-hidden py-0">
<div className="min-h-0 flex-1 overflow-auto">
<Table>
<TableHeader className="sticky top-0 z-10 bg-card">
<TableRow>
<TableHead>{ui("screen.409d0719010a46")}</TableHead>
<TableHead className="w-24 text-right">{ui("screen.ae4728f0882849")}</TableHead>
<TableHead>{ui("screen.c3f497207f2e9c")}</TableHead>
<TableHead className="w-36 text-right">{ui("screen.ed31fbb483ee1b")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{companies.map((c) => (
<TableRow key={c.id}>
<TableCell>
<div className="flex items-center gap-2">
<CompanyAvatar name={c.name} logo={c.logo} />
<span className="font-medium">{c.name}</span>
</div>
</TableCell>
<TableCell className="text-right tabular-nums text-sm">{c.asset_count}</TableCell>
<TableCell>
{c.scope?.length ? (
<div className="flex flex-wrap gap-1">
{c.scope.map((s, i) => (
<Badge key={i} variant="secondary" className="font-mono text-[11px]">
{s.raw}
</Badge>
))}
</div>
) : (
<span className="text-xs text-muted-foreground">{ui("screen.8d66a21a659363")}</span>
)}
</TableCell>
<TableCell className="text-right">
<div className="flex items-center justify-end gap-1.5">
<EditScopeDialog company={c} onSaved={refresh} />
<AppendScopeDialog company={c} onSaved={refresh} />
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => {
setCompanyDeleteTarget(c);
setCompanyDeleteAssets(false);
}}
aria-label={ui("screen.c36a45833ff362", { v0: c.name })}
>
<Trash2Icon className="size-3.5" />
</Button>
</div>
</TableCell>
</TableRow>
))}
{companies.length === 0 && (
<TableRow>
<TableCell colSpan={4} className="py-10 text-center text-sm text-muted-foreground">
{ui("screen.1b38865e529a92")}
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
</Card>
</TabsContent>
{/* 根域名 */}
<TabsContent value="root_domain" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={["", ui("screen.222952431147e5"), ui("screen.b49f6a22e68943"), ui("screen.04423e6cdc8b75"), ""]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("root_domain")).map((a) => (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="font-mono text-xs font-medium">{a.domain}</TableCell>
<TableCell className="text-xs">{a.icp || "—"}</TableCell>
<TableCell className="text-xs">
{companyName(a.company_id) || (
<span className="text-muted-foreground">{ui("screen.96181a00e02657")}</span>
)}
</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.domain || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
))}
</AssetCard>
</TabsContent>
{/* IP */}
<TabsContent value="ip" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={["", "IP", ui("screen.ace9cc9aa062fe"), ui("screen.dd799f925f944f"), ui("screen.b4de56b75bf521"), ""]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("ip")).map((a) => (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="font-mono text-xs font-medium">{a.ip}</TableCell>
<TableCell className="font-mono text-xs">{a.c_segment || "—"}</TableCell>
<TableCell>
<Chips items={a.bound_domains ?? []} mono />
</TableCell>
<TableCell>
<Chips
items={(a.open_ports ?? []).map((p) => (p.service ? `${p.port}/${p.service}` : String(p.port)))}
mono
/>
</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.ip || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
))}
</AssetCard>
</TabsContent>
{/* 子域名 */}
<TabsContent value="subdomain" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={[
"",
ui("screen.222952431147e5"),
ui("screen.bc208f697acff9"),
ui("screen.d45e6a43ac7291"),
ui("screen.5632a6a4e5429f"),
"",
]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("subdomain")).map((a) => (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="font-mono text-xs font-medium">{a.domain}</TableCell>
<TableCell className="font-mono text-xs">{a.root_domain || "—"}</TableCell>
<TableCell className="text-xs">{a.record_type || "—"}</TableCell>
<TableCell className="max-w-xs truncate font-mono text-xs">
{(Array.isArray(a.record_value) ? a.record_value.join(", ") : a.record_value) || "—"}
</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.domain || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
))}
</AssetCard>
</TabsContent>
{/* 应用 */}
<TabsContent value="app" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={[
"",
ui("screen.c131777b2f3467"),
"Bundle ID",
ui("screen.515559957fd3ac"),
ui("screen.b49f6a22e68943"),
"",
]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("app")).map((a) => (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="text-xs font-medium">{a.app_name || "—"}</TableCell>
<TableCell className="font-mono text-xs">{a.bundle_id || "—"}</TableCell>
<TableCell className="text-xs">{a.category || "—"}</TableCell>
<TableCell className="text-xs">{a.app_icp || "—"}</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.app_name || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
))}
</AssetCard>
</TabsContent>
{/* 服务 */}
<TabsContent value="service" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={[
"",
ui("screen.ec309ab207ef7f"),
ui("screen.222952431147e5"),
"IP",
ui("screen.e71ac32b544b0e"),
ui("screen.06f7d73412128f"),
ui("screen.c3405f8c7d9d39"),
ui("screen.0d6a14a9ab254e"),
ui("screen.d2d648bd1c94b7"),
"",
]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("service")).map((a) => {
const isHttp = a.service_type === "http";
const svc = a.service_name || (isHttp ? "http" : "") || a.service_type || "";
let domainCell: React.ReactNode = "—";
if (isHttp && a.url) {
domainCell = (
<a
href={a.url}
target="_blank"
rel="noreferrer"
className="text-blue-600 hover:underline dark:text-blue-400"
>
{a.domain || a.url}
</a>
);
} else if (a.domain) {
domainCell = a.domain;
}
return (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="text-xs">
<Badge variant={isHttp ? "default" : "secondary"} className="font-mono text-[10px]">
{svc || "—"}
</Badge>
</TableCell>
<TableCell className="max-w-[14rem] truncate font-mono text-xs" title={a.domain || a.url}>
{domainCell}
</TableCell>
<TableCell className="font-mono text-xs">{a.ip || "—"}</TableCell>
<TableCell className="font-mono text-xs tabular-nums">{a.port || "—"}</TableCell>
<TableCell>
{a.status_code != null ? (
<span className={cn("font-mono text-xs font-semibold tabular-nums", statusTone(a.status_code))}>
{a.status_code}
</span>
) : (
"—"
)}
</TableCell>
<TableCell className="max-w-[12rem] truncate text-xs" title={a.page_title}>
{a.page_title || "—"}
</TableCell>
<TableCell>
<Chips items={a.technologies ?? []} />
</TableCell>
<TableCell>
{(a.auth ?? []).length === 0 ? (
<span className="text-xs text-muted-foreground">—</span>
) : (
(a.auth ?? []).map((authItem, i) => {
const item = authItem as Record<string, string>;
const label = item.type || item.username || ui("screen.d2d648bd1c94b7");
return (
<span key={i} className="inline-flex items-center gap-1 text-[11px]">
<KeyRoundIcon className="size-3 text-muted-foreground" />
<span className="font-mono">{label}</span>
</span>
);
})
)}
</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.url || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
);
})}
</AssetCard>
</TabsContent>
{/* 接口 */}
<TabsContent value="endpoint" className="mt-0 flex min-h-0 flex-1 flex-col gap-2">
{searchBox}
<AssetCard
cols={["", ui("screen.22b9f0b6621280"), ui("screen.cfd721ac9e2819"), ui("screen.9634fb0832be62"), ""]}
loaded={loaded}
total={total}
page={page}
size={size}
onSize={setSize}
onPage={setPage}
rows={currentRows}
selected={selected}
onToggleAll={(ids) => toggleSelectAll(ids)}
>
{slice(tabData("endpoint")).map((a) => (
<TableRow key={a.id} className={selected.has(a.id) ? "bg-muted/40" : undefined}>
<TableCell className="w-8 pr-0">
<Checkbox checked={selected.has(a.id)} onCheckedChange={() => toggleSelect(a.id)} />
</TableCell>
<TableCell className="w-16">
<MethodBadge method={a.method || ""} />
</TableCell>
<TableCell className="max-w-sm truncate font-mono text-xs" title={a.url}>
{a.url || "—"}
</TableCell>
<TableCell>
<Chips
items={(a.params ?? []).map((p) => {
const item = p as Record<string, string>;
return item.name ? `${item.name}(${item.location || "?"})` : "?";
})}
mono
/>
</TableCell>
<TableCell className="w-8 pl-0">
<Button
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-destructive"
onClick={() => openDelete([a.id])}
aria-label={ui("screen.2bf7036e5e76dc", { v0: a.url || a.id })}
>
<Trash2Icon className="size-3.5" />
</Button>
</TableCell>
</TableRow>
))}
</AssetCard>
</TabsContent>
</Tabs>
<AlertDialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>{ui("screen.a3ea3c17b401bd")}</AlertDialogTitle>
<AlertDialogDescription>
{ui("screen.cf89e2d397024a")}
<span className="font-semibold tabular-nums">{deleteIds.length}</span> {ui("screen.49a6675fda9172")}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={deleting}>{ui("screen.2cd0f3be8738a8")}</AlertDialogCancel>
<AlertDialogAction
onClick={(e) => {
e.preventDefault();
void confirmDelete();
}}
disabled={deleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{deleting ? ui("screen.5e8e7af57abbcb") : ui("screen.a3ea3c17b401bd")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
<AlertDialog
open={!!companyDeleteTarget}
onOpenChange={(o) => {
if (!o) {
setCompanyDeleteTarget(null);
setCompanyDeleteAssets(false);
}
}}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>
{ui("screen.06a9a4ee83c2ec")}
{companyDeleteTarget?.name}
</AlertDialogTitle>
<AlertDialogDescription asChild>
<div className="space-y-3">
<p>{ui("screen.3f76f4b32468a9")}</p>
<label
htmlFor="delete-assets-opt"
className="flex cursor-pointer items-center gap-2.5 rounded-md border p-3 hover:bg-muted/50"
>
<Checkbox
id="delete-assets-opt"
checked={companyDeleteAssets}
onCheckedChange={(v) => setCompanyDeleteAssets(!!v)}
/>
<span className="text-sm leading-snug">
{ui("screen.b742a8330d8adc")}
<span className="block text-xs text-muted-foreground">{ui("screen.0f7eabb425f308")}</span>
</span>
</label>
</div>
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={companyDeleting}>{ui("screen.2cd0f3be8738a8")}</AlertDialogCancel>
<AlertDialogAction
onClick={(e) => {
e.preventDefault();
void confirmDeleteCompany();
}}
disabled={companyDeleting}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
{companyDeleting ? ui("screen.5e8e7af57abbcb") : ui("screen.a3ea3c17b401bd")}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}
function AssetCard({
cols,
loaded,
total,
page,
size,
onSize,
onPage,
rows: dataRows,
selected,
onToggleAll,
children,
}: {
cols: string[];
loaded: boolean;
total: number;
page: number;
size: number;
onSize: React.Dispatch<React.SetStateAction<number>>;
onPage: React.Dispatch<React.SetStateAction<number>>;
rows?: Asset[];
selected?: Set<number>;
onToggleAll?: (ids: number[]) => void;
children: React.ReactNode;
}) {
const { ui } = useUITranslation();
const childRows = React.Children.toArray(children);
const pageCount = Math.max(1, Math.ceil(total / size));
const start = total === 0 ? 0 : page * size + 1;
const end = page * size + childRows.length;
const pageIds = dataRows?.map((r) => r.id) ?? [];
const allSelected = pageIds.length > 0 && selected != null && pageIds.every((id) => selected.has(id));
const someSelected = selected != null && pageIds.some((id) => selected.has(id));
return (
<Card className="flex min-h-0 flex-1 flex-col overflow-hidden py-0">
<div className="min-h-0 flex-1 overflow-auto scrollbar-thin scrollbar-track-transparent">
<Table>
<TableHeader className="sticky top-0 z-10 bg-card">
<TableRow>
{cols.map((c, i) =>
c === "" && i === 0 && onToggleAll ? (
<TableHead key={i} className="w-8 pr-0">
<Checkbox
checked={allSelected ? true : someSelected ? "indeterminate" : false}
onCheckedChange={() => onToggleAll(pageIds)}
/>
</TableHead>
) : (
<TableHead key={c + i}>{c}</TableHead>
),
)}
</TableRow>
</TableHeader>
<TableBody>
{childRows.length > 0 ? (
childRows
) : (
<TableRow>
<TableCell colSpan={cols.length} className="py-12 text-center text-sm text-muted-foreground">
{loaded ? ui("screen.ef3763ae265043") : ui("screen.4927a53bcc886a")}
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
{total > 0 && (
<div className="flex shrink-0 items-center gap-2 border-t px-3 py-1.5 text-xs text-muted-foreground">
<Select value={String(size)} onValueChange={(v) => onSize(Number(v))}>
<SelectTrigger size="sm" className="h-7 w-24">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{PAGE_SIZES.map((n) => (
<SelectItem key={n} value={String(n)}>
{n} {ui("screen.785e3cd53901f4")}
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
<span className="tabular-nums">
{start}–{end} / {total}
</span>
{pageCount > 1 && (
<div className="flex items-center gap-2">
<Button
variant="outline"
size="icon"
className="size-7"
disabled={page <= 0}
onClick={() => onPage((p) => Math.max(0, p - 1))}
>
<ChevronLeftIcon />
</Button>
<span className="tabular-nums">
{page + 1} / {pageCount}
</span>
<Button
variant="outline"
size="icon"
className="size-7"
disabled={page + 1 >= pageCount}
onClick={() => onPage((p) => Math.min(pageCount - 1, p + 1))}
>
<ChevronRightIcon />
</Button>
</div>
)}
</div>
)}
</Card>
);
}
function Chips({ items, mono }: { items: string[]; mono?: boolean }) {
const clean = items.filter(Boolean);
if (clean.length === 0) return <span className="text-xs text-muted-foreground">—</span>;
return (
<div className="flex flex-wrap gap-1">
{clean.map((s, i) => (
<Badge key={i} variant="outline" className={cn("text-[10px]", mono && "font-mono")}>
{s}
</Badge>
))}
</div>
);
}
function CompanyAvatar({ name, logo }: { name: string; logo?: string }) {
const initial = (name.trim()[0] ?? "?").toUpperCase();
return (
<Avatar className="size-7 shrink-0">
{logo ? <AvatarImage src={logo} alt={name} /> : null}
<AvatarFallback className="text-xs">{initial}</AvatarFallback>
</Avatar>
);
}
// 后端返回的 warnings 说的是既有数据问题(不是本次提交的行有错),保存本身已经
// 成功。给更长的停留时间,因为它需要用户去处理具体的资产,扫一眼标题不够。
function showScopeWarnings(warnings?: string[]) {
for (const warning of warnings ?? []) {
toast.warning(warning, { duration: 15000 });
}
}
function savedScopeRules(company: Company): CompanyScopeRule[] {
return (company.scope ?? [])
.map((scope) => ({ kind: scope.kind, value: scope.raw.trim() }))
.filter((scope) => scope.value);
}
function savedScopeText(company: Company): string {
return savedScopeRules(company)
.map((scope) => scope.value)
.join("\n");
}
// 新增企业使用与任务、LLM 编辑一致的右侧抽屉。
function CompanyDialog({ onSaved }: { onSaved: () => void }) {
const { ui } = useUITranslation();
const [open, setOpen] = React.useState(false);
const [name, setName] = React.useState("");
const [scopeText, setScopeText] = React.useState("");
const [busy, setBusy] = React.useState(false);
const parsedScope = React.useMemo(() => parseCompanyScopeText(scopeText), [scopeText]);
React.useEffect(() => {
if (!open) return;
setName("");
setScopeText("");
}, [open]);
const submit = async () => {
if (!name.trim()) {
toast.error(ui("screen.04c47542433fac"));
return;
}
if (parsedScope.errors.length > 0) {
toast.error(ui("screen.031baaffb78d07"));
return;
}
setBusy(true);
try {
const res = await api.createCompany(name.trim(), parsedScope.rules);
const added = res.scope_added ?? 0;
const invalid = res.scope_invalid ?? 0;
if (invalid > 0) toast.warning(ui("screen.2802702c973642", { v0: added, v1: invalid }));
else toast.success(ui("screen.f18ce3be548c06", { v0: added }));
setOpen(false);
onSaved();
} catch (e) {
const msg = String((e as Error)?.message ?? e);
if (/:\s*409$/.test(msg)) toast.error(ui("screen.55d0e12adaef50"));
else toast.error(ui("screen.20e1b0f2590bc7", { v0: msg }));
} finally {
setBusy(false);
}
};
return (
<Sheet open={open} onOpenChange={setOpen}>
<SheetTrigger asChild>
<Button size="sm">
<BuildingIcon data-icon="inline-start" /> {ui("screen.926e2875424e77")}
</Button>
</SheetTrigger>
<SheetContent className="w-full! max-w-none! gap-0 p-0 sm:w-[520px]! sm:max-w-[520px]!">
<SheetHeader className="border-b p-6">
<SheetTitle>{ui("screen.926e2875424e77")}</SheetTitle>
<SheetDescription>{ui("screen.087da716915bc9")}</SheetDescription>
</SheetHeader>
<div className="flex-1 overflow-y-auto p-6">
<FieldGroup>
<Field>
<FieldLabel htmlFor="cn-name">{ui("screen.69211530fe250a")}</FieldLabel>
<Input
id="cn-name"
placeholder={ui("screen.edc6705d84153c")}
value={name}
onChange={(e) => setName(e.target.value)}
/>
</Field>
<ScopeTextEditor id="cn-scope" value={scopeText} onValueChange={setScopeText} parsed={parsedScope} />
</FieldGroup>
</div>
<SheetFooter className="flex-row justify-end gap-2 border-t p-4">
<Button variant="outline" onClick={() => setOpen(false)} disabled={busy}>
{ui("screen.2cd0f3be8738a8")}
</Button>
<Button onClick={submit} disabled={busy || !name.trim() || parsedScope.errors.length > 0}>
{busy ? ui("screen.ff509c9ba052a2") : ui("screen.a3030bf8f16dc6")}
</Button>
</SheetFooter>
</SheetContent>
</Sheet>
);
}
// 编辑(覆盖)资产范围弹窗
function EditScopeDialog({ company, onSaved }: { company: Company; onSaved: () => void }) {
const { ui } = useUITranslation();
const [open, setOpen] = React.useState(false);
const [scopeText, setScopeText] = React.useState("");
const [reason, setReason] = React.useState("");
const [busy, setBusy] = React.useState(false);
const preservedScopeRules = React.useMemo(() => savedScopeRules(company), [company]);
const parsedScope = React.useMemo(
() => parseCompanyScopeText(scopeText, { preservedRules: preservedScopeRules }),
[preservedScopeRules, scopeText],
);
React.useEffect(() => {
if (!open) return;
setScopeText(savedScopeText(company));
setReason("");
}, [open, company]);
const submit = async () => {
if (parsedScope.errors.length > 0) {
toast.error(ui("screen.031baaffb78d07"));
return;
}
setBusy(true);
try {
const res = await api.updateCompanyScope(company.id, parsedScope.rules, reason);
const errCount = res.invalid ?? 0;
if (errCount > 0) toast.warning(ui("screen.da721b3eee9ce1", { v0: errCount }));
else toast.success(ui("screen.123e089ef2fa0a", { v0: res.added }));
showScopeWarnings(res.warnings);
setOpen(false);
onSaved();
} catch (e) {
toast.error(ui("screen.20e1b0f2590bc7", { v0: String((e as Error)?.message ?? e) }));
} finally {
setBusy(false);
}
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm" className="h-7">
{ui("screen.051836569928a9")}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>
{ui("screen.db6fafa66f2df7")}
{company.name}
</DialogTitle>
<DialogDescription>{ui("screen.514634cb134223")}</DialogDescription>
</DialogHeader>
<FieldGroup className="py-2">
<ScopeTextEditor
id={`edit-company-scope-${company.id}`}
value={scopeText}
onValueChange={setScopeText}
parsed={parsedScope}
/>
<Field>
<FieldLabel htmlFor="es-reason">{ui("screen.6f74197f3bdddf")}</FieldLabel>
<Input
id="es-reason"
placeholder={ui("screen.f3f53abf207e35")}
value={reason}
onChange={(e) => setReason(e.target.value)}
/>
</Field>
</FieldGroup>
<DialogFooter>
<Button variant="outline" onClick={() => setOpen(false)} disabled={busy}>
{ui("screen.2cd0f3be8738a8")}
</Button>
<Button onClick={submit} disabled={busy || parsedScope.errors.length > 0}>
{busy ? ui("screen.ff509c9ba052a2") : ui("screen.f72279f15f551c")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
// 追加资产范围弹窗
function AppendScopeDialog({ company, onSaved }: { company: Company; onSaved: () => void }) {
const { ui } = useUITranslation();
const [open, setOpen] = React.useState(false);
const [scopeText, setScopeText] = React.useState("");
const [reason, setReason] = React.useState("");
const [busy, setBusy] = React.useState(false);
const parsedScope = React.useMemo(() => parseCompanyScopeText(scopeText), [scopeText]);
React.useEffect(() => {
if (!open) return;
setScopeText("");
setReason("");
}, [open]);
const submit = async () => {
if (parsedScope.rules.length === 0) {
toast.error(ui("screen.e71265f94553c7"));
return;
}
if (parsedScope.errors.length > 0) {
toast.error(ui("screen.031baaffb78d07"));
return;
}
setBusy(true);
try {
const res = await api.addCompanyScope(company.id, parsedScope.rules, reason);
const errCount = res.invalid ?? 0;
if (errCount > 0) toast.warning(ui("screen.da721b3eee9ce1", { v0: errCount }));
else toast.success(ui("screen.5a718e3167fc5a", { v0: res.added }));
showScopeWarnings(res.warnings);
setOpen(false);
onSaved();
} catch (e) {
toast.error(ui("screen.20e1b0f2590bc7", { v0: String((e as Error)?.message ?? e) }));
} finally {
setBusy(false);
}
};
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm" className="h-7">
{ui("screen.d91017c932b5fc")}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>
{ui("screen.3653428c258c3e")}
{company.name}
</DialogTitle>
<DialogDescription>{ui("screen.98412bfb06851c")}</DialogDescription>
</DialogHeader>
<FieldGroup className="py-2">
<ScopeTextEditor
id={`append-company-scope-${company.id}`}
value={scopeText}
onValueChange={setScopeText}
parsed={parsedScope}
/>
<Field>
<FieldLabel htmlFor="as-reason">{ui("screen.6f74197f3bdddf")}</FieldLabel>
<Input
id="as-reason"
placeholder={ui("screen.f3f53abf207e35")}
value={reason}
onChange={(e) => setReason(e.target.value)}
/>
</Field>
</FieldGroup>
<DialogFooter>
<Button variant="outline" onClick={() => setOpen(false)} disabled={busy}>
{ui("screen.2cd0f3be8738a8")}
</Button>
<Button onClick={submit} disabled={busy || parsedScope.rules.length === 0 || parsedScope.errors.length > 0}>
{busy ? ui("screen.ff509c9ba052a2") : ui("screen.d91017c932b5fc")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
export default function AssetsPage() {
return (
<React.Suspense fallback={null}>
<AssetsContent />
</React.Suspense>
);
}