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("已复制到剪贴板!")
} }
+62 -70
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,11 +130,32 @@ 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={() => {
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"> <ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
<div className="items-center mx-1"> <div className="items-center mx-1">
<DialogHeader> <DialogHeader>
@@ -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>
</>
) )
} }
+8 -3
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,7 +43,8 @@ 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", id: "select",
header: ({ table }) => ( header: ({ table }) => (
@@ -150,7 +153,9 @@ export default function ServerPage() {
) )
}, },
}, },
] ],
[t, mutate, serverGroups],
)
const dataCache = useMemo(() => { const dataCache = useMemo(() => {
return data ?? [] return data ?? []