feat: optimize server management and fix visual config sync

This commit is contained in:
Bot
2026-05-11 17:35:06 +08:00
parent 2da8565e47
commit ec38164aff
3 changed files with 200 additions and 194 deletions
+15 -6
View File
@@ -782,7 +782,7 @@
<button onclick="generatePublicNoteJSON()">生成 JSON</button> <button onclick="generatePublicNoteJSON()">生成 JSON</button>
</div> </div>
<div class="col-right"> <div class="col-right">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput')"> <button class="copy-btn" onclick="copyToClipboard('jsonOutput', this)">
复制到剪贴板 复制到剪贴板
</button> </button>
<pre id="jsonOutput"><code class="language-json"></code></pre> <pre id="jsonOutput"><code class="language-json"></code></pre>
@@ -889,7 +889,7 @@
<button onclick="generateTrafficMonitorJSON()">生成 JSON</button> <button onclick="generateTrafficMonitorJSON()">生成 JSON</button>
</div> </div>
<div class="col-right"> <div class="col-right">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput2')"> <button class="copy-btn" onclick="copyToClipboard('jsonOutput2', this)">
复制到剪贴板 复制到剪贴板
</button> </button>
<pre id="jsonOutput2"><code class="language-json"></code></pre> <pre id="jsonOutput2"><code class="language-json"></code></pre>
@@ -976,20 +976,29 @@
} }
} }
function copyToClipboard(elementId) { function copyToClipboard(elementId, btn) {
const urlParams = new URLSearchParams(window.location.search);
const targetParam = urlParams.get('target');
const oldText = btn ? btn.innerHTML : null;
var text = document.querySelector("#" + elementId + " code").innerText var text = document.querySelector("#" + elementId + " code").innerText
navigator.clipboard.writeText(text).then(function () { navigator.clipboard.writeText(text).then(function () {
if (window.opener) { if (window.opener) {
window.opener.postMessage( window.opener.postMessage(
{ {
type: "NZCFG_JSON", type: "NZCFG_JSON",
target: elementId === "jsonOutput" ? "public_note" : "traffic", target: targetParam || (elementId === "jsonOutput" ? "public_note" : "traffic"),
payload: text, payload: text,
}, },
"*", "*",
) )
alert("已生成并同步至控制面板中!") if (btn) {
window.close() btn.innerHTML = '<i class="fas fa-check"></i> 已同步至控制面板';
btn.style.backgroundColor = '#2ecc71';
}
setTimeout(() => {
window.close();
}, 600);
} else { } else {
alert("已复制到剪贴板!") alert("已复制到剪贴板!")
} }
+75 -83
View File
@@ -95,9 +95,12 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
useEffect(() => { useEffect(() => {
const handleMessage = (e: MessageEvent) => { const handleMessage = (e: MessageEvent) => {
if (e.data?.type === "NZCFG_JSON") { if (e.data?.type === "NZCFG_JSON") {
if (e.data.target === "public_note") { const target = e.data.target === "traffic" ? "public_note" : e.data.target
form.setValue("public_note", e.data.payload) if (target === "public_note" || target === "note") {
toast(t("Success"), { description: "配置已通过可视化构建器自动填入" }) form.setValue(target, e.data.payload)
toast(t("Success"), {
description: `配置已自动填入${target === "public_note" ? t("PublicNote.Label") : t("Private") + t("Note")}`,
})
} }
} }
} }
@@ -127,24 +130,45 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
} }
return ( return (
<Dialog open={open} onOpenChange={setOpen}> <>
<DialogTrigger asChild> <IconButton
<IconButton variant="outline" icon="edit" /> variant="outline"
</DialogTrigger> icon="edit"
<DialogContent className="sm:max-w-xl"> onClick={() => {
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3"> setOpen(true)
<div className="items-center mx-1"> }}
<DialogHeader> />
<DialogTitle>{t("EditServer")}</DialogTitle> <Dialog
<DialogDescription /> open={open}
</DialogHeader> onOpenChange={(val) => {
<Form {...(form as any)}> setOpen(val)
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2"> }}
<FormField >
control={form.control as any} <DialogContent
name="name" className="sm:max-w-xl"
render={({ field }) => ( onPointerDownOutside={(e) => {
<FormItem> e.preventDefault()
}}
onInteractOutside={(e) => {
e.preventDefault()
}}
onFocusOutside={(e) => {
e.preventDefault()
}}
>
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
<div className="items-center mx-1">
<DialogHeader>
<DialogTitle>{t("EditServer")}</DialogTitle>
<DialogDescription />
</DialogHeader>
<Form {...(form as any)}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
<FormField
control={form.control as any}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>{t("Name")}</FormLabel> <FormLabel>{t("Name")}</FormLabel>
<FormControl> <FormControl>
<Input placeholder="My Server" {...field} /> <Input placeholder="My Server" {...field} />
@@ -248,7 +272,26 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
name="note" name="note"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel>{t("Private") + t("Note")}</FormLabel> <FormLabel className="flex justify-between items-center w-full">
<span>{t("Private") + t("Note")}</span>
<Button
variant="link"
type="button"
className="text-blue-500 hover:text-blue-700 text-xs flex items-center gap-1 h-auto p-0"
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
window.open(
"/dashboard/nzcfg.html?target=note",
"nzcfg",
"width=1000,height=800",
)
}}
>
{" "}
<i className="fa-solid fa-up-right-from-square"></i>
</Button>
</FormLabel>
<FormControl> <FormControl>
<Textarea className="resize-none" {...field} /> <Textarea className="resize-none" {...field} />
</FormControl> </FormControl>
@@ -256,53 +299,7 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</FormItem> </FormItem>
)} )}
/> />
<div className="p-3 border rounded-md border-dashed space-y-2">
<Label className="text-xs text-muted-foreground uppercase font-bold">
Billing & Expiry
</Label>
<FormField
control={form.control as any}
name="billing_data.registrar"
render={({ field }) => (
<FormItem>
<FormLabel>Registrar</FormLabel>
<FormControl>
<Input
placeholder="AWS / Azure /阿里云"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control as any}
name="billing_data.endDate"
render={({ field }) => (
<FormItem>
<FormLabel>Expiry Date</FormLabel>
<FormControl>
<Input
type="date"
{...field}
value={field.value?.split("T")[0] || ""}
onChange={(e) =>
field.onChange(
e.target.value
? new Date(
e.target.value,
).toISOString()
: "",
)
}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField <FormField
control={form.control as any} control={form.control as any}
name="public_note" name="public_note"
@@ -310,29 +307,23 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
<FormItem> <FormItem>
<FormLabel className="flex justify-between items-center w-full"> <FormLabel className="flex justify-between items-center w-full">
<span>{t("Public") + t("Note")}</span> <span>{t("Public") + t("Note")}</span>
<a <Button
href="/dashboard/nzcfg.html" variant="link"
target="_blank" type="button"
className="text-blue-500 hover:text-blue-700 text-xs flex items-center gap-1" className="text-blue-500 hover:text-blue-700 text-xs flex items-center gap-1 h-auto p-0"
onClick={(e) => { onClick={(e) => {
e.preventDefault() e.preventDefault()
const popup = window.open( e.stopPropagation()
"/dashboard/nzcfg.html", window.open(
"/dashboard/nzcfg.html?target=public_note",
"nzcfg", "nzcfg",
"width=1000,height=800", "width=1000,height=800",
) )
if (popup) {
const timer = setInterval(() => {
if (popup.closed) {
clearInterval(timer)
}
}, 500)
}
}} }}
> >
{" "} {" "}
<i className="fa-solid fa-up-right-from-square"></i> <i className="fa-solid fa-up-right-from-square"></i>
</a> </Button>
</FormLabel> </FormLabel>
<FormControl> <FormControl>
<Textarea className="resize-y" {...field} /> <Textarea className="resize-y" {...field} />
@@ -357,5 +348,6 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</ScrollArea> </ScrollArea>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</>
) )
} }
+110 -105
View File
@@ -30,7 +30,9 @@ import useSWR from "swr"
export default function ServerPage() { export default function ServerPage() {
const { t } = useTranslation() const { t } = useTranslation()
const { data, mutate, error, isLoading } = useSWR<Server[]>("/api/v1/server", swrFetcher) const { data, mutate, error, isLoading } = useSWR<Server[]>("/api/v1/server", swrFetcher, {
revalidateOnFocus: false,
})
const { serverGroups } = useServer() const { serverGroups } = useServer()
useEffect(() => { useEffect(() => {
@@ -41,116 +43,119 @@ export default function ServerPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [error]) }, [error])
const columns: ColumnDef<Server>[] = [ const columns = useMemo<ColumnDef<Server>[]>(
{ () => [
id: "select", {
header: ({ table }) => ( id: "select",
<Checkbox header: ({ table }) => (
checked={ <Checkbox
table.getIsAllPageRowsSelected() || checked={
(table.getIsSomePageRowsSelected() && "indeterminate") table.getIsAllPageRowsSelected() ||
} (table.getIsSomePageRowsSelected() && "indeterminate")
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)} }
aria-label="Select all" onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
/> aria-label="Select all"
), />
cell: ({ row }) => ( ),
<Checkbox cell: ({ row }) => (
checked={row.getIsSelected()} <Checkbox
onCheckedChange={(value) => row.toggleSelected(!!value)} checked={row.getIsSelected()}
aria-label="Select row" onCheckedChange={(value) => row.toggleSelected(!!value)}
/> aria-label="Select row"
), />
enableSorting: false, ),
enableHiding: false, enableSorting: false,
}, enableHiding: false,
{
header: "ID",
accessorKey: "id",
accessorFn: (row) => `${row.id}(${row.display_index})`,
},
{
header: t("Name"),
accessorKey: "name",
accessorFn: (row) => row.name,
cell: ({ row }) => {
const s = row.original
return <div className="max-w-24 whitespace-normal break-words">{s.name}</div>
}, },
}, {
{ header: "ID",
header: t("Group"), accessorKey: "id",
accessorKey: "groups", accessorFn: (row) => `${row.id}(${row.display_index})`,
accessorFn: (row) => {
return (
serverGroups
?.filter((sg) => sg.servers?.includes(row.id!))
.map((sg) => sg.group.id) || []
)
}, },
}, {
{ header: t("Name"),
id: "ip", accessorKey: "name",
header: "IP", accessorFn: (row) => row.name,
cell: ({ row }) => { cell: ({ row }) => {
const s = row.original const s = row.original
return ( return <div className="max-w-24 whitespace-normal break-words">{s.name}</div>
<div className="max-w-24 whitespace-normal break-words"> },
{joinIP(s.geoip?.ip)}
</div>
)
}, },
}, {
{ header: t("Group"),
header: t("Version"), accessorKey: "groups",
accessorKey: "host.version", accessorFn: (row) => {
accessorFn: (row) => row.host?.version || t("Unknown"), return (
}, serverGroups
{ ?.filter((sg) => sg.servers?.includes(row.id!))
header: t("EnableDDNS"), .map((sg) => sg.group.id) || []
accessorKey: "enableDDNS", )
accessorFn: (row) => row.enable_ddns ?? false, },
},
{
header: t("HideForGuest"),
accessorKey: "hideForGuest",
accessorFn: (row) => row.hide_for_guest ?? false,
},
{
id: "note",
header: t("Note"),
cell: ({ row }) => {
const s = row.original
return <NoteMenu note={{ private: s.note, public: s.public_note }} />
}, },
}, {
{ id: "ip",
id: "uuid", header: "IP",
header: "UUID", cell: ({ row }) => {
cell: ({ row }) => { const s = row.original
const s = row.original return (
return <CopyButton text={s.uuid} /> <div className="max-w-24 whitespace-normal break-words">
{joinIP(s.geoip?.ip)}
</div>
)
},
}, },
}, {
{ header: t("Version"),
id: "actions", accessorKey: "host.version",
header: t("Actions"), accessorFn: (row) => row.host?.version || t("Unknown"),
cell: ({ row }) => {
const s = row.original
return (
<ActionButtonGroup
className="flex gap-2"
delete={{ fn: deleteServer, id: s.id!, mutate: mutate }}
>
<>
<ServerCard mutate={mutate} data={s} />
<ServerConfigCard sid={s.id!} variant="outline" />
</>
</ActionButtonGroup>
)
}, },
}, {
] header: t("EnableDDNS"),
accessorKey: "enableDDNS",
accessorFn: (row) => row.enable_ddns ?? false,
},
{
header: t("HideForGuest"),
accessorKey: "hideForGuest",
accessorFn: (row) => row.hide_for_guest ?? false,
},
{
id: "note",
header: t("Note"),
cell: ({ row }) => {
const s = row.original
return <NoteMenu note={{ private: s.note, public: s.public_note }} />
},
},
{
id: "uuid",
header: "UUID",
cell: ({ row }) => {
const s = row.original
return <CopyButton text={s.uuid} />
},
},
{
id: "actions",
header: t("Actions"),
cell: ({ row }) => {
const s = row.original
return (
<ActionButtonGroup
className="flex gap-2"
delete={{ fn: deleteServer, id: s.id!, mutate: mutate }}
>
<>
<ServerCard mutate={mutate} data={s} />
<ServerConfigCard sid={s.id!} variant="outline" />
</>
</ActionButtonGroup>
)
},
},
],
[t, mutate, serverGroups],
)
const dataCache = useMemo(() => { const dataCache = useMemo(() => {
return data ?? [] return data ?? []