MCPcopy
hub / github.com/janhq/jan / LogsViewer

Function LogsViewer

web-app/src/routes/logs.tsx:15–112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13// Define log entry type
14
15function LogsViewer() {
16 const { t } = useTranslation()
17 const [logs, setLogs] = useState<LogEntry[]>([])
18 const logsContainerRef = useRef<HTMLDivElement>(null)
19 const serviceHub = useServiceHub()
20
21 useEffect(() => {
22 let lastLogsLength = 0
23 function updateLogs() {
24 serviceHub
25 .app()
26 .readLogs()
27 .then((logData) => {
28 let needScroll = false
29 const filteredLogs = logData.filter(Boolean) as LogEntry[]
30 if (filteredLogs.length > lastLogsLength) needScroll = true
31
32 lastLogsLength = filteredLogs.length
33 setLogs(filteredLogs)
34
35 // Scroll to bottom after initial logs are loaded
36 if (needScroll) setTimeout(() => scrollToBottom(), 100)
37 })
38 }
39 updateLogs()
40
41 // repeat action each 3s
42 const intervalId = setInterval(() => updateLogs(), 3000)
43
44 return () => {
45 clearInterval(intervalId)
46 }
47 }, [serviceHub])
48
49 // Function to scroll to the bottom of the logs container
50 const scrollToBottom = () => {
51 if (logsContainerRef.current) {
52 const { scrollHeight, clientHeight } = logsContainerRef.current
53 logsContainerRef.current.scrollTop = scrollHeight - clientHeight
54 }
55 }
56
57 // Function to get appropriate color for log level
58 const getLogLevelColor = (level: string) => {
59 switch (level) {
60 case 'error':
61 return 'text-red-500'
62 case 'warn':
63 return 'text-yellow-500'
64 case 'info':
65 return 'text-blue-500'
66 case 'debug':
67 return 'text-gray-500'
68 default:
69 return 'text-gray-500'
70 }
71 }
72

Callers

nothing calls this directly

Calls 7

useTranslationFunction · 0.90
useServiceHubFunction · 0.90
updateLogsFunction · 0.85
tFunction · 0.85
formatTimestampFunction · 0.70
getLogLevelColorFunction · 0.70
mapMethod · 0.45

Tested by

no test coverage detected