import { useEffect, useRef, useState } from 'react'; import { toast } from 'react-toastify'; import DownloadIcon from '@mui/icons-material/GetApp'; import PlayArrowIcon from '@mui/icons-material/PlayArrow'; import WarningIcon from '@mui/icons-material/Warning'; import { Box, Button, Checkbox, Grid2 as Grid, IconButton, MenuItem, TextField, styled } from '@mui/material'; import { API } from 'api/app'; import { fetchLogES, readLogSettings, updateLogSettings } from 'api/system'; import { useRequest, useSSE } from 'alova/client'; import { BlockFormControlLabel, BlockNavigation, FormLoader, SectionContent, useLayoutTitle } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; import type { LogEntry, LogSettings } from 'types'; import { LogLevel } from 'types'; import { updateValueDirty, useRest } from 'utils'; const TextColors = { [LogLevel.ERROR]: '#ff0000', // red [LogLevel.WARNING]: '#ff0000', // red [LogLevel.NOTICE]: '#ffffff', // white [LogLevel.INFO]: '#ffcc00', // yellow [LogLevel.DEBUG]: '#00ffff', // cyan [LogLevel.TRACE]: '#00ffff' // cyan }; const LogEntryLine = styled('span')( ({ details: { level } }: { details: { level: LogLevel } }) => ({ color: TextColors[level] }) ); const topOffset = () => document.getElementById('log-window')?.getBoundingClientRect().bottom || 0; const leftOffset = () => document.getElementById('log-window')?.getBoundingClientRect().left || 0; const levelLabel = (level: LogLevel) => { switch (level) { case LogLevel.ERROR: return 'ERROR'; case LogLevel.WARNING: return 'WARNING'; case LogLevel.NOTICE: return 'NOTICE'; case LogLevel.INFO: return 'INFO'; case LogLevel.DEBUG: return 'DEBUG'; case LogLevel.TRACE: return 'TRACE'; default: return ''; } }; const SystemLog = () => { const { LL } = useI18nContext(); useLayoutTitle(LL.LOG_OF(LL.SYSTEM(0))); const { loadData, data, updateDataValue, origData, dirtyFlags, setDirtyFlags, blocker, saveData, errorMessage } = useRest({ read: readLogSettings, update: updateLogSettings }); const { send } = useRequest( (data: string) => API({ device: 'system', cmd: 'read', id: 0, data: data }), { immediate: false } ); const [readValue, setReadValue] = useState(''); const [readOpen, setReadOpen] = useState(false); const [logEntries, setLogEntries] = useState([]); const [autoscroll, setAutoscroll] = useState(true); const [lastId, setLastId] = useState(-1); const ALPHA_NUMERIC_DASH_REGEX = /^[a-fA-F0-9 ]+$/; const updateFormValue = updateValueDirty( origData, dirtyFlags, setDirtyFlags, updateDataValue ); useSSE(fetchLogES, { immediate: true, interceptByGlobalResponded: false }) .onMessage((message: { data: string }) => { const rawData = message.data; const logentry = JSON.parse(rawData) as LogEntry; if (lastId < logentry.i) { setLogEntries((log) => [...log, logentry]); setLastId(logentry.i); } }) .onError(() => { toast.error('No connection to Log service'); }); const paddedLevelLabel = (level: LogLevel) => { const label = levelLabel(level); return data?.compact ? ' ' + label[0] : label.padStart(8, '\xa0'); }; const paddedNameLabel = (name: string) => { const label = '[' + name + ']'; return data?.compact ? label : label.padEnd(12, '\xa0'); }; const paddedIDLabel = (id: number) => { const label = id + ':'; return data?.compact ? label : label.padEnd(7, '\xa0'); }; const onDownload = () => { let result = ''; for (const i of logEntries) { result += i.t + ' ' + levelLabel(i.l) + ' ' + i.i + ': [' + i.n + '] ' + i.m + '\n'; } const a = document.createElement('a'); a.setAttribute( 'href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(result) ); a.setAttribute('download', 'log.txt'); document.body.appendChild(a); a.click(); document.body.removeChild(a); }; const saveSettings = async () => { await saveData(); }; // handle scrolling const ref = useRef(null); useEffect(() => { if (logEntries.length && autoscroll) { ref.current?.scrollIntoView({ behavior: 'smooth', block: 'end' }); } }, [logEntries.length]); const sendReadCommand = () => { if (readValue === '') { setReadOpen(!readOpen); return; } if (readValue.split(' ').filter((word) => word !== '').length > 1) { void send(readValue); setReadOpen(false); setReadValue(''); } }; const content = () => { if (!data) { return ; } return ( <> OFF ERROR WARNING NOTICE INFO ALL {data.psram && ( 25 50 75 100 )} } label={LL.COMPACT()} /> setAutoscroll(!autoscroll)} name="autoscroll" /> } label={LL.AUTO_SCROLL()} /> {readOpen ? ( { e.preventDefault(); sendReadCommand(); }} > { setReadOpen(false); setReadValue(''); }} > { const value = e.target.value; if (value !== '' && !ALPHA_NUMERIC_DASH_REGEX.test(value)) { return; } setReadValue(value); }} focused={true} size="small" label="Send Read command" // doesn't need translating - developer only helperText=" [offset] [len]" /> ) : ( <> {data.developer_mode && ( )} )} {dirtyFlags && dirtyFlags.length !== 0 && ( )} leftOffset(), top: () => topOffset(), p: 1 }} > {logEntries.map((e) => (
{e.t} {paddedLevelLabel(e.l)}  {paddedIDLabel(e.i)} {paddedNameLabel(e.n)} {e.m}
))}
); }; return ( {blocker ? : null} {content()} ); }; export default SystemLog;