arbret/frontend/app/audit/page.tsx

243 lines
8.8 KiB
TypeScript
Raw Normal View History

2025-12-18 22:51:43 +01:00
"use client";
2025-12-18 23:54:51 +01:00
import { useEffect, useState, useCallback } from "react";
2025-12-19 11:08:19 +01:00
import { Permission } from "../auth-context";
import { api } from "../api";
import { layoutStyles, cardStyles, tableStyles, paginationStyles } from "../styles/shared";
2025-12-19 11:08:19 +01:00
import { Header } from "../components/Header";
import { useRequireAuth } from "../hooks/useRequireAuth";
2025-12-20 23:06:05 +01:00
import { components } from "../generated/api";
2025-12-18 22:51:43 +01:00
2025-12-20 23:06:05 +01:00
// Use generated types from OpenAPI schema
type _CounterRecord = components["schemas"]["CounterRecordResponse"];
type _SumRecord = components["schemas"]["SumRecordResponse"];
2025-12-20 23:06:05 +01:00
type PaginatedCounterRecords = components["schemas"]["PaginatedResponse_CounterRecordResponse_"];
type PaginatedSumRecords = components["schemas"]["PaginatedResponse_SumRecordResponse_"];
2025-12-18 22:51:43 +01:00
export default function AuditPage() {
2025-12-20 23:06:05 +01:00
const [counterData, setCounterData] = useState<PaginatedCounterRecords | null>(null);
const [sumData, setSumData] = useState<PaginatedSumRecords | null>(null);
2025-12-18 23:54:51 +01:00
const [counterError, setCounterError] = useState<string | null>(null);
const [sumError, setSumError] = useState<string | null>(null);
2025-12-18 22:51:43 +01:00
const [counterPage, setCounterPage] = useState(1);
const [sumPage, setSumPage] = useState(1);
2025-12-19 11:08:19 +01:00
const { user, isLoading, isAuthorized } = useRequireAuth({
requiredPermission: Permission.VIEW_AUDIT,
fallbackRedirect: "/",
});
2025-12-18 22:51:43 +01:00
2025-12-18 23:54:51 +01:00
const fetchCounterRecords = useCallback(async (page: number) => {
setCounterError(null);
2025-12-18 22:51:43 +01:00
try {
2025-12-20 23:06:05 +01:00
const data = await api.get<PaginatedCounterRecords>(
2025-12-19 11:08:19 +01:00
`/api/audit/counter?page=${page}&per_page=10`
);
2025-12-18 22:51:43 +01:00
setCounterData(data);
2025-12-18 23:54:51 +01:00
} catch (err) {
2025-12-18 22:51:43 +01:00
setCounterData(null);
2025-12-18 23:54:51 +01:00
setCounterError(err instanceof Error ? err.message : "Failed to load counter records");
2025-12-18 22:51:43 +01:00
}
2025-12-18 23:54:51 +01:00
}, []);
2025-12-18 22:51:43 +01:00
2025-12-18 23:54:51 +01:00
const fetchSumRecords = useCallback(async (page: number) => {
setSumError(null);
2025-12-18 22:51:43 +01:00
try {
const data = await api.get<PaginatedSumRecords>(`/api/audit/sum?page=${page}&per_page=10`);
2025-12-18 22:51:43 +01:00
setSumData(data);
2025-12-18 23:54:51 +01:00
} catch (err) {
2025-12-18 22:51:43 +01:00
setSumData(null);
2025-12-18 23:54:51 +01:00
setSumError(err instanceof Error ? err.message : "Failed to load sum records");
2025-12-18 22:51:43 +01:00
}
2025-12-18 23:54:51 +01:00
}, []);
useEffect(() => {
2025-12-19 11:08:19 +01:00
if (user && isAuthorized) {
2025-12-18 23:54:51 +01:00
fetchCounterRecords(counterPage);
}
2025-12-19 11:08:19 +01:00
}, [user, counterPage, isAuthorized, fetchCounterRecords]);
2025-12-18 23:54:51 +01:00
useEffect(() => {
2025-12-19 11:08:19 +01:00
if (user && isAuthorized) {
2025-12-18 23:54:51 +01:00
fetchSumRecords(sumPage);
}
2025-12-19 11:08:19 +01:00
}, [user, sumPage, isAuthorized, fetchSumRecords]);
2025-12-18 22:51:43 +01:00
const formatDate = (dateStr: string) => {
return new Date(dateStr).toLocaleString();
};
if (isLoading) {
return (
<main style={layoutStyles.main}>
<div style={layoutStyles.loader}>Loading...</div>
2025-12-18 22:51:43 +01:00
</main>
);
}
2025-12-19 11:08:19 +01:00
if (!user || !isAuthorized) {
2025-12-18 22:51:43 +01:00
return null;
}
return (
<main style={layoutStyles.main}>
2025-12-19 11:08:19 +01:00
<Header currentPage="audit" />
2025-12-18 22:51:43 +01:00
<div style={layoutStyles.contentScrollable}>
2025-12-18 22:51:43 +01:00
<div style={styles.tablesContainer}>
{/* Counter Records Table */}
<div style={cardStyles.tableCard}>
<div style={tableStyles.tableHeader}>
<h2 style={tableStyles.tableTitle}>Counter Activity</h2>
<span style={tableStyles.totalCount}>{counterData?.total ?? 0} records</span>
2025-12-18 22:51:43 +01:00
</div>
<div style={tableStyles.tableWrapper}>
<table style={tableStyles.table}>
2025-12-18 22:51:43 +01:00
<thead>
<tr>
<th style={tableStyles.th}>User</th>
<th style={tableStyles.th}>Before</th>
<th style={tableStyles.th}>After</th>
<th style={tableStyles.th}>Date</th>
2025-12-18 22:51:43 +01:00
</tr>
</thead>
<tbody>
2025-12-18 23:54:51 +01:00
{counterError && (
<tr>
<td colSpan={4} style={tableStyles.errorRow}>
{counterError}
</td>
2025-12-18 23:54:51 +01:00
</tr>
)}
{!counterError &&
counterData?.records.map((record) => (
<tr key={record.id} style={tableStyles.tr}>
<td style={tableStyles.td}>{record.user_email}</td>
<td style={tableStyles.tdNum}>{record.value_before}</td>
<td style={tableStyles.tdNum}>{record.value_after}</td>
<td style={tableStyles.tdDate}>{formatDate(record.created_at)}</td>
</tr>
))}
2025-12-18 23:54:51 +01:00
{!counterError && (!counterData || counterData.records.length === 0) && (
2025-12-18 22:51:43 +01:00
<tr>
<td colSpan={4} style={tableStyles.emptyRow}>
No records yet
</td>
2025-12-18 22:51:43 +01:00
</tr>
)}
</tbody>
</table>
</div>
{counterData && counterData.total_pages > 1 && (
<div style={paginationStyles.pagination}>
2025-12-18 22:51:43 +01:00
<button
onClick={() => setCounterPage((p) => Math.max(1, p - 1))}
disabled={counterPage === 1}
style={paginationStyles.pageBtn}
2025-12-18 22:51:43 +01:00
>
</button>
<span style={paginationStyles.pageInfo}>
2025-12-18 22:51:43 +01:00
{counterPage} / {counterData.total_pages}
</span>
<button
onClick={() => setCounterPage((p) => Math.min(counterData.total_pages, p + 1))}
disabled={counterPage === counterData.total_pages}
style={paginationStyles.pageBtn}
2025-12-18 22:51:43 +01:00
>
</button>
</div>
)}
</div>
{/* Sum Records Table */}
<div style={cardStyles.tableCard}>
<div style={tableStyles.tableHeader}>
<h2 style={tableStyles.tableTitle}>Sum Activity</h2>
<span style={tableStyles.totalCount}>{sumData?.total ?? 0} records</span>
2025-12-18 22:51:43 +01:00
</div>
<div style={tableStyles.tableWrapper}>
<table style={tableStyles.table}>
2025-12-18 22:51:43 +01:00
<thead>
<tr>
<th style={tableStyles.th}>User</th>
<th style={tableStyles.th}>A</th>
<th style={tableStyles.th}>B</th>
<th style={tableStyles.th}>Result</th>
<th style={tableStyles.th}>Date</th>
2025-12-18 22:51:43 +01:00
</tr>
</thead>
<tbody>
2025-12-18 23:54:51 +01:00
{sumError && (
<tr>
<td colSpan={5} style={tableStyles.errorRow}>
{sumError}
</td>
2025-12-18 23:54:51 +01:00
</tr>
)}
{!sumError &&
sumData?.records.map((record) => (
<tr key={record.id} style={tableStyles.tr}>
<td style={tableStyles.td}>{record.user_email}</td>
<td style={tableStyles.tdNum}>{record.a}</td>
<td style={tableStyles.tdNum}>{record.b}</td>
<td style={styles.tdResult}>{record.result}</td>
<td style={tableStyles.tdDate}>{formatDate(record.created_at)}</td>
</tr>
))}
2025-12-18 23:54:51 +01:00
{!sumError && (!sumData || sumData.records.length === 0) && (
2025-12-18 22:51:43 +01:00
<tr>
<td colSpan={5} style={tableStyles.emptyRow}>
No records yet
</td>
2025-12-18 22:51:43 +01:00
</tr>
)}
</tbody>
</table>
</div>
{sumData && sumData.total_pages > 1 && (
<div style={paginationStyles.pagination}>
2025-12-18 22:51:43 +01:00
<button
onClick={() => setSumPage((p) => Math.max(1, p - 1))}
disabled={sumPage === 1}
style={paginationStyles.pageBtn}
2025-12-18 22:51:43 +01:00
>
</button>
<span style={paginationStyles.pageInfo}>
2025-12-18 22:51:43 +01:00
{sumPage} / {sumData.total_pages}
</span>
<button
onClick={() => setSumPage((p) => Math.min(sumData.total_pages, p + 1))}
disabled={sumPage === sumData.total_pages}
style={paginationStyles.pageBtn}
2025-12-18 22:51:43 +01:00
>
</button>
</div>
)}
</div>
</div>
</div>
</main>
);
}
// Page-specific styles only
const styles: Record<string, React.CSSProperties> = {
2025-12-18 22:51:43 +01:00
tablesContainer: {
display: "flex",
flexDirection: "column",
gap: "2rem",
maxWidth: "1200px",
margin: "0 auto",
},
tdResult: {
padding: "0.875rem 1rem",
fontSize: "0.875rem",
color: "#a78bfa",
fontWeight: 600,
fontFamily: "'DM Sans', monospace",
},
};