From 0ff0c4eb7d795ee27f97c0abf5973ba5baa31f82 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 27 Sep 2026 16:17:45 -0500 Subject: reuse the explorer from /files --- src/web/src/components/System/Logs/Explorer.jsx | 202 ++++++++++++++++++++++++ src/web/src/components/System/Logs/Live.jsx | 59 ++----- src/web/src/components/System/Logs/index.jsx | 35 ++++ 3 files changed, 249 insertions(+), 47 deletions(-) create mode 100644 src/web/src/components/System/Logs/Explorer.jsx create mode 100644 src/web/src/components/System/Logs/index.jsx (limited to 'src') diff --git a/src/web/src/components/System/Logs/Explorer.jsx b/src/web/src/components/System/Logs/Explorer.jsx new file mode 100644 index 00000000..ac6d982f --- /dev/null +++ b/src/web/src/components/System/Logs/Explorer.jsx @@ -0,0 +1,202 @@ +import '../Files/Files.css'; +import { getFileBlob, list } from '../../../lib/logs'; +import { downloadFile, formatBytes, formatDate } from '../../../lib/util'; +import { LoaderSegment } from '../../Shared'; +import React, { useEffect, useState } from 'react'; +import { toast } from 'react-toastify'; +import { Header, Icon, Table } from 'semantic-ui-react'; + +const view = async (filename) => { + const tab = window.open('', '_blank'); + + try { + const blob = await getFileBlob({ filename }); + + // hack: we can't pass a JWT in a plain link, so we have to fetch the blob + // and then shove it into the window rather than just displaying it + tab.location.href = window.URL.createObjectURL( + new Blob([blob], { type: 'text/plain; charset=utf-8' }), + ); + } catch (error) { + tab.close(); + toast.error(error?.message ?? error); + } +}; + +const download = async (filename) => { + try { + const blob = await getFileBlob({ filename }); + + downloadFile(blob, filename, 'text/plain'); + } catch (error) { + toast.error(error?.message ?? error); + } +}; + +const FileRow = ({ length, modifiedAt, name }) => ( + + + + {name} + + {modifiedAt ? formatDate(modifiedAt) : ''} + {length ? formatBytes(length) : ''} + + view(name)} + style={{ cursor: 'pointer', marginRight: '0.5em' }} + title={`View ${name} in a new window`} + /> + download(name)} + style={{ cursor: 'pointer' }} + title={`Download ${name}`} + /> + + +); + +const Explorer = () => { + const [files, setFiles] = useState([]); + const [loading, setLoading] = useState(false); + const [sortColumn, setSortColumn] = useState('date'); + const [sortDirection, setSortDirection] = useState('descending'); + + const fetch = async () => { + setLoading(true); + + try { + setFiles(await list()); + } catch (error) { + toast.error(error?.message ?? error); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + fetch(); + }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const handleSort = (column) => { + if (sortColumn === column) { + setSortDirection( + sortDirection === 'ascending' ? 'descending' : 'ascending', + ); + } else { + setSortColumn(column); + setSortDirection('ascending'); + } + }; + + const sortItems = (items) => { + if (!items || items.length === 0) return items; + + return [...items].sort((a, b) => { + let compareValue = 0; + + if (sortColumn === 'name') { + compareValue = a.name.localeCompare(b.name, undefined, { + numeric: true, + sensitivity: 'base', + }); + } else if (sortColumn === 'date') { + const dateA = a.modifiedAt ? new Date(a.modifiedAt) : new Date(0); + const dateB = b.modifiedAt ? new Date(b.modifiedAt) : new Date(0); + compareValue = dateA - dateB; + } + + return sortDirection === 'ascending' ? compareValue : -compareValue; + }); + }; + + if (loading) { + return ; + } + + const sortedFiles = sortItems(files); + + return ( + <> +
+ + /logs +
+ + + + handleSort('name')} + style={{ cursor: 'pointer' }} + > + Name + {sortColumn === 'name' && ( + + )} + + handleSort('date')} + style={{ cursor: 'pointer' }} + > + Date Modified + {sortColumn === 'date' && ( + + )} + + + Size + + + + + + {files.length === 0 ? ( + + + No log files + + + ) : ( + sortedFiles.map((f) => ( + + )) + )} + +
+ + ); +}; + +export default Explorer; diff --git a/src/web/src/components/System/Logs/Live.jsx b/src/web/src/components/System/Logs/Live.jsx index f36258ec..2699a2e3 100644 --- a/src/web/src/components/System/Logs/Live.jsx +++ b/src/web/src/components/System/Logs/Live.jsx @@ -1,24 +1,17 @@ import '../System.css'; import { createLogsHubConnection } from '../../../lib/hubFactory'; import { LoaderSegment } from '../../Shared'; -import React, { Component } from 'react'; -import { Table } from 'semantic-ui-react'; +import LogTable from './LogTable'; +import { Component } from 'react'; const initialState = { connected: false, logs: [], }; -const levels = { - Debug: 'DBG', - Error: 'ERR', - Information: 'INF', - Warning: 'WRN', -}; - const maxLogs = 500; -class Logs extends Component { +class Live extends Component { constructor(props) { super(props); @@ -31,14 +24,16 @@ class Logs extends Component { logsHub.on('buffer', (buffer) => { this.setState({ connected: true, - logs: buffer.reverse().slice(0, maxLogs), + logs: buffer.reverse().slice(0, maxLogs).map(this.withId), }); }); logsHub.on('log', (log) => { + const record = this.withId(log); + this.setState((previousState) => ({ connected: true, - logs: [log].concat(previousState.logs).slice(0, maxLogs), + logs: [record].concat(previousState.logs).slice(0, maxLogs), })); }); @@ -49,10 +44,9 @@ class Logs extends Component { logsHub.start(); } - formatTimestamp = (timestamp) => { - const date = new Date(timestamp); - return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`; // eslint-disable-line max-len - }; + nextId = 0; + + withId = (log) => ({ ...log, id: this.nextId++ }); render() { const { connected, logs } = this.state; @@ -60,39 +54,10 @@ class Logs extends Component { return (
{!connected && } - {connected && ( - - - - Timestamp - Level - Message - - - - {logs.map((log) => ( - - {this.formatTimestamp(log.timestamp)} - {levels[log.level] || log.level} - - {log.message} - - - ))} - -
- )} + {connected && }
); } } -export default Logs; +export default Live; diff --git a/src/web/src/components/System/Logs/index.jsx b/src/web/src/components/System/Logs/index.jsx new file mode 100644 index 00000000..f0fa4a58 --- /dev/null +++ b/src/web/src/components/System/Logs/index.jsx @@ -0,0 +1,35 @@ +import Explorer from './Explorer'; +import Live from './Live'; +import React from 'react'; +import { Tab } from 'semantic-ui-react'; + +const Logs = () => { + const panes = [ + { + menuItem: 'Live', + render: () => ( + + + + ), + route: 'live', + }, + { + menuItem: 'History', + render: () => ( + + + + ), + route: 'history', + }, + ]; + + return ( +
+ +
+ ); +}; + +export default Logs; -- cgit v1.2.3