feat: improve notification settings and interactive TG bot

This commit is contained in:
Bot
2026-05-13 00:52:29 +08:00
parent bc242ab64b
commit 340fb61a61
8 changed files with 435 additions and 128 deletions
+227
View File
@@ -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
View File
@@ -49,9 +49,9 @@ interface NotifierCardProps {
const notificationFormSchema = z.object({ const notificationFormSchema = z.object({
name: z.string().min(1), name: z.string().min(1),
url: z.string().min(1), url: z.string().default(""),
request_method: z.coerce.number().int().min(1).max(255), request_method: z.coerce.number().int().default(1),
request_type: z.coerce.number().int().min(1).max(255), request_type: z.coerce.number().int().default(1),
request_header: z.string(), request_header: z.string(),
request_body: z.string(), request_body: z.string(),
verify_tls: z.boolean().default(false), verify_tls: z.boolean().default(false),
@@ -162,42 +162,33 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
</FormControl> </FormControl>
<SelectContent> <SelectContent>
<SelectItem value="1">Webhook</SelectItem> <SelectItem value="1">Webhook</SelectItem>
<SelectItem value="2">SMTP (Email)</SelectItem> <SelectItem value="2">Email (Global)</SelectItem>
<SelectItem value="3">Telegram</SelectItem> <SelectItem value="3">Telegram (Global)</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
)} )}
/> />
<FormField {form.watch("type") == 1 && (
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 && (
<> <>
<FormField
control={form.control}
name="url"
render={({ field }) => (
<FormItem>
<FormLabel>URL</FormLabel>
<FormControl>
<Input
{...field}
placeholder="https://..."
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField <FormField
control={form.control} control={form.control}
name="request_method" name="request_method"
@@ -256,30 +247,35 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
</FormItem> </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 <FormField
control={form.control} control={form.control}
name="request_header" name="request_body"
render={({ field }) => ( render={({ field }) => (
<FormItem> <FormItem>
<FormLabel> <FormLabel>{t("RequestBody")}</FormLabel>
{form.watch("type") == 2
? "SMTP User:Pass"
: form.watch("type") == 3
? "Chat ID"
: t("RequestHeader")}
</FormLabel>
<FormControl> <FormControl>
<Textarea <Textarea
className="resize-y" className="resize-y h-[240px]"
placeholder={ placeholder="..."
form.watch("type") == 2
? "user:pass"
: form.watch("type") == 3
? "123456789"
: '{"User-Agent":"Nezha-Agent"}'
}
{...field} {...field}
/> />
</FormControl> </FormControl>
@@ -287,59 +283,30 @@ export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
</FormItem> </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"> <div className="pt-4 border-t space-y-3">
<Label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider"> <Label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
{t("AdvancedSettings")} {t("AdvancedSettings")}
</Label> </Label>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-2"> <div className="grid grid-cols-1 sm:grid-cols-2 gap-x-4 gap-y-2">
<FormField {form.watch("type") == 1 && (
control={form.control} <FormField
name="verify_tls" control={form.control}
render={({ field }) => ( name="verify_tls"
<FormItem className="flex items-center space-x-2 space-y-0 py-1"> render={({ field }) => (
<FormControl> <FormItem className="flex items-center space-x-2 space-y-0 py-1">
<Checkbox <FormControl>
checked={field.value} <Checkbox
onCheckedChange={field.onChange} checked={field.value}
/> onCheckedChange={field.onChange}
</FormControl> />
<FormLabel className="text-sm font-normal cursor-pointer"> </FormControl>
{t("VerifyTLS")} <FormLabel className="text-sm font-normal cursor-pointer">
</FormLabel> {t("VerifyTLS")}
</FormItem> </FormLabel>
)} </FormItem>
/> )}
/>
)}
<FormField <FormField
control={form.control} control={form.control}
name="skip_check" name="skip_check"
-1
View File
@@ -9,7 +9,6 @@ import {
DialogFooter, DialogFooter,
DialogHeader, DialogHeader,
DialogTitle, DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog" } from "@/components/ui/dialog"
import { import {
Form, Form,
+3
View File
@@ -153,6 +153,9 @@
"IPChangeNotification": "IP Change Notification", "IPChangeNotification": "IP Change Notification",
"IPChangeNotificationGroupID": "IP Change Notification Group ID", "IPChangeNotificationGroupID": "IP Change Notification Group ID",
"ExpiryNotificationGroupID": "Expiry Notification Group ID", "ExpiryNotificationGroupID": "Expiry Notification Group ID",
"ExpiryNotification": "Expiry Notification",
"DomainNotificationDays": "Domain Notification Days",
"ServerNotificationDays": "Server Notification Days",
"FullIPNotification": "Show Full IP Address in Notification Messages", "FullIPNotification": "Show Full IP Address in Notification Messages",
"EditService": "Edit Service", "EditService": "Edit Service",
"CreateService": "Create Service", "CreateService": "Create Service",
+3
View File
@@ -160,6 +160,9 @@
"IPChangeNotification": "IP 变更通知", "IPChangeNotification": "IP 变更通知",
"IPChangeNotificationGroupID": "IP 变更通知组 ID", "IPChangeNotificationGroupID": "IP 变更通知组 ID",
"ExpiryNotificationGroupID": "到期通知组 ID", "ExpiryNotificationGroupID": "到期通知组 ID",
"ExpiryNotification": "到期提醒设置",
"DomainNotificationDays": "域名到期提醒天数",
"ServerNotificationDays": "VPS到期提醒天数",
"FullIPNotification": "在通知消息中显示完整的 IP 地址", "FullIPNotification": "在通知消息中显示完整的 IP 地址",
"LoginFailed": "登录失败", "LoginFailed": "登录失败",
"BruteForceAttackingToken": "暴力攻击令牌", "BruteForceAttackingToken": "暴力攻击令牌",
+5 -1
View File
@@ -121,7 +121,11 @@ const router = createBrowserRouter([
}, },
{ {
path: "/dashboard/settings", path: "/dashboard/settings",
element: <SettingsPage />, element: (
<NotificationProvider withNotifierGroup>
<SettingsPage />
</NotificationProvider>
),
}, },
{ {
path: "/dashboard/settings/user", path: "/dashboard/settings/user",
+126 -30
View File
@@ -59,6 +59,13 @@ const settingFormSchema = z.object({
background_image_night: asOptionalField(z.string()), background_image_night: asOptionalField(z.string()),
telegram_bot_token: asOptionalField(z.string()), telegram_bot_token: asOptionalField(z.string()),
telegram_admin_chat_id: asOptionalField(z.string()), telegram_admin_chat_id: asOptionalField(z.string()),
smtp_server: asOptionalField(z.string()),
smtp_user: asOptionalField(z.string()),
smtp_password: asOptionalField(z.string()),
admin_email: asOptionalField(z.string()),
domain_expiry_notification_days: asOptionalField(z.string()),
server_expiry_notification_days: asOptionalField(z.string()),
expiry_notification_group_id: z.coerce.number().int().min(0),
}) })
export default function SettingsPage() { export default function SettingsPage() {
@@ -95,6 +102,9 @@ export default function SettingsPage() {
site_name: "", site_name: "",
language: "", language: "",
user_template: "user-dist", user_template: "user-dist",
expiry_notification_group_id: 0,
domain_expiry_notification_days: "",
server_expiry_notification_days: "",
}, },
resetOptions: { resetOptions: {
keepDefaultValues: false, keepDefaultValues: false,
@@ -133,36 +143,6 @@ export default function SettingsPage() {
<div> <div>
<Form {...form}> <Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2"> <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
<FormField
control={form.control}
name="ip_change_notification_group_id"
render={({ field }) => (
<FormItem>
<FormLabel>{t("IPChangeNotificationGroupID")}</FormLabel>
<FormControl>
<Input type="number" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="expiry_notification_group_id"
render={({ field }) => (
<FormItem>
<FormLabel>Expiry Notification Group ID</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Enter Group ID"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField <FormField
control={form.control} control={form.control}
name="site_name" name="site_name"
@@ -279,6 +259,58 @@ export default function SettingsPage() {
</FormItem> </FormItem>
)} )}
/> />
<FormField
control={form.control}
name="smtp_server"
render={({ field }) => (
<FormItem>
<FormLabel>SMTP Server (host:port)</FormLabel>
<FormControl>
<Input placeholder="smtp.example.com:465" {...field} value={field.value as string || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="smtp_user"
render={({ field }) => (
<FormItem>
<FormLabel>SMTP User</FormLabel>
<FormControl>
<Input placeholder="user@example.com" {...field} value={field.value as string || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="smtp_password"
render={({ field }) => (
<FormItem>
<FormLabel>SMTP Password</FormLabel>
<FormControl>
<Input type="password" placeholder="••••••••" {...field} value={field.value as string || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="admin_email"
render={({ field }) => (
<FormItem>
<FormLabel>Admin Email (Recipient)</FormLabel>
<FormControl>
<Input type="email" placeholder="admin@example.com" {...field} value={field.value as string || ""} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField <FormField
control={form.control} control={form.control}
name="language" name="language"
@@ -540,6 +572,70 @@ export default function SettingsPage() {
</FormItem> </FormItem>
)} )}
/> />
<FormItem>
<FormLabel>{t("ExpiryNotification")}</FormLabel>
<Card className="w-full">
<CardContent>
<div className="flex flex-col space-y-4 mt-4">
<FormField
control={form.control}
name="expiry_notification_group_id"
render={({ field }) => (
<FormItem>
<FormLabel>{t("NotificationGroup")}</FormLabel>
<Combobox
options={ngroupList}
defaultValue={`${field.value}`}
onValueChange={field.onChange}
/>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="domain_expiry_notification_days"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("DomainNotificationDays") +
" " +
t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input
placeholder="60,30,15,7,3,1,0"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="server_expiry_notification_days"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("ServerNotificationDays") +
" " +
t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input
placeholder="30,15,7,3,1,0"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</CardContent>
</Card>
</FormItem>
<FormItem> <FormItem>
<FormLabel>{t("IPChangeNotification")}</FormLabel> <FormLabel>{t("IPChangeNotification")}</FormLabel>
<Card className="w-full"> <Card className="w-full">
+8
View File
@@ -696,6 +696,10 @@ export interface ModelSetting {
background_image_night?: string background_image_night?: string
telegram_bot_token?: string telegram_bot_token?: string
telegram_admin_chat_id?: string telegram_admin_chat_id?: string
smtp_server?: string
smtp_user?: string
smtp_password?: string
admin_email?: string
} }
export interface ModelSettingForm { export interface ModelSettingForm {
@@ -731,6 +735,10 @@ export interface ModelSettingForm {
background_image_night?: string background_image_night?: string
telegram_bot_token?: string telegram_bot_token?: string
telegram_admin_chat_id?: string telegram_admin_chat_id?: string
smtp_server?: string
smtp_user?: string
smtp_password?: string
admin_email?: string
} }
export interface ModelSettingResponse { export interface ModelSettingResponse {