1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224import { useQuery } from "@tanstack/react-query";
import * as API from "aws-amplify/api";
import getLogsResponse from "../../../../shared/types/logs";
import getLogsResponseLog from "../../../../shared/types/logs";
import { ChangeEvent, useMemo, useState } from "react";
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
import { SkeletonTable } from "#src/components/Skeleton.tsx";
export const Logs = () => {
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(50);
const { data: logs, isLoading, isError } = useQuery({
queryKey: ["logs"],
queryFn: async () => {
return (await (
await API.get({
apiName: "auth",
path: "/getLogs",
options: { queryParams: { page: page.toString(), pageSize: pageSize.toString() } },
}).response
).body.json()) as unknown as getLogsResponse;
},
refetchInterval: 5000,
placeholderData: (previousData) => previousData,
});
//console.log(logs);
interface userObj {
name:string,
email:string
}
const columns = useMemo<ColumnDef<getLogsResponseLog>[]>(
() => [
{
accessorKey: "created_at",
header: "Date",
cell: ({ getValue }) => {
const theDate = getValue() as string;
return new Date(theDate).toLocaleString("en-US", {
dateStyle: 'short'
}) + " " +
new Date(theDate).toLocaleString("en-US", {
hour: "2-digit",
minute: "2-digit",
});
},
},
{
accessorKey: "message",
header: "Message",
cell: ({ getValue }) => {
return getValue();
},
},
{
accessorKey: "LogToUser",
header: "User",
cell: ({ getValue }) => {
const userObj = getValue() as userObj;
return `${userObj.name} (${userObj.email})`;
}
},
{
accessorKey: "LogToAudit",
header: "Audit",
cell: ({ getValue }) => {
const userObj = getValue() as { name:string } ?? null;
return userObj && userObj.name ? userObj.name : "N/A";
}
}
],
[]
);
const table = useReactTable({
data: logs?.logs || [],
columns,
getCoreRowModel: getCoreRowModel(),
manualPagination: true,
pageCount: logs ? Math.ceil(logs?.logs_aggregate?.aggregate?.count / pageSize) : 0,
});
const handlePageSizeChange = (e: ChangeEvent<HTMLSelectElement>)=>{
setPageSize(parseInt(e.target.value))
}
return (
<>
<h1 className="initial-focus-element pb-3">Logs</h1>
{isError && (
<div className="text-center py-8">Error Loading Logs Data</div>
)}
{isLoading ? (
<SkeletonTable columns={4} rows={8} headers={["Date", "Message", "User", "Audit"]} />
) : (
<>
<div className="table-container" style={{ marginBottom : "16px" }}>
<table
aria-label="Logs table"
>
<thead>
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id} className="bg-gray-100">
{headerGroup.headers.map((header) => (
<th
key={header.id}
scope="col"
className="border border-gray-300 px-4 py-2 text-left font-semibold"
>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</th>
))}
</tr>
))}
</thead>
<tbody>
{table.getRowModel().rows.length === 0 ? (
<tr>
<td
colSpan={columns.length}
className="border border-gray-300 px-4 py-8 text-center text-gray-500"
>
No logs found
</td>
</tr>
) : (
table.getRowModel().rows.map((row, index) => (
<tr
key={row.id}
className={index % 2 === 0 ? "bg-white" : "bg-gray-50"}
>
{row.getVisibleCells().map((cell) => (
<td
key={cell.id}
className="border border-gray-300 px-4 py-2"
>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</td>
))}
</tr>
))
)}
</tbody>
</table>
{/* Pagination Controls */}
<div
className="pagination"
role="navigation"
aria-label="Pagination"
>
<div className="pagination-text">
Showing {logs?.logs?.length || 0} of{" "}
{logs?.logs_aggregate.aggregate.count || 0} logs
{logs?.logs_aggregate.aggregate.count &&
` (Page ${page + 1} of ${Math.ceil(logs.logs_aggregate.aggregate.count / pageSize)})`}
</div>
<div className="pagination-buttons">
<label htmlFor="pageSize" className="text-sm">Logs per page:</label>
<select id="pageSize" value={pageSize} onChange={handlePageSizeChange}>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<button
onClick={() => setPage(0)}
disabled={page === 0}
aria-label="Go to first page"
>
First
</button>
<button
onClick={() => setPage((p) => Math.max(0, p - 1))}
disabled={page === 0}
aria-label="Go to previous page"
>
Previous
</button>
<button
onClick={() => setPage((p) => p + 1)}
disabled={
!logs || page >= Math.ceil(logs.logs_aggregate.aggregate.count / pageSize) - 1
}
aria-label="Go to next page"
>
Next
</button>
<button
onClick={() =>
logs && setPage(Math.ceil(logs.logs_aggregate.aggregate.count / pageSize) - 1)
}
disabled={
!logs || page >= Math.ceil(logs.logs_aggregate.aggregate.count / pageSize) - 1
}
aria-label="Go to last page"
>
Last
</button>
</div>
</div>
</div>
</>
)}
</>
);
};