mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-05-06 13:48:55 +00:00
198 lines
6.9 KiB
TypeScript
198 lines
6.9 KiB
TypeScript
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, useMemo } from "react";
|
|
import { ActionButtonGroup } from "@/components/action-button-group";
|
|
import { HeaderButtonGroup } from "@/components/header-button-group";
|
|
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";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
export default function NotificationPage() {
|
|
const { t } = useTranslation();
|
|
const { data, mutate, error, isLoading } = useSWR<ModelNotification[]>(
|
|
"/api/v1/notification",
|
|
swrFetcher
|
|
);
|
|
const { notifierGroup } = useNotification();
|
|
|
|
useEffect(() => {
|
|
if (error)
|
|
toast(t("Error"), {
|
|
description: t("Results.ErrorFetchingResource", { error: error.message }),
|
|
});
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [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: t("Name"),
|
|
accessorKey: "name",
|
|
accessorFn: (row) => row.name,
|
|
cell: ({ row }) => {
|
|
const s = row.original;
|
|
return <div className="max-w-32 whitespace-normal break-words">{s.name}</div>;
|
|
},
|
|
},
|
|
{
|
|
header: t("Group"),
|
|
accessorKey: "groups",
|
|
accessorFn: (row) => {
|
|
return (
|
|
notifierGroup
|
|
?.filter((ng) => ng.notifications?.includes(row.id))
|
|
.map((ng) => ng.group.id) || []
|
|
);
|
|
},
|
|
},
|
|
{
|
|
header: "URL",
|
|
accessorKey: "url",
|
|
accessorFn: (row) => row.url,
|
|
cell: ({ row }) => {
|
|
const s = row.original;
|
|
return <div className="max-w-64 whitespace-normal break-words">{s.url}</div>;
|
|
},
|
|
},
|
|
{
|
|
header: t("VerifyTLS"),
|
|
accessorKey: "verify_tls",
|
|
accessorFn: (row) => row.verify_tls,
|
|
},
|
|
{
|
|
id: "actions",
|
|
header: t("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 dataCache = useMemo(() => {
|
|
return data ?? [];
|
|
}, [data]);
|
|
|
|
const table = useReactTable({
|
|
data: dataCache,
|
|
columns,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
});
|
|
|
|
const selectedRows = table.getSelectedRowModel().rows;
|
|
|
|
return (
|
|
<div className="px-8">
|
|
<div className="flex mt-6 mb-4">
|
|
<NotificationTab className="flex-1 mr-4 sm:max-w-[40%]" />
|
|
<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>
|
|
|
|
<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>
|
|
{isLoading ? (
|
|
<TableRow>
|
|
<TableCell colSpan={columns.length} className="h-24 text-center">
|
|
{t("Loading")}...
|
|
</TableCell>
|
|
</TableRow>
|
|
) : 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">
|
|
{t("NoResults")}
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
);
|
|
}
|