27 lines
2.5 KiB
TypeScript
27 lines
2.5 KiB
TypeScript
import Link from 'next/link';
|
|
import { cookies } from 'next/headers';
|
|
import { AdminConsole } from '../../components/admin-console';
|
|
|
|
type AuditEvent = { id: string; event_type: string; actor_user_id: string | null; target_user_id: string | null; created_at: string; metadata: Record<string, unknown> | null };
|
|
|
|
async function loadAuditEvents(): Promise<{ status: number; events: AuditEvent[] }> {
|
|
const token = (await cookies()).get('motorsignal_access_token')?.value;
|
|
if (!token) return { status: 401, events: [] };
|
|
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
|
|
try {
|
|
const response = await fetch(`${baseUrl}/api/v1/admin/audit-events`, { headers: { authorization: `Bearer ${token}` }, cache: 'no-store' });
|
|
if (!response.ok) return { status: response.status, events: [] };
|
|
return { status: response.status, events: await response.json() as AuditEvent[] };
|
|
} catch {
|
|
return { status: 503, events: [] };
|
|
}
|
|
}
|
|
|
|
export default async function AdminPage() {
|
|
const result = await loadAuditEvents();
|
|
if (result.status === 401) return <section className="content-page"><span className="eyebrow">Admin console</span><h1>Sign in required.</h1><p>Admin tools require an authenticated MotorSignal session.</p><Link className="button" href="/login">Sign in</Link></section>;
|
|
if (result.status === 403) return <section className="content-page"><span className="eyebrow">Admin console</span><h1>Access restricted.</h1><p>Your account does not have support or admin permissions.</p><Link className="button button-secondary" href="/account">Back to account</Link></section>;
|
|
if (result.status !== 200) return <section className="content-page"><span className="eyebrow">Admin console</span><h1>Console unavailable.</h1><p>Audit data could not be loaded. Try again when the API is available.</p></section>;
|
|
return <section className="content-page"><span className="eyebrow">Admin console</span><h1>Operations.</h1><p>Manage users, vehicles, rides, and review recent account activity.</p><AdminConsole /><h2 className="admin-audit-heading">Recent audit events</h2><div className="audit-list">{result.events.length === 0 ? <div className="panel"><p>No events recorded.</p></div> : result.events.map((event) => <article className="panel audit-row" key={event.id}><div><strong>{event.event_type}</strong><p>{event.actor_user_id ?? 'System'} {event.target_user_id ? `targeted ${event.target_user_id}` : ''}</p></div><time dateTime={event.created_at}>{new Date(event.created_at).toLocaleString()}</time></article>)}</div></section>;
|
|
}
|