187 lines
8.0 KiB
TypeScript
187 lines
8.0 KiB
TypeScript
import { Head, Link, router } from '@inertiajs/react';
|
|
import { Search, ScrollText } from 'lucide-react';
|
|
import type { FormEvent } from 'react';
|
|
import { useState } from 'react';
|
|
import { PageHeading } from '@/components/page-heading';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
import { Input } from '@/components/ui/input';
|
|
import type { Paginated } from '@/types';
|
|
|
|
type AuditLog = {
|
|
id: number;
|
|
event: string;
|
|
auditable_type: string | null;
|
|
auditable_id: string | null;
|
|
before: Record<string, unknown> | null;
|
|
after: Record<string, unknown> | null;
|
|
metadata: Record<string, unknown> | null;
|
|
created_at: string;
|
|
actor: { name: string; email: string } | null;
|
|
};
|
|
|
|
export default function AuditIndex({
|
|
logs,
|
|
actors,
|
|
events,
|
|
filters,
|
|
}: {
|
|
logs: Paginated<AuditLog>;
|
|
actors: { id: number; name: string }[];
|
|
events: string[];
|
|
filters: {
|
|
event?: string;
|
|
actor_id?: number;
|
|
date_from?: string;
|
|
date_to?: string;
|
|
};
|
|
}) {
|
|
const [event, setEvent] = useState(filters.event ?? '');
|
|
const [actorId, setActorId] = useState(filters.actor_id?.toString() ?? '');
|
|
const [dateFrom, setDateFrom] = useState(filters.date_from ?? '');
|
|
const [dateTo, setDateTo] = useState(filters.date_to ?? '');
|
|
|
|
function filter(submitEvent: FormEvent) {
|
|
submitEvent.preventDefault();
|
|
router.get('/auditoria', {
|
|
event: event || undefined,
|
|
actor_id: actorId || undefined,
|
|
date_from: dateFrom || undefined,
|
|
date_to: dateTo || undefined,
|
|
});
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Head title="Auditoria" />
|
|
<main className="mx-auto flex w-full max-w-7xl flex-1 flex-col gap-7 p-5 sm:p-8">
|
|
<PageHeading
|
|
eyebrow="Controlo"
|
|
title="Registo de auditoria"
|
|
description="Histórico imutável das decisões, operações e alterações administrativas do portal."
|
|
/>
|
|
<form
|
|
onSubmit={filter}
|
|
className="grid gap-2 rounded-xl border bg-card p-4 lg:grid-cols-[1fr_220px_160px_160px_auto]"
|
|
>
|
|
<div className="relative">
|
|
<Search className="absolute top-2.5 left-3 size-4 text-muted-foreground" />
|
|
<Input
|
|
className="pl-9"
|
|
list="audit-events"
|
|
value={event}
|
|
onChange={(e) => setEvent(e.target.value)}
|
|
placeholder="Evento…"
|
|
/>
|
|
<datalist id="audit-events">
|
|
{events.map((item) => (
|
|
<option key={item} value={item} />
|
|
))}
|
|
</datalist>
|
|
</div>
|
|
<select
|
|
className="rounded-md border bg-transparent px-3 text-sm"
|
|
value={actorId}
|
|
onChange={(e) => setActorId(e.target.value)}
|
|
>
|
|
<option value="">Todos os autores</option>
|
|
{actors.map((actor) => (
|
|
<option key={actor.id} value={actor.id}>
|
|
{actor.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<Input
|
|
type="date"
|
|
value={dateFrom}
|
|
onChange={(e) => setDateFrom(e.target.value)}
|
|
aria-label="Data inicial"
|
|
/>
|
|
<Input
|
|
type="date"
|
|
value={dateTo}
|
|
onChange={(e) => setDateTo(e.target.value)}
|
|
aria-label="Data final"
|
|
/>
|
|
<Button type="submit" variant="secondary">
|
|
Filtrar
|
|
</Button>
|
|
</form>
|
|
<section className="space-y-3">
|
|
{logs.data.map((log) => (
|
|
<Card key={log.id} className="gap-3 py-4 shadow-none">
|
|
<CardContent className="space-y-2">
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
<p className="flex items-center gap-2 font-mono text-sm font-semibold">
|
|
<ScrollText className="size-4 text-primary" />
|
|
{log.event}
|
|
</p>
|
|
<time className="text-xs text-muted-foreground">
|
|
{new Intl.DateTimeFormat('pt-PT', {
|
|
dateStyle: 'short',
|
|
timeStyle: 'medium',
|
|
}).format(new Date(log.created_at))}
|
|
</time>
|
|
</div>
|
|
<p className="text-sm text-muted-foreground">
|
|
{log.actor
|
|
? `${log.actor.name} · ${log.actor.email}`
|
|
: 'Sistema'}
|
|
{log.auditable_type
|
|
? ` · ${log.auditable_type} #${log.auditable_id}`
|
|
: ''}
|
|
</p>
|
|
{(log.before || log.after || log.metadata) && (
|
|
<details>
|
|
<summary className="cursor-pointer text-xs font-medium text-primary">
|
|
Ver dados técnicos
|
|
</summary>
|
|
<pre className="mt-2 overflow-x-auto rounded bg-slate-950 p-3 text-xs text-slate-100">
|
|
{JSON.stringify(
|
|
{
|
|
before: log.before,
|
|
after: log.after,
|
|
metadata: log.metadata,
|
|
},
|
|
null,
|
|
2,
|
|
)}
|
|
</pre>
|
|
</details>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</section>
|
|
{logs.last_page > 1 && (
|
|
<nav className="flex flex-wrap gap-2">
|
|
{logs.links.map((link) =>
|
|
link.url ? (
|
|
<Button
|
|
key={link.label}
|
|
asChild
|
|
size="sm"
|
|
variant={
|
|
link.active ? 'default' : 'outline'
|
|
}
|
|
>
|
|
<Link
|
|
href={link.url}
|
|
dangerouslySetInnerHTML={{
|
|
__html: link.label,
|
|
}}
|
|
/>
|
|
</Button>
|
|
) : null,
|
|
)}
|
|
</nav>
|
|
)}
|
|
</main>
|
|
</>
|
|
);
|
|
}
|
|
|
|
AuditIndex.layout = {
|
|
breadcrumbs: [{ title: 'Auditoria', href: '/auditoria' }],
|
|
};
|