Skip to content

Commit fa7a54b

Browse files
committed
feat: add sorting and pagination to Docker events table
1 parent daba2d7 commit fa7a54b

1 file changed

Lines changed: 205 additions & 57 deletions

File tree

‎apps/dokploy/components/dashboard/docker/events/show-docker-events.tsx‎

Lines changed: 205 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { Activity, Loader2, RefreshCw } from "lucide-react";
1+
import {
2+
type ColumnDef,
3+
flexRender,
4+
getCoreRowModel,
5+
getPaginationRowModel,
6+
getSortedRowModel,
7+
type PaginationState,
8+
type SortingState,
9+
useReactTable,
10+
} from "@tanstack/react-table";
11+
import { Activity, ArrowUpDown, Loader2, RefreshCw } from "lucide-react";
212
import { useMemo, useState } from "react";
313
import { Badge } from "@/components/ui/badge";
414
import { Button } from "@/components/ui/button";
@@ -32,6 +42,7 @@ interface Props {
3242
}
3343

3444
type DockerEvent = RouterOutputs["docker"]["getEvents"]["events"][number];
45+
type EventRow = DockerEvent & { key: string };
3546

3647
const RANGE_OPTIONS = [
3748
{ label: "Last 5 minutes", value: 5 },
@@ -73,17 +84,61 @@ const getTypeVariant = (type?: string): BadgeVariant =>
7384
const getActionVariant = (action?: string): BadgeVariant =>
7485
(action && ACTION_VARIANTS[action]) || "blank";
7586

87+
const getResource = (event: DockerEvent) =>
88+
event.Actor?.Attributes?.name ?? event.Actor?.ID ?? "-";
89+
90+
const getAttributesText = (event: DockerEvent) => {
91+
const attributes = Object.entries(event.Actor?.Attributes ?? {}).filter(
92+
([key]) => key !== "name",
93+
);
94+
return attributes.map(([key, value]) => `${key}=${value}`).join(" ") || "-";
95+
};
96+
97+
const SortableHeader = ({
98+
column,
99+
title,
100+
}: {
101+
column: {
102+
getIsSorted: () => false | "asc" | "desc";
103+
toggleSorting: (asc: boolean) => void;
104+
};
105+
title: string;
106+
}) => (
107+
<Button
108+
variant="ghost"
109+
className="-ml-3 h-8"
110+
onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
111+
>
112+
{title}
113+
<ArrowUpDown className="ml-2 size-4" />
114+
</Button>
115+
);
116+
76117
export const ShowDockerEvents = ({ serverId }: Props) => {
77118
const [minutes, setMinutes] = useState(15);
78119
const [search, setSearch] = useState("");
120+
const [sorting, setSorting] = useState<SortingState>([
121+
{ id: "time", desc: true },
122+
]);
123+
const [pagination, setPagination] = useState<PaginationState>({
124+
pageIndex: 0,
125+
pageSize: 15,
126+
});
79127

80128
const { data, isLoading, isRefetching, refetch, error } =
81129
api.docker.getEvents.useQuery({
82130
serverId,
83131
minutes,
84132
});
85133

86-
const events = data?.events ?? [];
134+
const events = useMemo<EventRow[]>(
135+
() =>
136+
(data?.events ?? []).map((event, index) => ({
137+
...event,
138+
key: `${event.time}-${event.Action}-${index}`,
139+
})),
140+
[data],
141+
);
87142

88143
const filteredEvents = useMemo(() => {
89144
if (!search.trim()) return events;
@@ -98,6 +153,96 @@ export const ShowDockerEvents = ({ serverId }: Props) => {
98153
});
99154
}, [events, search]);
100155

156+
const columns = useMemo<ColumnDef<EventRow>[]>(
157+
() => [
158+
{
159+
id: "time",
160+
accessorFn: (event) => event.time ?? 0,
161+
header: ({ column }) => <SortableHeader column={column} title="Time" />,
162+
cell: ({ row }) => (
163+
<span className="text-xs text-muted-foreground whitespace-nowrap">
164+
{row.original.time
165+
? new Date(row.original.time * 1000).toLocaleTimeString()
166+
: "-"}
167+
</span>
168+
),
169+
},
170+
{
171+
id: "type",
172+
accessorFn: (event) => event.Type ?? "",
173+
header: ({ column }) => <SortableHeader column={column} title="Type" />,
174+
cell: ({ row }) => (
175+
<Badge variant={getTypeVariant(row.original.Type)}>
176+
{row.original.Type ?? "unknown"}
177+
</Badge>
178+
),
179+
},
180+
{
181+
id: "action",
182+
accessorFn: (event) => event.Action ?? "",
183+
header: ({ column }) => (
184+
<SortableHeader column={column} title="Action" />
185+
),
186+
cell: ({ row }) => (
187+
<Badge variant={getActionVariant(row.original.Action)}>
188+
{row.original.Action ?? "-"}
189+
</Badge>
190+
),
191+
},
192+
{
193+
id: "resource",
194+
accessorFn: (event) => getResource(event),
195+
header: ({ column }) => (
196+
<SortableHeader column={column} title="Resource" />
197+
),
198+
cell: ({ row }) => {
199+
const resource = getResource(row.original);
200+
return (
201+
<div
202+
className="max-w-[220px] truncate font-mono text-xs"
203+
title={resource}
204+
>
205+
{resource}
206+
</div>
207+
);
208+
},
209+
},
210+
{
211+
id: "attributes",
212+
accessorFn: (event) => getAttributesText(event),
213+
header: "Attributes",
214+
enableSorting: false,
215+
cell: ({ row }) => {
216+
const attributesText = getAttributesText(row.original);
217+
return (
218+
<div
219+
className="max-w-[360px] truncate text-xs text-muted-foreground"
220+
title={attributesText}
221+
>
222+
{attributesText}
223+
</div>
224+
);
225+
},
226+
},
227+
],
228+
[],
229+
);
230+
231+
const table = useReactTable({
232+
data: filteredEvents,
233+
columns,
234+
getRowId: (row) => row.key,
235+
state: {
236+
sorting,
237+
pagination,
238+
},
239+
onSortingChange: setSorting,
240+
onPaginationChange: setPagination,
241+
getCoreRowModel: getCoreRowModel(),
242+
getSortedRowModel: getSortedRowModel(),
243+
getPaginationRowModel: getPaginationRowModel(),
244+
});
245+
101246
return (
102247
<div className="w-full">
103248
<Card className="h-full bg-sidebar p-2.5 rounded-xl">
@@ -135,7 +280,10 @@ export const ShowDockerEvents = ({ serverId }: Props) => {
135280
<Input
136281
placeholder="Filter by type, action or name..."
137282
value={search}
138-
onChange={(e) => setSearch(e.target.value)}
283+
onChange={(e) => {
284+
setSearch(e.target.value);
285+
setPagination((prev) => ({ ...prev, pageIndex: 0 }));
286+
}}
139287
className="max-w-xs"
140288
/>
141289
<Select
@@ -157,19 +305,26 @@ export const ShowDockerEvents = ({ serverId }: Props) => {
157305
<div className="rounded-md border overflow-x-auto">
158306
<Table>
159307
<TableHeader>
160-
<TableRow>
161-
<TableHead className="w-[110px]">Time</TableHead>
162-
<TableHead className="w-[100px]">Type</TableHead>
163-
<TableHead className="w-[110px]">Action</TableHead>
164-
<TableHead>Resource</TableHead>
165-
<TableHead>Attributes</TableHead>
166-
</TableRow>
308+
{table.getHeaderGroups().map((headerGroup) => (
309+
<TableRow key={headerGroup.id}>
310+
{headerGroup.headers.map((header) => (
311+
<TableHead key={header.id}>
312+
{header.isPlaceholder
313+
? null
314+
: flexRender(
315+
header.column.columnDef.header,
316+
header.getContext(),
317+
)}
318+
</TableHead>
319+
))}
320+
</TableRow>
321+
))}
167322
</TableHeader>
168323
<TableBody>
169324
{isLoading ? (
170325
<TableRow>
171326
<TableCell
172-
colSpan={5}
327+
colSpan={columns.length}
173328
className="h-24 text-center text-muted-foreground"
174329
>
175330
<div className="flex flex-row items-center justify-center gap-2">
@@ -178,56 +333,23 @@ export const ShowDockerEvents = ({ serverId }: Props) => {
178333
</div>
179334
</TableCell>
180335
</TableRow>
181-
) : filteredEvents.length ? (
182-
filteredEvents.map((event: DockerEvent, index: number) => {
183-
const attributes = Object.entries(
184-
event.Actor?.Attributes ?? {},
185-
).filter(([key]) => key !== "name");
186-
const attributesText =
187-
attributes
188-
.map(([key, value]) => `${key}=${value}`)
189-
.join(" ") || "-";
190-
const resource =
191-
event.Actor?.Attributes?.name ?? event.Actor?.ID ?? "-";
192-
193-
return (
194-
<TableRow
195-
key={`${event.time}-${event.Action}-${index}`}
196-
>
197-
<TableCell className="text-xs text-muted-foreground whitespace-nowrap">
198-
{event.time
199-
? new Date(event.time * 1000).toLocaleTimeString()
200-
: "-"}
201-
</TableCell>
202-
<TableCell>
203-
<Badge variant={getTypeVariant(event.Type)}>
204-
{event.Type ?? "unknown"}
205-
</Badge>
336+
) : table.getRowModel().rows?.length ? (
337+
table.getRowModel().rows.map((row) => (
338+
<TableRow key={row.id}>
339+
{row.getVisibleCells().map((cell) => (
340+
<TableCell key={cell.id}>
341+
{flexRender(
342+
cell.column.columnDef.cell,
343+
cell.getContext(),
344+
)}
206345
</TableCell>
207-
<TableCell>
208-
<Badge variant={getActionVariant(event.Action)}>
209-
{event.Action ?? "-"}
210-
</Badge>
211-
</TableCell>
212-
<TableCell
213-
className="max-w-[220px] truncate font-mono text-xs"
214-
title={resource}
215-
>
216-
{resource}
217-
</TableCell>
218-
<TableCell
219-
className="max-w-[360px] truncate text-xs text-muted-foreground"
220-
title={attributesText}
221-
>
222-
{attributesText}
223-
</TableCell>
224-
</TableRow>
225-
);
226-
})
346+
))}
347+
</TableRow>
348+
))
227349
) : (
228350
<TableRow>
229351
<TableCell
230-
colSpan={5}
352+
colSpan={columns.length}
231353
className="h-24 text-center text-muted-foreground"
232354
>
233355
No events found in the selected time range.
@@ -237,6 +359,32 @@ export const ShowDockerEvents = ({ serverId }: Props) => {
237359
</TableBody>
238360
</Table>
239361
</div>
362+
{table.getPageCount() > 1 && (
363+
<div className="flex items-center justify-end gap-4">
364+
<span className="text-sm text-muted-foreground">
365+
Page {table.getState().pagination.pageIndex + 1} of{" "}
366+
{table.getPageCount()}
367+
</span>
368+
<div className="flex gap-2">
369+
<Button
370+
variant="outline"
371+
size="sm"
372+
onClick={() => table.previousPage()}
373+
disabled={!table.getCanPreviousPage()}
374+
>
375+
Previous
376+
</Button>
377+
<Button
378+
variant="outline"
379+
size="sm"
380+
onClick={() => table.nextPage()}
381+
disabled={!table.getCanNextPage()}
382+
>
383+
Next
384+
</Button>
385+
</div>
386+
</div>
387+
)}
240388
</CardContent>
241389
</div>
242390
</Card>

0 commit comments

Comments
 (0)