2026-07-16 06:19:58 -04:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from '@/components/ui/card';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { AlertTriangle, Trash2 } from 'lucide-react';
|
2026-07-18 19:05:52 -04:00
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
} from '@/components/ui/alert-dialog';
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
interface ErrorLog {
|
|
|
|
|
id: string;
|
|
|
|
|
level: string;
|
|
|
|
|
source: string;
|
|
|
|
|
message: string;
|
|
|
|
|
metadata: Record<string, unknown>;
|
|
|
|
|
created: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function ErrorLogPage() {
|
|
|
|
|
const [errors, setErrors] = useState<ErrorLog[]>([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
2026-07-18 19:05:52 -04:00
|
|
|
const [clearing, setClearing] = useState(false);
|
|
|
|
|
const [confirmClear, setConfirmClear] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
const [status, setStatus] = useState<string | null>(null);
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchErrors();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
async function fetchErrors() {
|
2026-07-18 19:05:52 -04:00
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
2026-07-16 06:19:58 -04:00
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/error-logs?limit=50');
|
2026-07-18 19:05:52 -04:00
|
|
|
if (!response.ok) throw new Error('Unable to load error logs.');
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
setErrors(data.items || []);
|
2026-07-16 06:19:58 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to fetch error logs:', error);
|
2026-07-18 19:05:52 -04:00
|
|
|
setError('Unable to load error logs. Please try again.');
|
2026-07-16 06:19:58 -04:00
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function clearErrors() {
|
2026-07-18 19:05:52 -04:00
|
|
|
setClearing(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
setStatus(null);
|
2026-07-16 06:19:58 -04:00
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/error-logs', { method: 'DELETE' });
|
2026-07-18 19:05:52 -04:00
|
|
|
if (!response.ok) throw new Error('Unable to clear error logs.');
|
|
|
|
|
setErrors([]);
|
|
|
|
|
setConfirmClear(false);
|
|
|
|
|
setStatus('Error logs cleared successfully.');
|
2026-07-16 06:19:58 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to clear error logs:', error);
|
2026-07-18 19:05:52 -04:00
|
|
|
setError('Unable to clear error logs. Please try again.');
|
|
|
|
|
} finally {
|
|
|
|
|
setClearing(false);
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle>Error Log</CardTitle>
|
|
|
|
|
<CardDescription>Loading...</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<CardTitle>Error Log</CardTitle>
|
|
|
|
|
<CardDescription>
|
|
|
|
|
Recent errors from the application (auto-purged after 30 days)
|
|
|
|
|
</CardDescription>
|
|
|
|
|
</div>
|
|
|
|
|
{errors.length > 0 && (
|
2026-07-18 19:05:52 -04:00
|
|
|
<AlertDialog open={confirmClear} onOpenChange={setConfirmClear}>
|
|
|
|
|
<Button variant="outline" size="sm" onClick={() => setConfirmClear(true)} aria-label="Clear all error logs">
|
|
|
|
|
<Trash2 className="mr-2 h-4 w-4" aria-hidden="true" />
|
|
|
|
|
Clear all
|
|
|
|
|
</Button>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>Clear all error logs?</AlertDialogTitle>
|
|
|
|
|
<AlertDialogDescription>This permanently removes all displayed error logs.</AlertDialogDescription>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel disabled={clearing}>Cancel</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction onClick={clearErrors} disabled={clearing}>
|
|
|
|
|
{clearing ? 'Clearing...' : 'Clear all'}
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
2026-07-16 06:19:58 -04:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2026-07-18 19:05:52 -04:00
|
|
|
{error && (
|
|
|
|
|
<div className="mb-4 flex items-center justify-between gap-3 text-sm text-destructive" role="alert">
|
|
|
|
|
<span>{error}</span>
|
|
|
|
|
<Button variant="outline" size="sm" onClick={fetchErrors} disabled={loading}>Retry</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{status && <p className="mb-4 text-sm text-muted-foreground" role="status">{status}</p>}
|
2026-07-16 06:19:58 -04:00
|
|
|
{errors.length === 0 ? (
|
|
|
|
|
<p className="text-center text-muted-foreground py-8">
|
|
|
|
|
No errors logged
|
|
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
{errors.map((error) => (
|
|
|
|
|
<div
|
|
|
|
|
key={error.id}
|
|
|
|
|
className="rounded-lg border border-border bg-card p-4 space-y-2"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-start justify-between gap-2">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<AlertTriangle className="h-4 w-4 text-destructive" aria-hidden="true" />
|
|
|
|
|
<span className="text-sm font-medium">{error.level}</span>
|
|
|
|
|
<span className="text-xs text-muted-foreground">
|
|
|
|
|
{error.source}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<span className="text-xs text-muted-foreground">
|
|
|
|
|
{new Date(error.created).toLocaleString()}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-sm">{error.message}</p>
|
|
|
|
|
{error.metadata &&
|
|
|
|
|
Object.keys(error.metadata).length > 0 && (
|
|
|
|
|
<details className="text-xs">
|
|
|
|
|
<summary className="cursor-pointer text-muted-foreground hover:text-foreground" role="button">
|
|
|
|
|
Details
|
|
|
|
|
</summary>
|
|
|
|
|
<pre className="mt-2 rounded bg-muted p-2 overflow-x-auto">
|
|
|
|
|
{JSON.stringify(error.metadata, null, 2)}
|
|
|
|
|
</pre>
|
|
|
|
|
</details>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|