'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'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; interface ErrorLog { id: string; level: string; source: string; message: string; metadata: Record; created: string; } export default function ErrorLogPage() { const [errors, setErrors] = useState([]); const [loading, setLoading] = useState(true); const [clearing, setClearing] = useState(false); const [confirmClear, setConfirmClear] = useState(false); const [error, setError] = useState(null); const [status, setStatus] = useState(null); useEffect(() => { fetchErrors(); }, []); async function fetchErrors() { setLoading(true); setError(null); try { const response = await fetch('/api/error-logs?limit=50'); if (!response.ok) throw new Error('Unable to load error logs.'); const data = await response.json(); setErrors(data.items || []); } catch (error) { console.error('Failed to fetch error logs:', error); setError('Unable to load error logs. Please try again.'); } finally { setLoading(false); } } async function clearErrors() { setClearing(true); setError(null); setStatus(null); try { const response = await fetch('/api/error-logs', { method: 'DELETE' }); if (!response.ok) throw new Error('Unable to clear error logs.'); setErrors([]); setConfirmClear(false); setStatus('Error logs cleared successfully.'); } catch (error) { console.error('Failed to clear error logs:', error); setError('Unable to clear error logs. Please try again.'); } finally { setClearing(false); } } if (loading) { return ( Error Log Loading... ); } return (
Error Log Recent errors from the application (auto-purged after 30 days)
{errors.length > 0 && ( Clear all error logs? This permanently removes all displayed error logs. Cancel {clearing ? 'Clearing...' : 'Clear all'} )}
{error && (
{error}
)} {status &&

{status}

} {errors.length === 0 ? (

No errors logged

) : (
{errors.map((error) => (
{new Date(error.created).toLocaleString()}

{error.message}

{error.metadata && Object.keys(error.metadata).length > 0 && (
Details
                        {JSON.stringify(error.metadata, null, 2)}
                      
)}
))}
)}
); }