import { createFileRoute } from '@tanstack/react-router' import { route } from '@/constants/routes' import { useEffect, useState, useRef } from 'react' import { useServiceHub } from '@/hooks/useServiceHub' import { useTranslation } from '@/i18n/react-i18next-compat' import { PlatformGuard } from '@/lib/platform/PlatformGuard' import { PlatformFeature } from '@/lib/platform' // eslint-disable-next-line @typescript-eslint/no-explicit-any export const Route = createFileRoute(route.appLogs as any)({ component: LogsViewerGuarded, }) function LogsViewerGuarded() { return ( ) } // Define log entry type function LogsViewer() { const { t } = useTranslation() const [logs, setLogs] = useState([]) const logsContainerRef = useRef(null) const serviceHub = useServiceHub() useEffect(() => { let lastLogsLength = 0 function updateLogs() { serviceHub .app() .readLogs() .then((logData) => { let needScroll = false const filteredLogs = logData.filter(Boolean) as LogEntry[] if (filteredLogs.length > lastLogsLength) needScroll = true lastLogsLength = filteredLogs.length setLogs(filteredLogs) // Scroll to bottom after initial logs are loaded if (needScroll) setTimeout(() => scrollToBottom(), 100) }) } updateLogs() // repeat action each 3s const intervalId = setInterval(() => updateLogs(), 3000) return () => { clearInterval(intervalId) } }, [serviceHub]) // Function to scroll to the bottom of the logs container const scrollToBottom = () => { if (logsContainerRef.current) { const { scrollHeight, clientHeight } = logsContainerRef.current logsContainerRef.current.scrollTop = scrollHeight - clientHeight } } // Function to get appropriate color for log level const getLogLevelColor = (level: string) => { switch (level) { case 'error': return 'text-red-500' case 'warn': return 'text-yellow-500' case 'info': return 'text-blue-500' case 'debug': return 'text-gray-500' default: return 'text-gray-500' } } // Format timestamp to be more readable const formatTimestamp = (timestamp: string | number) => { const date = new Date(timestamp) return date.toLocaleTimeString() } return ( {logs.length === 0 ? ( {t('logs:noLogs')} ) : ( logs.map((log, index) => ( [{formatTimestamp(log.timestamp)}] {log.level.toUpperCase()} {log.message} )) )} ) }