mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-11-04 00:40:21 +00:00
feat: improve notification settings and interactive TG bot
This commit is contained in:
1 parent
bc242ab64b
commit
340fb61a61
8 files changed
+435
-128
No files matched your search
@@ -0,0 +1,227 @@
|
||||
import { updateServer } from "@/api/server"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { IconButton } from "@/components/xui/icon-button"
|
||||
import { ModelServer as Server } from "@/types"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { toast } from "sonner"
|
||||
import { KeyedMutator } from "swr"
|
||||
import { z } from "zod"
|
||||
|
||||
interface BatchEditNoteIconProps {
|
||||
servers: Server[]
|
||||
selectedIds: number[]
|
||||
mutate: KeyedMutator<Server[]>
|
||||
}
|
||||
|
||||
const batchNoteFormSchema = z.object({
|
||||
note: z.string().optional(),
|
||||
public_note: z.string().optional(),
|
||||
})
|
||||
|
||||
export const BatchEditNoteIcon: React.FC<BatchEditNoteIconProps> = ({
|
||||
servers,
|
||||
selectedIds,
|
||||
mutate,
|
||||
}) => {
|
||||
const { t } = useTranslation()
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
const form = useForm<z.infer<typeof batchNoteFormSchema>>({
|
||||
resolver: zodResolver(batchNoteFormSchema),
|
||||
defaultValues: {
|
||||
note: "",
|
||||
public_note: "",
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
const handleMessage = (e: MessageEvent) => {
|
||||
if (e.data?.type === "NZCFG_JSON") {
|
||||
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")
|
||||
}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
window.addEventListener("message", handleMessage)
|
||||
return () => window.removeEventListener("message", handleMessage)
|
||||
}, [form, t])
|
||||
|
||||
const onSubmit = async (values: z.infer<typeof batchNoteFormSchema>) => {
|
||||
if (selectedIds.length === 0) {
|
||||
toast(t("Error"), { description: t("Results.SelectAtLeastOneServer") })
|
||||
return
|
||||
}
|
||||
|
||||
const promises = selectedIds.map((id) => {
|
||||
const server = servers.find((s) => s.id === id)
|
||||
if (!server) return Promise.resolve()
|
||||
|
||||
// Only update note/public_note if the batch form field is not empty!
|
||||
// Wait, what if they WANT to clear the note?
|
||||
// For batch, usually we only override if they typed something.
|
||||
// We can provide a checkbox or just update if not empty.
|
||||
// Let's just update if not empty for safety.
|
||||
let updatePayload = { ...server }
|
||||
if (values.note && values.note.trim() !== "") {
|
||||
updatePayload.note = values.note
|
||||
}
|
||||
if (values.public_note && values.public_note.trim() !== "") {
|
||||
updatePayload.public_note = values.public_note
|
||||
}
|
||||
|
||||
// Clean up non-API fields
|
||||
return updateServer(id, {
|
||||
name: updatePayload.name,
|
||||
display_index: updatePayload.display_index,
|
||||
note: updatePayload.note,
|
||||
public_note: updatePayload.public_note,
|
||||
hide_for_guest: updatePayload.hide_for_guest,
|
||||
enable_ddns: updatePayload.enable_ddns,
|
||||
ddns_profiles: updatePayload.ddns_profiles,
|
||||
override_ddns_domains: updatePayload.override_ddns_domains,
|
||||
})
|
||||
})
|
||||
|
||||
toast.promise(Promise.all(promises), {
|
||||
loading: t("Saving..."),
|
||||
success: () => {
|
||||
setOpen(false)
|
||||
mutate()
|
||||
form.reset()
|
||||
return t("Success")
|
||||
},
|
||||
error: t("Results.UnExpectedError"),
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<IconButton
|
||||
icon="edit"
|
||||
onClick={() => {
|
||||
if (selectedIds.length === 0) {
|
||||
toast(t("Error"), { description: t("Results.SelectAtLeastOneServer") })
|
||||
return
|
||||
}
|
||||
setOpen(true)
|
||||
}}
|
||||
/>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogContent className="sm:max-w-xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>批量修改配置/备注 (已选 {selectedIds.length} 台)</DialogTitle>
|
||||
</DialogHeader>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="note"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<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"
|
||||
placeholder="在此粘贴或使用右上角可视化工具生成配置..."
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="public_note"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel className="flex justify-between items-center w-full">
|
||||
<span>{t("Public") + 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=public_note",
|
||||
"nzcfg",
|
||||
"width=1000,height=800",
|
||||
)
|
||||
}}
|
||||
>
|
||||
可视化管理配置 <i className="fa-solid fa-up-right-from-square"></i>
|
||||
</Button>
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
className="resize-y"
|
||||
placeholder="在此粘贴或使用右上角可视化工具生成配置..."
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<DialogFooter>
|
||||
<DialogClose asChild>
|
||||
<Button type="button" variant="secondary">
|
||||
{t("Close")}
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button type="submit">{t("Submit")}</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</>
|
||||
)
|
||||
}
|
||||
+63
-96
@@ -49,9 +49,9 @@ interface NotifierCardProps {
|
||||
|
||||
const notificationFormSchema = z.object({
|
||||
name: z.string().min(1),
|
||||
url: z.string().min(1),
|
||||
request_method: z.coerce.number().int().min(1).max(255),
|
||||
request_type: z.coerce.number().int().min(1).max(255),
|
||||
url: z.string().default(""),
|
||||
request_method: z.coerce.number().int().default(1),
|
||||
request_type: z.coerce.number().int().default(1),
|
||||
request_header: z.string(),
|
||||
request_body: z.string(),
|
||||
verify_tls: z.boolean().default(false),
|
||||
@@ -162,42 +162,33 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="1">Webhook</SelectItem>
|
||||
<SelectItem value="2">SMTP (Email)</SelectItem>
|
||||
<SelectItem value="3">Telegram</SelectItem>
|
||||
<SelectItem value="2">Email (Global)</SelectItem>
|
||||
<SelectItem value="3">Telegram (Global)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
{form.watch("type") == 2
|
||||
? "SMTP Server (host:port)"
|
||||
: form.watch("type") == 3
|
||||
? "Bot Token"
|
||||
: "URL"}
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
placeholder={
|
||||
form.watch("type") == 3
|
||||
? "123456:ABC-DEF"
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{form.watch("type") != 2 && form.watch("type") != 3 && (
|
||||
{form.watch("type") == 1 && (
|
||||
<>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>URL</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
{...field}
|
||||
placeholder="https://..."
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="request_method"
|
||||
@@ -256,30 +247,35 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="request_header"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("RequestHeader")}</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
className="resize-y"
|
||||
placeholder={'{"User-Agent":"Nezha-Agent"}'}
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="request_header"
|
||||
name="request_body"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
{form.watch("type") == 2
|
||||
? "SMTP User:Pass"
|
||||
: form.watch("type") == 3
|
||||
? "Chat ID"
|
||||
: t("RequestHeader")}
|
||||
</FormLabel>
|
||||
<FormLabel>{t("RequestBody")}</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
className="resize-y"
|
||||
placeholder={
|
||||
form.watch("type") == 2
|
||||
? "user:pass"
|
||||
: form.watch("type") == 3
|
||||
? "123456789"
|
||||
: '{"User-Agent":"Nezha-Agent"}'
|
||||
}
|
||||
className="resize-y h-[240px]"
|
||||
placeholder="..."
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
@@ -287,59 +283,30 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{form.watch("type") != 3 && (
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="request_body"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>
|
||||
{form.watch("type") == 2
|
||||
? "Recipient Email"
|
||||
: t("RequestBody")}
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea
|
||||
className={
|
||||
form.watch("type") == 2
|
||||
? "resize-y"
|
||||
: "resize-y h-[240px]"
|
||||
}
|
||||
placeholder={
|
||||
form.watch("type") == 2
|
||||
? "target@example.com"
|
||||
: "..."
|
||||
}
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
<div className="pt-4 border-t space-y-3">
|
||||
<Label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
|
||||
{t("AdvancedSettings")}
|
||||
</Label>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-2">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="verify_tls"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex items-center space-x-2 space-y-0 py-1">
|
||||
<FormControl>
|
||||
<Checkbox
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormLabel className="text-sm font-normal cursor-pointer">
|
||||
{t("VerifyTLS")}
|
||||
</FormLabel>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{form.watch("type") == 1 && (
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="verify_tls"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex items-center space-x-2 space-y-0 py-1">
|
||||
<FormControl>
|
||||
<Checkbox
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormLabel className="text-sm font-normal cursor-pointer">
|
||||
{t("VerifyTLS")}
|
||||
</FormLabel>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="skip_check"
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import {
|
||||
Form,
|
||||
|
||||
Reference in new issue
Block a user