Optimize loading and style (#2)

This commit is contained in:
Weilong Huang
2024-11-25 06:40:28 +01:00
committed by GitHub
parent bcd5e721c2
commit 5a874d4930
14 changed files with 1246 additions and 1267 deletions
+95 -91
View File
@@ -1,27 +1,36 @@
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 { ModelNotificationGroupResponseItem } from "@/types"
import { deleteNotificationGroups } from "@/api/notification-group"
import { GroupTab } from "@/components/group-tab"
import { NotificationGroupCard } from "@/components/notification-group"
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 { ModelNotificationGroupResponseItem } from "@/types";
import { deleteNotificationGroups } from "@/api/notification-group";
import { GroupTab } from "@/components/group-tab";
import { NotificationGroupCard } from "@/components/notification-group";
export default function NotificationGroupPage() {
const { data, mutate, error, isLoading } = useSWR<ModelNotificationGroupResponseItem[]>("/api/v1/notification-group", swrFetcher);
const { data, mutate, error, isLoading } = useSWR<ModelNotificationGroupResponseItem[]>(
"/api/v1/notification-group",
swrFetcher
);
useEffect(() => {
if (error)
toast("Error", {
description: `Error fetching resource: ${error.message}.`,
})
}, [error])
});
}, [error]);
const columns: ColumnDef<ModelNotificationGroupResponseItem>[] = [
{
@@ -30,7 +39,7 @@ export default function NotificationGroupPage() {
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && "indeterminate")
(table.getIsSomePageRowsSelected() && "indeterminate")
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label="Select all"
@@ -49,49 +58,48 @@ export default function NotificationGroupPage() {
{
header: "ID",
accessorKey: "id",
accessorFn: row => row.group.id,
accessorFn: (row) => row.group.id,
},
{
header: "Name",
accessorKey: "name",
accessorFn: row => row.group.name,
accessorFn: (row) => row.group.name,
cell: ({ row }) => {
const s = row.original;
return (
<div className="max-w-48 whitespace-normal break-words">
{s.group.name}
</div>
)
}
return <div className="max-w-48 whitespace-normal break-words">{s.group.name}</div>;
},
},
{
header: "Notifiers (ID)",
accessorKey: "notifications",
accessorFn: row => row.notifications,
accessorFn: (row) => row.notifications,
},
{
id: "actions",
header: "Actions",
cell: ({ row }) => {
const s = row.original
const s = row.original;
return (
<ActionButtonGroup className="flex gap-2" delete={{
fn: deleteNotificationGroups,
id: s.group.id,
mutate: mutate,
}}>
<ActionButtonGroup
className="flex gap-2"
delete={{
fn: deleteNotificationGroups,
id: s.group.id,
mutate: mutate,
}}
>
<NotificationGroupCard mutate={mutate} data={s} />
</ActionButtonGroup>
)
);
},
},
]
];
const table = useReactTable({
data: data ?? [],
columns,
getCoreRowModel: getCoreRowModel(),
})
});
const selectedRows = table.getSelectedRowModel().rows;
@@ -99,64 +107,60 @@ export default function NotificationGroupPage() {
<div className="px-8">
<div className="flex mt-6 mb-4">
<GroupTab className="flex-1 mr-4 sm:max-w-[40%]" />
<HeaderButtonGroup className="flex-2 flex gap-2 ml-auto" delete={{
fn: deleteNotificationGroups,
id: selectedRows.map(r => r.original.group.id),
mutate: mutate
}}>
<HeaderButtonGroup
className="flex-2 flex gap-2 ml-auto"
delete={{
fn: deleteNotificationGroups,
id: selectedRows.map((r) => r.original.group.id),
mutate: mutate,
}}
>
<NotificationGroupCard 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>
)
);
}