mirror of
https://github.com/Buriburizaem0n/admin-frontend-domain.git
synced 2026-09-19 17:50:13 +00:00
Optimize loading and style (#2)
This commit is contained in:
+92
-90
@@ -1,17 +1,23 @@
|
||||
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 { ModelWAF, wafBlockReasons } from "@/types"
|
||||
import { deleteWAF } from "@/api/waf"
|
||||
import { ip16Str } from "@/lib/utils"
|
||||
import { SettingsTab } from "@/components/settings-tab"
|
||||
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 { toast } from "sonner";
|
||||
import { ModelWAF, wafBlockReasons } from "@/types";
|
||||
import { deleteWAF } from "@/api/waf";
|
||||
import { ip16Str } from "@/lib/utils";
|
||||
import { SettingsTab } from "@/components/settings-tab";
|
||||
|
||||
export default function WAFPage() {
|
||||
const { data, mutate, error, isLoading } = useSWR<ModelWAF[]>("/api/v1/waf", swrFetcher);
|
||||
@@ -20,8 +26,8 @@ export default function WAFPage() {
|
||||
if (error)
|
||||
toast("Error", {
|
||||
description: `Error fetching resource: ${error.message}.`,
|
||||
})
|
||||
}, [error])
|
||||
});
|
||||
}, [error]);
|
||||
|
||||
const columns: ColumnDef<ModelWAF>[] = [
|
||||
{
|
||||
@@ -30,7 +36,7 @@ export default function WAFPage() {
|
||||
<Checkbox
|
||||
checked={
|
||||
table.getIsAllPageRowsSelected() ||
|
||||
(table.getIsSomePageRowsSelected() && "indeterminate")
|
||||
(table.getIsSomePageRowsSelected() && "indeterminate")
|
||||
}
|
||||
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
|
||||
aria-label="Select all"
|
||||
@@ -49,54 +55,55 @@ export default function WAFPage() {
|
||||
{
|
||||
header: "IP",
|
||||
accessorKey: "ip",
|
||||
accessorFn: row => ip16Str(row.ip ?? []),
|
||||
accessorFn: (row) => ip16Str(row.ip ?? []),
|
||||
},
|
||||
{
|
||||
header: "Count",
|
||||
accessorKey: "count",
|
||||
accessorFn: row => row.count,
|
||||
accessorFn: (row) => row.count,
|
||||
},
|
||||
{
|
||||
header: "Last Block Reason",
|
||||
accessorKey: "lastBlockReason",
|
||||
accessorFn: row => row.last_block_reason,
|
||||
cell: ({ row }) => (
|
||||
<span>{wafBlockReasons[row.original.last_block_reason] || ''}</span>
|
||||
)
|
||||
accessorFn: (row) => row.last_block_reason,
|
||||
cell: ({ row }) => <span>{wafBlockReasons[row.original.last_block_reason] || ""}</span>,
|
||||
},
|
||||
{
|
||||
header: "Last Block Time",
|
||||
accessorKey: "lastBlockTime",
|
||||
accessorFn: row => row.last_block_timestamp,
|
||||
accessorFn: (row) => row.last_block_timestamp,
|
||||
cell: ({ row }) => {
|
||||
const s = row.original;
|
||||
const date = new Date(s.last_block_timestamp || 0);
|
||||
return <span>{date.toISOString()}</span>
|
||||
}
|
||||
return <span>{date.toISOString()}</span>;
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
header: "Actions",
|
||||
cell: ({ row }) => {
|
||||
const s = row.original
|
||||
const s = row.original;
|
||||
return (
|
||||
<ActionButtonGroup className="flex gap-2" delete={{
|
||||
fn: deleteWAF,
|
||||
id: ip16Str(s.ip ?? []),
|
||||
mutate: mutate,
|
||||
}}>
|
||||
<ActionButtonGroup
|
||||
className="flex gap-2"
|
||||
delete={{
|
||||
fn: deleteWAF,
|
||||
id: ip16Str(s.ip ?? []),
|
||||
mutate: mutate,
|
||||
}}
|
||||
>
|
||||
<></>
|
||||
</ActionButtonGroup>
|
||||
)
|
||||
);
|
||||
},
|
||||
},
|
||||
]
|
||||
];
|
||||
|
||||
const table = useReactTable({
|
||||
data: data ?? [],
|
||||
columns,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
})
|
||||
});
|
||||
|
||||
const selectedRows = table.getSelectedRowModel().rows;
|
||||
|
||||
@@ -104,64 +111,59 @@ export default function WAFPage() {
|
||||
<div className="px-8">
|
||||
<SettingsTab className="mt-6 w-full" />
|
||||
<div className="flex mt-4 mb-4">
|
||||
<HeaderButtonGroup className="flex-2 flex gap-2 ml-auto" delete={{
|
||||
fn: deleteWAF,
|
||||
id: selectedRows.map(r => ip16Str(r.original.ip ?? [])),
|
||||
mutate: mutate,
|
||||
}}>
|
||||
<HeaderButtonGroup
|
||||
className="flex-2 flex gap-2 ml-auto"
|
||||
delete={{
|
||||
fn: deleteWAF,
|
||||
id: selectedRows.map((r) => ip16Str(r.original.ip ?? [])),
|
||||
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>
|
||||
)
|
||||
})}
|
||||
<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">
|
||||
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>
|
||||
))}
|
||||
</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>
|
||||
)}
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={columns.length} className="h-24 text-center">
|
||||
No results.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user