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>
</div>
<div class="col-right">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput')">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput', this)">
复制到剪贴板
</button>
<pre id="jsonOutput"><code class="language-json"></code></pre>
@@ -889,7 +889,7 @@
<button onclick="generateTrafficMonitorJSON()">生成 JSON</button>
</div>
<div class="col-right">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput2')">
<button class="copy-btn" onclick="copyToClipboard('jsonOutput2', this)">
复制到剪贴板
</button>
<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
navigator.clipboard.writeText(text).then(function () {
if (window.opener) {
window.opener.postMessage(
{
type: "NZCFG_JSON",
target: elementId === "jsonOutput" ? "public_note" : "traffic",
target: targetParam || (elementId === "jsonOutput" ? "public_note" : "traffic"),
payload: text,
},
"*",
)
alert("已生成并同步至控制面板中!")
window.close()
if (btn) {
btn.innerHTML = '<i class="fas fa-check"></i> 已同步至控制面板';
btn.style.backgroundColor = '#2ecc71';
}
setTimeout(() => {
window.close();
}, 600);
} else {
alert("已复制到剪贴板!")
}
+62 -70
View File
@@ -95,9 +95,12 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
useEffect(() => {
const handleMessage = (e: MessageEvent) => {
if (e.data?.type === "NZCFG_JSON") {
if (e.data.target === "public_note") {
form.setValue("public_note", e.data.payload)
toast(t("Success"), { description: "配置已通过可视化构建器自动填入" })
const target = e.data.target === "traffic" ? "public_note" : e.data.target
if (target === "public_note" || target === "note") {
form.setValue(target, e.data.payload)
toast(t("Success"), {
description: `配置已自动填入${target === "public_note" ? t("PublicNote.Label") : t("Private") + t("Note")}`,
})
}
}
}
@@ -127,11 +130,32 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<IconButton variant="outline" icon="edit" />
</DialogTrigger>
<DialogContent className="sm:max-w-xl">
<>
<IconButton
variant="outline"
icon="edit"
onClick={() => {
setOpen(true)
}}
/>
<Dialog
open={open}
onOpenChange={(val) => {
setOpen(val)
}}
>
<DialogContent
className="sm:max-w-xl"
onPointerDownOutside={(e) => {
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>
@@ -248,7 +272,26 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
name="note"
render={({ field }) => (
<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>
<Textarea className="resize-none" {...field} />
</FormControl>
@@ -256,53 +299,7 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</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
control={form.control as any}
name="public_note"
@@ -310,29 +307,23 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
<FormItem>
<FormLabel className="flex justify-between items-center w-full">
<span>{t("Public") + t("Note")}</span>
<a
href="/dashboard/nzcfg.html"
target="_blank"
className="text-blue-500 hover:text-blue-700 text-xs flex items-center gap-1"
<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()
const popup = window.open(
"/dashboard/nzcfg.html",
e.stopPropagation()
window.open(
"/dashboard/nzcfg.html?target=public_note",
"nzcfg",
"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>
</a>
</Button>
</FormLabel>
<FormControl>
<Textarea className="resize-y" {...field} />
@@ -357,5 +348,6 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</ScrollArea>
</DialogContent>
</Dialog>
</>
)
}
+8 -3
View File
@@ -30,7 +30,9 @@ import useSWR from "swr"
export default function ServerPage() {
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()
useEffect(() => {
@@ -41,7 +43,8 @@ export default function ServerPage() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [error])
const columns: ColumnDef<Server>[] = [
const columns = useMemo<ColumnDef<Server>[]>(
() => [
{
id: "select",
header: ({ table }) => (
@@ -150,7 +153,9 @@ export default function ServerPage() {
)
},
},
]
],
[t, mutate, serverGroups],
)
const dataCache = useMemo(() => {
return data ?? []