implement notification page (#8)

This commit is contained in:
UUBulb
2024-11-20 21:37:03 +08:00
committed by GitHub
parent e37f30d335
commit b3588b3378
27 changed files with 1133 additions and 36 deletions

186
src/routes/alert-rule.tsx Normal file
View File

@@ -0,0 +1,186 @@
import { swrFetcher } from "@/api/api"
import { Checkbox } from "@/components/ui/checkbox"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"
import useSWR from "swr"
import { useEffect } from "react"
import { ActionButtonGroup } from "@/components/action-button-group"
import { HeaderButtonGroup } from "@/components/header-button-group"
import { Skeleton } from "@/components/ui/skeleton"
import { toast } from "sonner"
import { ModelAlertRule, triggerModes } from "@/types"
import { deleteAlertRules } from "@/api/alert-rule"
import { NotificationTab } from "@/components/notification-tab"
import { AlertRuleCard } from "@/components/alert-rule"
export default function AlertRulePage() {
const { data, mutate, error, isLoading } = useSWR<ModelAlertRule[]>("/api/v1/alert-rule", swrFetcher);
useEffect(() => {
if (error)
toast("Error", {
description: `Error fetching resource: ${error.message}.`,
})
}, [error])
const columns: ColumnDef<ModelAlertRule>[] = [
{
id: "select",
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label="Select row"
/>
),
enableSorting: false,
enableHiding: false,
},
{
header: "ID",
accessorKey: "id",
accessorFn: row => row.id,
},
{
header: "Name",
accessorKey: "name",
cell: ({ row }) => {
const s = row.original;
return (
<div className="max-w-32 whitespace-normal break-words">
{s.name}
</div>
)
}
},
{
header: "Notifier Group",
accessorKey: "ngroup",
accessorFn: row => row.notification_group_id,
},
{
header: "Trigger Mode",
accessorKey: "trigger Mode",
accessorFn: row => triggerModes[row.trigger_mode] || '',
},
{
header: "Rules",
accessorKey: "rules",
accessorFn: row => JSON.stringify(row.rules),
},
{
header: "Tasks to trigger on alert",
accessorKey: "failTriggerTasks",
accessorFn: row => row.fail_trigger_tasks,
},
{
header: "Tasks to trigger after recovery",
accessorKey: "recoverTriggerTasks",
accessorFn: row => row.recover_trigger_tasks,
},
{
header: "Enable",
accessorKey: "enable",
accessorFn: row => row.enable,
},
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
const s = row.original
return (
<ActionButtonGroup className="flex gap-2" delete={{
fn: deleteAlertRules,
id: s.id,
mutate: mutate,
}}>
<AlertRuleCard mutate={mutate} data={s} />
</ActionButtonGroup>
)
},
},
]
const table = useReactTable({
data: data ?? [],
columns,
getCoreRowModel: getCoreRowModel(),
})
const selectedRows = table.getSelectedRowModel().rows;
return (
<div className="px-8">
<div className="flex mt-6 mb-4 gap-[60%]">
<NotificationTab className="flex-1" />
<HeaderButtonGroup className="flex-2 flex gap-2 ml-auto" delete={{
fn: deleteAlertRules,
id: selectedRows.map(r => r.original.id),
mutate: mutate,
}}>
<AlertRuleCard mutate={mutate} />
</HeaderButtonGroup>
</div>
{isLoading ? (
<div className="flex flex-col items-center space-y-4">
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
</div>
) : (
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id} className="text-sm">
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id} className="text-xsm">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)}
</div>
)
}

View File

@@ -96,9 +96,9 @@ export default function NotificationGroupPage() {
return (
<div className="px-8">
<div className="flex mt-6 mb-4">
<GroupTab />
<HeaderButtonGroup className="flex gap-2 ml-auto" delete={{
<div className="flex mt-6 mb-4 gap-[60%]">
<GroupTab className="flex-1" />
<HeaderButtonGroup className="flex-2 flex gap-2 ml-auto" delete={{
fn: deleteNotificationGroups,
id: selectedRows.map(r => r.original.group.id),
mutate: mutate

184
src/routes/notification.tsx Normal file
View File

@@ -0,0 +1,184 @@
import { swrFetcher } from "@/api/api"
import { Checkbox } from "@/components/ui/checkbox"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"
import useSWR from "swr"
import { useEffect } from "react"
import { ActionButtonGroup } from "@/components/action-button-group"
import { HeaderButtonGroup } from "@/components/header-button-group"
import { Skeleton } from "@/components/ui/skeleton"
import { toast } from "sonner"
import { ModelNotification } from "@/types"
import { deleteNotification } from "@/api/notification"
import { NotificationTab } from "@/components/notification-tab"
import { NotifierCard } from "@/components/notifier"
import { useNotification } from "@/hooks/useNotfication"
export default function NotificationPage() {
const { data, mutate, error, isLoading } = useSWR<ModelNotification[]>("/api/v1/notification", swrFetcher);
const { notifierGroup } = useNotification();
useEffect(() => {
if (error)
toast("Error", {
description: `Error fetching resource: ${error.message}.`,
})
}, [error])
const columns: ColumnDef<ModelNotification>[] = [
{
id: "select",
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all"
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label="Select row"
/>
),
enableSorting: false,
enableHiding: false,
},
{
header: "ID",
accessorKey: "id",
accessorFn: row => row.id,
},
{
header: "Name",
accessorKey: "name",
cell: ({ row }) => {
const s = row.original;
return (
<div className="max-w-32 whitespace-normal break-words">
{s.name}
</div>
)
}
},
{
header: "Groups",
accessorKey: "groups",
accessorFn: row => {
return notifierGroup?.filter(ng => ng.notifications?.includes(row.id))
.map(ng => ng.group.id)
|| [];
},
},
{
header: "URL",
accessorKey: "url",
cell: ({ row }) => {
const s = row.original;
return (
<div className="max-w-64 whitespace-normal break-words">
{s.url}
</div>
)
}
},
{
header: "Verify TLS",
accessorKey: "verify_tls",
accessorFn: row => row.verify_tls,
},
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
const s = row.original
return (
<ActionButtonGroup className="flex gap-2" delete={{
fn: deleteNotification,
id: s.id,
mutate: mutate,
}}>
<NotifierCard mutate={mutate} data={s} />
</ActionButtonGroup>
)
},
},
]
const table = useReactTable({
data: data ?? [],
columns,
getCoreRowModel: getCoreRowModel(),
})
const selectedRows = table.getSelectedRowModel().rows;
return (
<div className="px-8">
<div className="flex mt-6 mb-4 gap-[60%]">
<NotificationTab className="flex-1" />
<HeaderButtonGroup className="flex-2 flex gap-2 ml-auto" delete={{
fn: deleteNotification,
id: selectedRows.map(r => r.original.id),
mutate: mutate
}}>
<NotifierCard mutate={mutate} />
</HeaderButtonGroup>
</div>
{isLoading ? (
<div className="flex flex-col items-center space-y-4">
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
<Skeleton className="h-[60px] w-[100%] rounded-lg" />
</div>
) : (
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id} className="text-sm">
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id} className="text-xsm">
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
)}
</div>
)
}

View File

@@ -96,9 +96,9 @@ export default function ServerGroupPage() {
return (
<div className="px-8">
<div className="flex mt-6 mb-4">
<GroupTab />
<HeaderButtonGroup className="flex ml-auto gap-2" delete={{
<div className="flex mt-6 mb-4 gap-[60%]">
<GroupTab className="flex-1" />
<HeaderButtonGroup className="flex-2 flex ml-auto gap-2" delete={{
fn: deleteServerGroups,
id: selectedRows.map(r => r.original.group.id),
mutate: mutate

View File

@@ -72,7 +72,7 @@ export default function ServerPage() {
header: "Groups",
accessorKey: "groups",
accessorFn: row => {
return serverGroups?.filter(sg => sg.servers.includes(row.id))
return serverGroups?.filter(sg => sg.servers?.includes(row.id))
.map(sg => sg.group.id)
|| [];
},

View File

@@ -116,7 +116,7 @@ export default function ServicePage() {
accessorFn: row => row.service.enable_trigger_task ?? false,
},
{
header: "Tasks to trigger on an alarm",
header: "Tasks to trigger on alert",
accessorKey: "service.failTriggerTasks",
accessorFn: row => row.service.fail_trigger_tasks,
},