mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-05-06 05:38:51 +00:00
358 lines
18 KiB
TypeScript
358 lines
18 KiB
TypeScript
import { createNotification, updateNotification } from "@/api/notification"
|
|
import { Button } from "@/components/ui/button"
|
|
import { Checkbox } from "@/components/ui/checkbox"
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/components/ui/dialog"
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/ui/form"
|
|
import { Input } from "@/components/ui/input"
|
|
import { Label } from "@/components/ui/label"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select"
|
|
import { IconButton } from "@/components/xui/icon-button"
|
|
import { asOptionalField } from "@/lib/utils"
|
|
import { ModelNotification } from "@/types"
|
|
import { nrequestMethods, nrequestTypes } from "@/types"
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
import { 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"
|
|
|
|
import { Textarea } from "./ui/textarea"
|
|
|
|
interface NotifierCardProps {
|
|
data?: ModelNotification
|
|
mutate: KeyedMutator<ModelNotification[]>
|
|
}
|
|
|
|
const notificationFormSchema = z.object({
|
|
name: z.string().min(1),
|
|
url: z.string().url(),
|
|
request_method: z.coerce.number().int().min(1).max(255),
|
|
request_type: z.coerce.number().int().min(1).max(255),
|
|
request_header: z.string(),
|
|
request_body: z.string(),
|
|
verify_tls: asOptionalField(z.boolean()),
|
|
skip_check: asOptionalField(z.boolean()),
|
|
format_metric_units: asOptionalField(z.boolean()),
|
|
type: z.coerce.number().int().default(1),
|
|
})
|
|
|
|
export const NotifierCard: React.FC<NotifierCardProps> = ({ data, mutate }) => {
|
|
const { t } = useTranslation()
|
|
type notificationFormData = z.infer<typeof notificationFormSchema>
|
|
|
|
const form = useForm({
|
|
resolver: zodResolver(notificationFormSchema) as any,
|
|
defaultValues: data
|
|
? {
|
|
name: data.name ?? "",
|
|
url: data.url ?? "",
|
|
request_method: data.request_method ?? 1,
|
|
request_type: data.request_type ?? 1,
|
|
request_header: data.request_header ?? "",
|
|
request_body: data.request_body ?? "",
|
|
verify_tls: (data as any).verify_tls ?? false,
|
|
skip_check: (data as any).skip_check ?? false,
|
|
format_metric_units: (data as any).format_metric_units ?? false,
|
|
type: data.type ?? 1,
|
|
}
|
|
: {
|
|
name: "",
|
|
url: "",
|
|
request_method: 1,
|
|
request_type: 1,
|
|
request_header: "",
|
|
request_body: "",
|
|
verify_tls: false,
|
|
skip_check: false,
|
|
format_metric_units: false,
|
|
type: 1,
|
|
},
|
|
resetOptions: {
|
|
keepDefaultValues: false,
|
|
},
|
|
})
|
|
|
|
const [open, setOpen] = useState(false)
|
|
|
|
const onSubmit = async (values: notificationFormData) => {
|
|
try {
|
|
data?.id ? await updateNotification(data.id, values) : await createNotification(values)
|
|
} catch (e) {
|
|
console.error(e)
|
|
toast(t("Error"), {
|
|
description: t("Results.UnExpectedError"),
|
|
})
|
|
return
|
|
}
|
|
setOpen(false)
|
|
await mutate()
|
|
form.reset()
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
<DialogTrigger asChild>
|
|
{data ? <IconButton variant="outline" icon="edit" /> : <IconButton icon="plus" />}
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-xl">
|
|
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
|
|
<div className="items-center mx-1">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{data ? t("EditNotifier") : t("CreateNotifier")}
|
|
</DialogTitle>
|
|
<DialogDescription />
|
|
</DialogHeader>
|
|
<Form {...form}>
|
|
<form
|
|
onSubmit={form.handleSubmit(onSubmit as any)}
|
|
className="space-y-2 my-2"
|
|
>
|
|
<FormField
|
|
control={form.control}
|
|
name="name"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Name")}</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="My Notifier" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="type"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Notification Type</FormLabel>
|
|
<Select
|
|
onValueChange={field.onChange}
|
|
value={`${field.value}`}
|
|
>
|
|
<FormControl>
|
|
<SelectTrigger>
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
</FormControl>
|
|
<SelectContent>
|
|
<SelectItem value="1">Webhook</SelectItem>
|
|
<SelectItem value="2">SMTP (Email)</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="url"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{form.watch("type") == 2 ? "SMTP Server (host:port)" : "URL"}</FormLabel>
|
|
<FormControl>
|
|
<Input {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{form.watch("type") != 2 && (
|
|
<>
|
|
<FormField
|
|
control={form.control}
|
|
name="request_method"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("RequestMethod")}</FormLabel>
|
|
<Select
|
|
onValueChange={field.onChange}
|
|
defaultValue={`${field.value}`}
|
|
>
|
|
<FormControl>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Request Method" />
|
|
</SelectTrigger>
|
|
</FormControl>
|
|
<SelectContent>
|
|
{Object.entries(nrequestMethods).map(
|
|
([k, v]) => (
|
|
<SelectItem key={k} value={k}>
|
|
{v}
|
|
</SelectItem>
|
|
),
|
|
)}
|
|
</SelectContent>
|
|
</Select>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="request_type"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{t("Type")}</FormLabel>
|
|
<Select
|
|
onValueChange={field.onChange}
|
|
defaultValue={`${field.value}`}
|
|
>
|
|
<FormControl>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Request Type" />
|
|
</SelectTrigger>
|
|
</FormControl>
|
|
<SelectContent>
|
|
{Object.entries(nrequestTypes).map(([k, v]) => (
|
|
<SelectItem key={k} value={k}>
|
|
{v}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</>
|
|
)}
|
|
<FormField
|
|
control={form.control}
|
|
name="request_header"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>{form.watch("type") == 2 ? "SMTP User:Pass" : t("RequestHeader")}</FormLabel>
|
|
<FormControl>
|
|
<Textarea
|
|
className="resize-y"
|
|
placeholder={form.watch("type") == 2 ? "user:pass" : '{"User-Agent":"Nezha-Agent"}'}
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<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>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="verify_tls"
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center space-x-2">
|
|
<FormControl>
|
|
<div className="flex items-center gap-2">
|
|
<Checkbox
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
<Label className="text-sm">
|
|
{t("VerifyTLS")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="skip_check"
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center space-x-2">
|
|
<FormControl>
|
|
<div className="flex items-center gap-2">
|
|
<Checkbox
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
<Label className="text-sm">
|
|
{t("DoNotSendTestMessage")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="format_metric_units"
|
|
render={({ field }) => (
|
|
<FormItem className="flex items-center space-x-2">
|
|
<FormControl>
|
|
<div className="flex items-center gap-2">
|
|
<Checkbox
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
<Label className="text-sm">
|
|
{t("FormatMetricUnits")}
|
|
</Label>
|
|
</div>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<DialogFooter className="justify-end">
|
|
<DialogClose asChild>
|
|
<Button type="button" className="my-2" variant="secondary">
|
|
{t("Close")}
|
|
</Button>
|
|
</DialogClose>
|
|
<Button type="submit" className="my-2">
|
|
{t("Confirm")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
</ScrollArea>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|