import { Button, Panel, PanelBody, PanelHeader, Spinner } from '@wordpress/components'; import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { ErrorLogDetail, ErrorLogSummary, nativeGetErrorLog, nativeListErrorLogs, } from '@/utils/ai/services/nativeClient'; const getInitialLogId = () => { const value = new URLSearchParams(window.location.search).get('filter_ai_log_id'); return value ? Number.parseInt(value, 10) || null : null; }; const setLogUrl = (logId: number | null) => { const url = new URL(window.location.href); url.searchParams.set('page', 'filter_ai'); if (logId) { url.searchParams.set('filter_ai_log_id', logId.toString()); } else { url.searchParams.delete('filter_ai_log_id'); } url.hash = 'error_logs'; window.history.replaceState(null, '', `${url.pathname}${url.search}${url.hash}`); }; const ErrorLogs = () => { const [logs, setLogs] = useState([]); const [selectedLogId, setSelectedLogId] = useState(getInitialLogId); const [selectedLog, setSelectedLog] = useState(null); const [isLoadingLogs, setIsLoadingLogs] = useState(true); const [isLoadingLog, setIsLoadingLog] = useState(false); const [error, setError] = useState(''); useEffect(() => { setIsLoadingLogs(true); nativeListErrorLogs() .then((items) => { setLogs(items); setError(''); }) .catch(() => { setError(__('Unable to load Filter AI error logs.', 'filter-ai')); }) .finally(() => setIsLoadingLogs(false)); }, []); useEffect(() => { if (!selectedLogId) { setSelectedLog(null); return; } setIsLoadingLog(true); nativeGetErrorLog(selectedLogId) .then((log) => { setSelectedLog(log); setError(''); }) .catch(() => { setSelectedLog(null); setError(__('Error log not found.', 'filter-ai')); }) .finally(() => setIsLoadingLog(false)); }, [selectedLogId]); const openLog = (id: number) => { setSelectedLogId(id); setLogUrl(id); }; const showAllLogs = () => { setSelectedLogId(null); setLogUrl(null); }; return (

{__('Error Logs', 'filter-ai')}

{__( 'Recent AI provider and Filter AI errors. Entries older than one week are pruned automatically.', 'filter-ai' )}

{error && (

{error}

)} {selectedLogId ? (
{isLoadingLog && } {selectedLog && ( <>

{selectedLog.title}

{__('Date', 'filter-ai')}
{selectedLog.date}
{__('Source', 'filter-ai')}
{selectedLog.source}
{__('Message', 'filter-ai')}
{selectedLog.message}
{selectedLog.content}
)}
) : ( <> {isLoadingLogs && } {!isLoadingLogs && !logs.length &&

{__('No Filter AI errors have been logged yet.', 'filter-ai')}

} {!!logs.length && ( {logs.map((log) => ( ))}
{__('Date', 'filter-ai')} {__('Message', 'filter-ai')} {__('Details', 'filter-ai')}
{log.date} {log.message} { event.preventDefault(); openLog(log.id); }} > {__('View full response', 'filter-ai')}
)} )}
); }; export default ErrorLogs;