further implementing service page (#3)

This commit is contained in:
UUBulb
2024-11-17 10:05:20 +08:00
committed by GitHub
parent 55821320dc
commit 6e3f888792
20 changed files with 936 additions and 333 deletions

View File

@@ -0,0 +1,50 @@
import { Button } from "@/components/ui/button";
import { TrashButton } from "@/components/xui/icon-buttons";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogClose,
} from "@/components/ui/dialog"
import { KeyedMutator } from "swr";
interface ButtonGroupProps<T> {
className?: string;
children: React.ReactNode;
delete: { fn: (id: number[]) => Promise<void>, id: number, mutate: KeyedMutator<T> };
}
export function ActionButtonGroup<T>({ className, children, delete: { fn, id, mutate } }: ButtonGroupProps<T>) {
const handleDelete = async () => {
await fn([id]);
await mutate();
}
return (
<div className={className}>
{children}
<Dialog>
<DialogTrigger asChild>
<TrashButton variant="outline" />
</DialogTrigger>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>Confirm Deletion?</DialogTitle>
<DialogDescription>
This operation is unrecoverable!
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button type="submit" variant="destructive" onClick={handleDelete}>Confirm</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
}

View File

@@ -0,0 +1,64 @@
import { Button } from "@/components/ui/button";
import { TrashButton } from "@/components/xui/icon-buttons";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogClose,
} from "@/components/ui/dialog"
import { KeyedMutator } from "swr";
import { toast } from "sonner"
interface ButtonGroupProps<T> {
className?: string;
children: React.ReactNode;
delete: { fn: (id: number[]) => Promise<void>, id: number[], mutate: KeyedMutator<T> };
}
export function HeaderButtonGroup<T>({ className, children, delete: { fn, id, mutate } }: ButtonGroupProps<T>) {
const handleDelete = async () => {
await fn(id);
await mutate();
}
return (
<div className={className}>
{id.length < 1 ? (
<>
<TrashButton variant="destructive" onClick={() => {
toast("Error", {
description: "No rows are selected."
});
}} />
{children}
</>
) : (
<>
<Dialog>
<DialogTrigger asChild>
<TrashButton variant="destructive" />
</DialogTrigger>
<DialogContent className="sm:max-w-lg">
<DialogHeader>
<DialogTitle>Confirm Deletion?</DialogTitle>
<DialogDescription>
This operation is unrecoverable!
</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose asChild>
<Button type="submit" variant="destructive" onClick={handleDelete}>Confirm</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
{children}
</>
)}
</div>
)
}

View File

@@ -13,12 +13,14 @@ import { NzNavigationMenuLink } from "./xui/navigation-menu";
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger } from "./ui/dropdown-menu";
import { User, LogOut } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { Link } from "react-router-dom";
import { Link, useLocation } from "react-router-dom";
export default function Header() {
const { logout } = useAuth()
const profile = useMainStore(store => store.profile)
const { logout } = useAuth();
const profile = useMainStore(store => store.profile);
const location = useLocation();
return <header className="h-16 flex items-center border-b-2 px-4">
<NavigationMenu className="max-w-full">
<NavigationMenuList>
@@ -31,12 +33,12 @@ export default function Header() {
{
profile && <>
<NavigationMenuItem>
<NzNavigationMenuLink asChild active className={navigationMenuTriggerStyle()}>
<NzNavigationMenuLink asChild active={location.pathname === "/dashboard"} className={navigationMenuTriggerStyle()}>
<Link to="/dashboard">Server</Link>
</NzNavigationMenuLink>
</NavigationMenuItem>
<NavigationMenuItem>
<NzNavigationMenuLink asChild className={navigationMenuTriggerStyle()}>
<NzNavigationMenuLink asChild active={location.pathname === "/dashboard/service"} className={navigationMenuTriggerStyle()}>
<Link to="/dashboard/service">Service</Link>
</NzNavigationMenuLink>
</NavigationMenuItem>

View File

@@ -1,9 +1,9 @@
import { Plus } from "lucide-react"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
@@ -25,184 +25,388 @@ import {
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useForm } from "react-hook-form"
import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"
import { ModelService } from "@/types"
import { ModelService, ModelServiceResponse } from "@/types"
import { createService, updateService } from "@/api/service"
import { Checkbox } from "./ui/checkbox"
import { Label } from "./ui/label"
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"
import { conv } from "@/lib/utils"
import { useState } from "react"
import { KeyedMutator } from "swr"
import { asOptionalField } from "@/lib/utils"
import { EditButton, PlusButton } from "@/components/xui/icon-buttons"
import { serviceTypes, serviceCoverageTypes } from "@/types"
interface ServiceCardProps {
className?: string;
data?: ModelService;
mutate: KeyedMutator<ModelServiceResponse>;
}
const serviceFormSchema = z.object({
cover: z.number(),
duration: z.number().min(30),
enable_show_in_service: z.boolean().default(false),
enable_trigger_task: z.boolean().default(false),
fail_trigger_tasks: z.array(z.number()),
latency_notify: z.boolean(),
max_latency: z.number(),
min_latency: z.number(),
name: z.string(),
notification_group_id: z.number(),
notify: z.boolean(),
recover_trigger_tasks: z.array(z.number()),
cover: z.coerce.number().min(0),
duration: z.coerce.number().min(30),
enable_show_in_service: asOptionalField(z.boolean()),
enable_trigger_task: asOptionalField(z.boolean()),
fail_trigger_tasks: z.array(z.string()).transform((v => {
return v.filter(Boolean).map(Number);
})),
latency_notify: asOptionalField(z.boolean()),
max_latency: z.coerce.number().min(0),
min_latency: z.coerce.number().min(0),
name: z.string().min(1),
notification_group_id: z.coerce.number(),
notify: asOptionalField(z.boolean()),
recover_trigger_tasks: z.array(z.string()).transform((v => {
return v.filter(Boolean).map(Number);
})),
skip_servers: z.record(z.boolean()),
target: z.string(),
type: z.number(),
target: z.string().url(),
type: z.coerce.number().min(0),
});
const serviceTypes = {
1: "HTTP GET (Certificate expiration and changes)",
2: "ICMP Ping",
3: "TCPing",
}
const serviceCoverageTypes = {
0: "All excludes specific servers",
1: "Only specific servers",
}
export const ServiceCard: React.FC<ServiceCardProps> = ({ className, data }) => {
export const ServiceCard: React.FC<ServiceCardProps> = ({ data, mutate }) => {
const form = useForm<z.infer<typeof serviceFormSchema>>({
resolver: zodResolver(serviceFormSchema),
defaultValues: data,
defaultValues: data ? data : {
type: 1,
cover: 0,
name: "",
target: "",
max_latency: 0.0,
min_latency: 0.0,
duration: 30,
notification_group_id: 0,
fail_trigger_tasks: [],
recover_trigger_tasks: [],
skip_servers: {},
},
resetOptions: {
keepDefaultValues: false,
}
})
const onSubmit = (values: z.infer<typeof serviceFormSchema>) => {
data?.id ? updateService(data.id, values)
: createService(values);
const [open, setOpen] = useState(false);
const onSubmit = async (values: z.infer<typeof serviceFormSchema>) => {
data?.id ? await updateService(data.id, values) : await createService(values);
setOpen(false);
await mutate();
form.reset();
}
return (
<Dialog>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className={`${className}`}>
<Plus /> Add New Service
</Button>
{data
?
<EditButton variant="outline" />
:
<PlusButton />
}
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>New Service</DialogTitle>
</DialogHeader>
<div className="items-center">
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Service Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="target"
render={({ field }) => (
<FormItem>
<FormLabel>Target</FormLabel>
<FormControl>
<Input type="link" placeholder="HTTP (https://t.tt)Ping (t.tt)TCP (t.tt:80)" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="type"
render={({ field }) => (
<FormItem>
<FormLabel>Type</FormLabel>
<Select onValueChange={field.onChange} defaultValue={`${field.value}`}>
<DialogContent className="sm:max-w-xl">
<ScrollArea className="max-h-[calc(100dvh-5rem)] p-3">
<div className="items-center mx-1">
<DialogHeader>
<DialogTitle>New Service</DialogTitle>
<DialogDescription />
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2 my-2">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Service Name</FormLabel>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select service type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(serviceTypes).map(([k, v]) => (
<SelectItem value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enable_show_in_service"
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}
<Input
placeholder="My Service Monitor"
{...field}
/>
<Label className="text-sm">Show in Service</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="duration"
render={({ field }) => (
<FormItem>
<FormLabel>Interval</FormLabel>
<FormControl>
<Input type="number" placeholder="Seconds" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="cover"
render={({ field }) => (
<FormItem>
<FormLabel>Coverage</FormLabel>
<Select onValueChange={field.onChange} defaultValue={`${field.value}`}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select service type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(serviceCoverageTypes).map(([k, v]) => (
<SelectItem value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
</div>
<DialogFooter className="sm:justify-start">
<DialogClose asChild>
<Button type="button" variant="secondary">
Close
</Button>
</DialogClose>
</DialogFooter>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="target"
render={({ field }) => (
<FormItem>
<FormLabel>Target</FormLabel>
<FormControl>
<Input
placeholder="HTTP (https://t.tt)Ping (t.tt)TCP (t.tt:80)"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="type"
render={({ field }) => (
<FormItem>
<FormLabel>Type</FormLabel>
<Select onValueChange={field.onChange} defaultValue={`${field.value}`}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select service type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(serviceTypes).map(([k, v]) => (
<SelectItem key={k} value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enable_show_in_service"
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">Show in Service</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="duration"
render={({ field }) => (
<FormItem>
<FormLabel>Interval (s)</FormLabel>
<FormControl>
<Input
type="number"
placeholder="30"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="cover"
render={({ field }) => (
<FormItem>
<FormLabel>Coverage</FormLabel>
<Select onValueChange={field.onChange} defaultValue={`${field.value}`}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select service type" />
</SelectTrigger>
</FormControl>
<SelectContent>
{Object.entries(serviceCoverageTypes).map(([k, v]) => (
<SelectItem key={k} value={k}>{v}</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="skip_servers"
render={({ field }) => (
<FormItem>
<FormLabel>Specific Servers (separate with comma)</FormLabel>
<FormControl>
<Input
placeholder="1,2,3"
{...field}
value={conv.recordToStr(field.value ?? {})}
onChange={e => {
const rec = conv.strToRecord(e.target.value);
field.onChange(rec);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="notification_group_id"
render={({ field }) => (
<FormItem>
<FormLabel>Notification Group ID</FormLabel>
<FormControl>
<Input
type="number"
placeholder="1"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="notify"
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">Enable Failure Notification</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="max_latency"
render={({ field }) => (
<FormItem>
<FormLabel>Maximum Latency Time (ms)</FormLabel>
<FormControl>
<Input
type="number"
placeholder="100.88"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="min_latency"
render={({ field }) => (
<FormItem>
<FormLabel>Minimum Latency Time (ms)</FormLabel>
<FormControl>
<Input
type="number"
placeholder="100.88"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="latency_notify"
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">Enable Latency Notification</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="enable_trigger_task"
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">Enable Trigger Task</Label>
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="fail_trigger_tasks"
render={({ field }) => (
<FormItem>
<FormLabel>Tasks to trigger on an alarm (Separate with comma)</FormLabel>
<FormControl>
<Input
placeholder="1,2,3"
{...field}
value={conv.arrToStr(field.value ?? [])}
onChange={e => {
const arr = conv.strToArr(e.target.value);
field.onChange(arr);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="recover_trigger_tasks"
render={({ field }) => (
<FormItem>
<FormLabel>Tasks to trigger after recovery (Separate with comma)</FormLabel>
<FormControl>
<Input
placeholder="1,2,3"
{...field}
value={conv.arrToStr(field.value ?? [])}
onChange={e => {
const arr = conv.strToArr(e.target.value);
field.onChange(arr);
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<DialogFooter className="justify-end">
<DialogClose asChild>
<Button type="button" className="my-2" variant="secondary">
Close
</Button>
</DialogClose>
<Button type="submit" className="my-2">Submit</Button>
</DialogFooter>
</form>
</Form>
</div>
</ScrollArea>
</DialogContent>
</Dialog>
)

View File

@@ -11,7 +11,7 @@ const Checkbox = React.forwardRef<
<CheckboxPrimitive.Root
ref={ref}
className={cn(
"peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
"flex peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
className
)}
{...props}

View File

@@ -0,0 +1,46 @@
import * as React from "react"
import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area"
import { cn } from "@/lib/utils"
const ScrollArea = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn("relative overflow-hidden", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
))
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName
const ScrollBar = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
>(({ className, orientation = "vertical", ...props }, ref) => (
<ScrollAreaPrimitive.ScrollAreaScrollbar
ref={ref}
orientation={orientation}
className={cn(
"flex touch-none select-none transition-colors",
orientation === "vertical" &&
"h-full w-2.5 border-l border-l-transparent p-[1px]",
orientation === "horizontal" &&
"h-2.5 flex-col border-t border-t-transparent p-[1px]",
className
)}
{...props}
>
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" />
</ScrollAreaPrimitive.ScrollAreaScrollbar>
))
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName
export { ScrollArea, ScrollBar }

View File

@@ -0,0 +1,15 @@
import { cn } from "@/lib/utils"
function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn("animate-pulse rounded-md bg-muted", className)}
{...props}
/>
)
}
export { Skeleton }

View File

@@ -0,0 +1,27 @@
import { Plus, Edit2, Trash2 } from "lucide-react"
import { Button, ButtonProps } from "@/components/ui/button"
import { forwardRef } from "react";
export const EditButton = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
return (
<Button {...props} ref={ref} size="icon">
<Edit2 />
</Button>
);
});
export const TrashButton = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
return (
<Button {...props} ref={ref} size="icon">
<Trash2 />
</Button>
);
});
export const PlusButton = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
return (
<Button {...props} ref={ref} size="icon">
<Plus />
</Button>
);
});