import Refresh from '@mui/icons-material/Refresh' import VerticalAlignBottom from '@mui/icons-material/VerticalAlignBottom' import { Alert, Box, Button, MenuItem, Tab, Tabs, TextField, ToggleButton } from '@mui/material' import { useCallback, useEffect, useState } from 'react' import LogView from './LogView.tsx' import Toolbar from './Toolbar.tsx' import { LOGS_SHAPE, LOG_LINES_SHAPE, request, type Logs } from './api.ts' /** The numbers of lines offered, up to the server's most. */ const LINE_CHOICES = [100, 200, 500, 1000] /** Seconds between the reads of a followed log. */ const FOLLOW_SECONDS = 2 /** * The server's logs, from its /logs: the Journal sub-tab shows the last lines of the journal of a unit the server names, or of * the whole journal, and the Files sub-tab the last lines of a log file the server's web.log_files lists. The number of lines * is chosen up to the server's most; a log is read when its unit, file or number of lines is chosen and with Refresh, and * while Follow is on every FOLLOW_SECONDS, scrolled to its last line, as tail -f shows it. */ export default function LogsPanel() { const [sources, setSources] = useState(null) const [kind, setKind] = useState<'journal' | 'file'>('journal') const [unit, setUnit] = useState('') const [file, setFile] = useState('') const [count, setCount] = useState(200) const [lines, setLines] = useState(null) const [error, setError] = useState(null) const [busy, setBusy] = useState(false) const [follow, setFollow] = useState(false) useEffect(() => { request('GET', '/logs', LOGS_SHAPE) .then((found) => { setSources(found) setFile((current) => current || found.files[0] || '') }) .catch((caught) => setError((caught as Error).message)) }, []) // The query of the log chosen, null while the Files tab has no file. const query = kind === 'file' && !file ? null : kind === 'journal' ? `/logs/journal?lines=${count}${unit ? `&unit=${encodeURIComponent(unit)}` : ''}` : `/logs/file?lines=${count}&path=${encodeURIComponent(file)}` // The query whose answer is shown. const [shown, setShown] = useState(null) const reading = busy || (sources !== null && query !== null && shown !== query) /** Reads the log chosen; the state changes only once the server answers. */ const read = useCallback(async () => { if (query === null) { return } await request<{ lines: string[] }>('GET', query, LOG_LINES_SHAPE) .then((found) => { setLines(found.lines) setError(null) }, (caught) => { setLines(null) setError((caught as Error).message) }) .finally(() => { setShown(query) setBusy(false) }) }, [query]) /** Reads the log again, busy until the server answers. */ const refresh = useCallback(() => { setBusy(true) return read() }, [read]) useEffect(() => { if (sources) { void read() } }, [sources, read]) useEffect(() => { if (!follow || !sources) { return } const timer = window.setInterval(() => void read(), FOLLOW_SECONDS * 1000) return () => window.clearInterval(timer) }, [follow, sources, read]) return ( setKind(next)} sx={{ mb: 2 }}> {kind === 'journal' ? ( setUnit(event.target.value)} sx={{ minWidth: 240 }}> All units {(sources?.units ?? []).map((name) => {name})} ) : ( setFile(event.target.value)} sx={{ minWidth: 240 }} > {(sources?.files ?? []).map((name) => {name})} )} setCount(Number(event.target.value))} sx={{ minWidth: 100 }}> {LINE_CHOICES.filter((choice) => choice <= (sources?.max_lines ?? 0)).map((choice) => {choice})} setFollow(!follow)}> Follow {error && {error}} {kind === 'file' && sources && sources.files.length === 0 && No log files are configured.} ) }