Download ARTEX-main/web/src/app/(main)/function/sync/page.tsx from ghostai1/WardenPEN: direct link, hf CLI and curl.
- Browser
- Download file 18.4 kB
-
https://huggingface.co/ghostai1/WardenPEN/resolve/main/ARTEX-main/web/src/app/(main)/function/sync/page.tsx
- Command line
-
hf download 'hf://ghostai1/WardenPEN/ARTEX-main/web/src/app/(main)/function/sync/page.tsx'
-
curl -L -o page.tsx 'https://huggingface.co/ghostai1/WardenPEN/resolve/main/ARTEX-main/web/src/app/(main)/function/sync/page.tsx'
18.4 kB
| "use client"; | |
| import * as React from "react"; | |
| import { AlertCircleIcon, CheckCircle2Icon, DownloadIcon, PlugZapIcon, RefreshCwIcon, SearchIcon } from "lucide-react"; | |
| import { toast } from "sonner"; | |
| import { Badge } from "@/components/ui/badge"; | |
| import { Button } from "@/components/ui/button"; | |
| import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; | |
| import { Checkbox } from "@/components/ui/checkbox"; | |
| import { Input } from "@/components/ui/input"; | |
| import { Label } from "@/components/ui/label"; | |
| import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; | |
| import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; | |
| import { api } from "@/lib/api"; | |
| import type { SSProject, SSTask } from "@/lib/types"; | |
| import { t } from "@/i18n"; | |
| type SSStatus = { | |
| exists: boolean; | |
| configured: boolean; | |
| enabled: boolean; | |
| reachable: boolean; | |
| url?: string; | |
| tools: string[]; | |
| }; | |
| type Dimension = "project" | "task"; | |
| const ASSET_TYPES: { key: string; label: string }[] = [ | |
| { key: "subdomain", label: t("Subdomain") }, | |
| { key: "service", label: t("Service") }, | |
| { key: "app", label: t("App") }, | |
| ]; | |
| export default function AssetSyncPage() { | |
| return ( | |
| <div className="p-4 md:p-6"> | |
| <div className="mb-4"> | |
| <h1 className="font-semibold text-xl">{t("Asset sync")}</h1> | |
| <p className="text-muted-foreground text-sm">{t("Sync assets into the store from external data sources")}</p> | |
| </div> | |
| <Tabs defaultValue="scopesentry"> | |
| <TabsList> | |
| <TabsTrigger value="scopesentry">ScopeSentry</TabsTrigger> | |
| </TabsList> | |
| <TabsContent value="scopesentry" className="mt-4"> | |
| <ScopeSentryPanel /> | |
| </TabsContent> | |
| </Tabs> | |
| </div> | |
| ); | |
| } | |
| function ScopeSentryPanel() { | |
| const [status, setStatus] = React.useState<SSStatus | null>(null); | |
| const [loadingStatus, setLoadingStatus] = React.useState(true); | |
| const loadStatus = React.useCallback(() => { | |
| setLoadingStatus(true); | |
| api | |
| .ssStatus() | |
| .then(setStatus) | |
| .catch((e) => toast.error(t("Failed to read data source status: {error}", { error: e.message }))) | |
| .finally(() => setLoadingStatus(false)); | |
| }, []); | |
| React.useEffect(() => { | |
| loadStatus(); | |
| }, [loadStatus]); | |
| const ready = !!status && status.exists && status.configured && status.enabled; | |
| return ( | |
| <div className="space-y-4"> | |
| <DataSourceCard status={status} loading={loadingStatus} onChanged={loadStatus} /> | |
| {ready ? ( | |
| <SyncWorkbench /> | |
| ) : ( | |
| <Card> | |
| <CardContent className="py-8 text-center text-muted-foreground text-sm"> | |
| {t("Once the data source is ready, you can pick projects / tasks to sync.")} | |
| </CardContent> | |
| </Card> | |
| )} | |
| </div> | |
| ); | |
| } | |
| // ββ Data source status card βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| function DataSourceCard({ | |
| status, | |
| loading, | |
| onChanged, | |
| }: { | |
| status: SSStatus | null; | |
| loading: boolean; | |
| onChanged: () => void; | |
| }) { | |
| const [url, setUrl] = React.useState(""); | |
| const [apiKey, setApiKey] = React.useState(""); | |
| const [busy, setBusy] = React.useState(false); | |
| React.useEffect(() => { | |
| if (status?.url) setUrl(status.url); | |
| }, [status?.url]); | |
| const create = async () => { | |
| setBusy(true); | |
| try { | |
| await api.ssDatasource({}); | |
| toast.success(t("ScopeSentry data source created; fill in the URL and key")); | |
| onChanged(); | |
| } catch (e) { | |
| toast.error(t("Create failed: {error}", { error: (e as Error).message })); | |
| } finally { | |
| setBusy(false); | |
| } | |
| }; | |
| const save = async () => { | |
| if (!url.trim()) return toast.error(t("Please enter the MCP URL")); | |
| setBusy(true); | |
| try { | |
| const r = await api.ssDatasource({ url: url.trim(), api_key: apiKey.trim() }); | |
| toast.success(r.enabled ? t("Data source saved and enabled") : t("Saved (enable conditions not yet met)")); | |
| setApiKey(""); | |
| onChanged(); | |
| } catch (e) { | |
| toast.error(t("Save failed: {error}", { error: (e as Error).message })); | |
| } finally { | |
| setBusy(false); | |
| } | |
| }; | |
| return ( | |
| <Card> | |
| <CardHeader className="flex flex-row items-center justify-between"> | |
| <CardTitle className="flex items-center gap-2 text-base"> | |
| <PlugZapIcon className="size-4" /> {t("Data source status")} | |
| <StatusBadge status={status} loading={loading} /> | |
| </CardTitle> | |
| <Button variant="ghost" size="sm" onClick={onChanged} disabled={loading}> | |
| <RefreshCwIcon className={loading ? "size-4 animate-spin" : "size-4"} /> {t("Refresh")} | |
| </Button> | |
| </CardHeader> | |
| <CardContent className="space-y-3"> | |
| {!status?.exists ? ( | |
| <div className="flex items-center justify-between gap-4"> | |
| <p className="text-muted-foreground text-sm"> | |
| {t("No ScopeSentry data source yet. Creating one adds a placeholder MCP (empty URL/key, disabled).")} | |
| </p> | |
| <Button onClick={create} disabled={busy}> | |
| {t("Create data source")} | |
| </Button> | |
| </div> | |
| ) : ( | |
| <> | |
| {!status.configured && ( | |
| <p className="text-amber-600 text-sm dark:text-amber-500"> | |
| {t("The data source is created but not configured; fill in the MCP URL and API key, then enable it.")} | |
| </p> | |
| )} | |
| {status.configured && !status.enabled && ( | |
| <p className="text-amber-600 text-sm dark:text-amber-500"> | |
| {t("The data source is configured but not enabled; it will be enabled automatically on save.")} | |
| </p> | |
| )} | |
| <div className="grid gap-3 md:grid-cols-2"> | |
| <div className="space-y-1.5"> | |
| <Label>MCP URL</Label> | |
| <Input placeholder="http://<host>:8082/mcp" value={url} onChange={(e) => setUrl(e.target.value)} /> | |
| </div> | |
| <div className="space-y-1.5"> | |
| <Label>{t("API Key (X-API-Key; leave empty to keep the current value)")}</Label> | |
| <Input | |
| type="password" | |
| placeholder="ssk_..." | |
| value={apiKey} | |
| onChange={(e) => setApiKey(e.target.value)} | |
| /> | |
| </div> | |
| </div> | |
| <div className="flex items-center gap-2"> | |
| <Button onClick={save} disabled={busy}> | |
| {t("Save & enable")} | |
| </Button> | |
| {status.enabled && status.tools.length > 0 && ( | |
| <span className="text-muted-foreground text-xs"> | |
| {t("{count} tools discovered", { count: status.tools.length })} | |
| </span> | |
| )} | |
| </div> | |
| </> | |
| )} | |
| </CardContent> | |
| </Card> | |
| ); | |
| } | |
| function StatusBadge({ status, loading }: { status: SSStatus | null; loading: boolean }) { | |
| if (loading || !status) return <Badge variant="secondary">{t("Checkingβ¦")}</Badge>; | |
| if (!status.exists) return <Badge variant="destructive">{t("Not created")}</Badge>; | |
| if (!status.configured) return <Badge variant="outline">{t("Not configured")}</Badge>; | |
| if (!status.enabled) return <Badge variant="outline">{t("Not enabled")}</Badge>; | |
| if (status.reachable) | |
| return ( | |
| <Badge className="bg-emerald-600 hover:bg-emerald-600"> | |
| <CheckCircle2Icon className="mr-1 size-3" /> {t("Connected")} | |
| </Badge> | |
| ); | |
| return ( | |
| <Badge variant="destructive"> | |
| <AlertCircleIcon className="mr-1 size-3" /> {t("Unreachable")} | |
| </Badge> | |
| ); | |
| } | |
| // ββ Sync workbench (project / task dimension) ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| function SyncWorkbench() { | |
| const [dimension, setDimension] = React.useState<Dimension>("project"); | |
| const [assetTypes, setAssetTypes] = React.useState<Record<string, boolean>>({ | |
| subdomain: true, | |
| service: true, | |
| app: true, | |
| }); | |
| const [createCompany, setCreateCompany] = React.useState(true); | |
| const [projects, setProjects] = React.useState<SSProject[]>([]); | |
| const [tasks, setTasks] = React.useState<SSTask[]>([]); | |
| const [loading, setLoading] = React.useState(false); | |
| const [search, setSearch] = React.useState(""); | |
| const [page, setPage] = React.useState(1); | |
| const [selected, setSelected] = React.useState<Set<string>>(new Set()); | |
| const [syncing, setSyncing] = React.useState(false); | |
| const [result, setResult] = React.useState<Awaited<ReturnType<typeof api.ssSync>> | null>(null); | |
| const load = React.useCallback(() => { | |
| setLoading(true); | |
| setSelected(new Set()); | |
| const fn = | |
| dimension === "project" | |
| ? api.ssProjects(page, 50, search).then((r) => setProjects(r.projects)) | |
| : api.ssTasks(page, 50, search).then(setTasks); | |
| fn.catch((e) => toast.error(t("Failed to load the list: {error}", { error: e.message }))).finally(() => | |
| setLoading(false), | |
| ); | |
| }, [dimension, page, search]); | |
| React.useEffect(() => { | |
| load(); | |
| }, [load]); | |
| const rows = dimension === "project" ? projects : tasks; | |
| const idOf = (row: SSProject | SSTask) => (dimension === "project" ? (row as SSProject).id : (row as SSTask).name); | |
| const toggle = (id: string) => { | |
| setSelected((prev) => { | |
| const next = new Set(prev); | |
| if (next.has(id)) next.delete(id); | |
| else next.add(id); | |
| return next; | |
| }); | |
| }; | |
| const toggleAll = () => { | |
| setSelected((prev) => (prev.size === rows.length ? new Set() : new Set(rows.map(idOf)))); | |
| }; | |
| const chosenTypes = ASSET_TYPES.filter((t) => assetTypes[t.key]).map((t) => t.key); | |
| const runSync = async () => { | |
| if (selected.size === 0) | |
| return toast.error(dimension === "project" ? t("Select at least one project") : t("Select at least one task")); | |
| if (chosenTypes.length === 0) return toast.error(t("Select at least one asset type")); | |
| setSyncing(true); | |
| setResult(null); | |
| try { | |
| const r = await api.ssSync({ | |
| dimension, | |
| targets: [...selected], | |
| asset_types: chosenTypes, | |
| create_company: dimension === "project" ? createCompany : false, | |
| }); | |
| setResult(r); | |
| const total = Object.values(r.synced ?? {}).reduce((a, b) => a + b, 0); | |
| toast.success(t("Sync finished; {count} assets ingested in total", { count: total })); | |
| } catch (e) { | |
| toast.error(t("Sync failed: {error}", { error: (e as Error).message })); | |
| } finally { | |
| setSyncing(false); | |
| } | |
| }; | |
| const renderRows = () => { | |
| if (loading) { | |
| return ( | |
| <TableRow> | |
| <TableCell colSpan={4} className="py-8 text-center text-muted-foreground text-sm"> | |
| {t("Loadingβ¦")} | |
| </TableCell> | |
| </TableRow> | |
| ); | |
| } | |
| if (rows.length === 0) { | |
| return ( | |
| <TableRow> | |
| <TableCell colSpan={4} className="py-8 text-center text-muted-foreground text-sm"> | |
| {t("No data")} | |
| </TableCell> | |
| </TableRow> | |
| ); | |
| } | |
| if (dimension === "project") { | |
| return projects.map((p) => ( | |
| <TableRow key={p.id} className="cursor-pointer" onClick={() => toggle(p.id)}> | |
| <TableCell onClick={(e) => e.stopPropagation()}> | |
| <Checkbox checked={selected.has(p.id)} onCheckedChange={() => toggle(p.id)} /> | |
| </TableCell> | |
| <TableCell className="font-medium">{p.name}</TableCell> | |
| <TableCell>{p.tag ? <Badge variant="secondary">{p.tag}</Badge> : "β"}</TableCell> | |
| <TableCell className="text-right">{p.AssetCount ?? 0}</TableCell> | |
| </TableRow> | |
| )); | |
| } | |
| return tasks.map((t) => ( | |
| <TableRow key={t.id} className="cursor-pointer" onClick={() => toggle(t.name)}> | |
| <TableCell onClick={(e) => e.stopPropagation()}> | |
| <Checkbox checked={selected.has(t.name)} onCheckedChange={() => toggle(t.name)} /> | |
| </TableCell> | |
| <TableCell className="font-medium">{t.name}</TableCell> | |
| <TableCell> | |
| <Badge variant={t.progress === 100 ? "secondary" : "outline"}> | |
| {t.progress != null ? `${t.progress}%` : "β"} | |
| </Badge> | |
| </TableCell> | |
| <TableCell className="text-muted-foreground text-xs">{t.endTime || t.creatTime || "β"}</TableCell> | |
| </TableRow> | |
| )); | |
| }; | |
| return ( | |
| <Card> | |
| <CardHeader> | |
| <CardTitle className="text-base">{t("Pick data to sync")}</CardTitle> | |
| </CardHeader> | |
| <CardContent className="space-y-4"> | |
| {/* Dimension switch */} | |
| <Tabs | |
| value={dimension} | |
| onValueChange={(v) => { | |
| setDimension(v as Dimension); | |
| setPage(1); | |
| }} | |
| > | |
| <TabsList> | |
| <TabsTrigger value="project">{t("By project")}</TabsTrigger> | |
| <TabsTrigger value="task">{t("By task")}</TabsTrigger> | |
| </TabsList> | |
| </Tabs> | |
| {/* Asset types + options */} | |
| <div className="flex flex-wrap items-center gap-4"> | |
| <span className="font-medium text-sm">{t("Assets to sync:")}</span> | |
| {ASSET_TYPES.map((t) => ( | |
| <label key={t.key} htmlFor={`at-${t.key}`} className="flex items-center gap-1.5 text-sm"> | |
| <Checkbox | |
| id={`at-${t.key}`} | |
| checked={!!assetTypes[t.key]} | |
| onCheckedChange={(c) => setAssetTypes((prev) => ({ ...prev, [t.key]: !!c }))} | |
| /> | |
| {t.label} | |
| </label> | |
| ))} | |
| {dimension === "project" && ( | |
| <label htmlFor="create-company" className="flex items-center gap-1.5 text-sm"> | |
| <Checkbox id="create-company" checked={createCompany} onCheckedChange={(c) => setCreateCompany(!!c)} /> | |
| {t("Create a company per project and write its asset scope")} | |
| </label> | |
| )} | |
| </div> | |
| {/* Search + actions */} | |
| <div className="flex items-center gap-2"> | |
| <div className="relative max-w-xs flex-1"> | |
| <SearchIcon className="absolute top-1/2 left-2 size-4 -translate-y-1/2 text-muted-foreground" /> | |
| <Input | |
| className="pl-8" | |
| placeholder={dimension === "project" ? t("Search project names") : t("Search task names")} | |
| value={search} | |
| onChange={(e) => setSearch(e.target.value)} | |
| onKeyDown={(e) => { | |
| if (e.key === "Enter") { | |
| setPage(1); | |
| load(); | |
| } | |
| }} | |
| /> | |
| </div> | |
| <Button variant="outline" size="sm" onClick={load} disabled={loading}> | |
| <RefreshCwIcon className={loading ? "size-4 animate-spin" : "size-4"} /> | |
| </Button> | |
| <div className="flex-1" /> | |
| <span className="text-muted-foreground text-xs">{t("{count} selected", { count: selected.size })}</span> | |
| <Button onClick={runSync} disabled={syncing || selected.size === 0}> | |
| <DownloadIcon className={syncing ? "size-4 animate-pulse" : "size-4"} /> {t("Sync selected")} | |
| </Button> | |
| </div> | |
| {/* List */} | |
| <div className="rounded-md border"> | |
| <Table> | |
| <TableHeader> | |
| <TableRow> | |
| <TableHead className="w-10"> | |
| <Checkbox checked={rows.length > 0 && selected.size === rows.length} onCheckedChange={toggleAll} /> | |
| </TableHead> | |
| <TableHead>{dimension === "project" ? t("Project name") : t("Task name")}</TableHead> | |
| {dimension === "project" ? ( | |
| <> | |
| <TableHead>{t("Tag")}</TableHead> | |
| <TableHead className="text-right">{t("Assets")}</TableHead> | |
| </> | |
| ) : ( | |
| <> | |
| <TableHead>{t("Status")}</TableHead> | |
| <TableHead>{t("Time")}</TableHead> | |
| </> | |
| )} | |
| </TableRow> | |
| </TableHeader> | |
| <TableBody>{renderRows()}</TableBody> | |
| </Table> | |
| </div> | |
| {/* Pagination */} | |
| <div className="flex items-center justify-end gap-2"> | |
| <Button variant="outline" size="sm" disabled={page <= 1 || loading} onClick={() => setPage((p) => p - 1)}> | |
| {t("Previous")} | |
| </Button> | |
| <span className="text-muted-foreground text-xs">{t("Page {page}", { page })}</span> | |
| <Button | |
| variant="outline" | |
| size="sm" | |
| disabled={rows.length < 50 || loading} | |
| onClick={() => setPage((p) => p + 1)} | |
| > | |
| {t("Next")} | |
| </Button> | |
| </div> | |
| {/* Result */} | |
| {result && <SyncResult result={result} />} | |
| </CardContent> | |
| </Card> | |
| ); | |
| } | |
| function SyncResult({ result }: { result: Awaited<ReturnType<typeof api.ssSync>> }) { | |
| const synced = result.synced ?? {}; | |
| const labels: Record<string, string> = { subdomain: t("Subdomain"), service: t("Service"), app: t("App"), ip: "IP" }; | |
| return ( | |
| <div className="space-y-2 rounded-md border bg-muted/40 p-3 text-sm"> | |
| <div className="flex flex-wrap gap-3"> | |
| {Object.entries(synced).map(([k, v]) => ( | |
| <Badge key={k} variant="secondary"> | |
| {labels[k] ?? k}: {v} | |
| </Badge> | |
| ))} | |
| </div> | |
| {result.companies && result.companies.length > 0 && ( | |
| <p className="text-muted-foreground"> | |
| {t("Companies created/updated: {names}", { names: result.companies.join(", ") })} | |
| </p> | |
| )} | |
| {result.warnings && result.warnings.length > 0 && ( | |
| <ul className="list-inside list-disc text-amber-600 dark:text-amber-500"> | |
| {result.warnings.map((wm) => ( | |
| <li key={wm}>{wm}</li> | |
| ))} | |
| </ul> | |
| )} | |
| {result.errors && result.errors.length > 0 && ( | |
| <ul className="list-inside list-disc text-destructive"> | |
| {result.errors.slice(0, 20).map((em) => ( | |
| <li key={em}>{em}</li> | |
| ))} | |
| {result.errors.length > 20 && <li>{t("β¦ {count} errors in total", { count: result.errors.length })}</li>} | |
| </ul> | |
| )} | |
| </div> | |
| ); | |
| } | |