diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-05-26 16:52:36 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-05-26 16:52:36 -0500 |
| commit | 59623e20ac93565bf377457f70cff87fc4b6e459 (patch) | |
| tree | 22592dce3239f682644629d8033f6559e4dd840d /src | |
| parent | 0974475adc31aa5b4179c427ba7670fc96874481 (diff) | |
| download | slskd-59623e20ac93565bf377457f70cff87fc4b6e459.tar.gz | |
refactor System components
Diffstat (limited to 'src')
| -rw-r--r-- | src/web/src/components/System/Info/index.js (renamed from src/web/src/components/System/Info.js) | 28 | ||||
| -rw-r--r-- | src/web/src/components/System/Logs.js | 83 | ||||
| -rw-r--r-- | src/web/src/components/System/Options/DebugModal.js | 67 | ||||
| -rw-r--r-- | src/web/src/components/System/Options/EditModal.js (renamed from src/web/src/components/System/Options/Edit.js) | 80 | ||||
| -rw-r--r-- | src/web/src/components/System/Options/View.js | 30 | ||||
| -rw-r--r-- | src/web/src/components/System/Options/index.js | 74 | ||||
| -rw-r--r-- | src/web/src/components/System/System.css | 36 | ||||
| -rw-r--r-- | src/web/src/components/System/System.js | 37 |
8 files changed, 243 insertions, 192 deletions
diff --git a/src/web/src/components/System/Info.js b/src/web/src/components/System/Info/index.js index d9ce8813..f640716e 100644 --- a/src/web/src/components/System/Info.js +++ b/src/web/src/components/System/Info/index.js @@ -1,25 +1,21 @@ import React from 'react'; import YAML from 'yaml'; -import { restart, shutdown, getVersion } from '../../lib/application'; +import { restart, shutdown, getVersion } from '../../../lib/application'; -import { Modal, Header } from 'semantic-ui-react'; -import CodeEditor from '../Shared/CodeEditor'; -import ShrinkableButton from '../Shared/ShrinkableButton'; +import { Modal, Header, Divider } from 'semantic-ui-react'; + +import { + CodeEditor, + ShrinkableButton, +} from '../../Shared'; const Info = ({ state }) => { const stateAsYaml = YAML.stringify(state, { simpleKeys: true, sortMapEntries: true }); return ( <> - <div className='view-code-container'> - <CodeEditor - value={stateAsYaml} - basicSetup={false} - editable={false} - /> - </div> - <div className='footer-buttons'> + <div className='header-buttons'> <div style={{float: 'left'}}> <ShrinkableButton icon='refresh' @@ -63,6 +59,14 @@ const Info = ({ state }) => { actions={['Cancel', { key: 'done', content: 'Restart', negative: true, onClick: restart }]} /> </div> + <Divider/> + <div className='view-code-container'> + <CodeEditor + value={stateAsYaml} + basicSetup={false} + editable={false} + /> + </div> </> ); }; diff --git a/src/web/src/components/System/Logs.js b/src/web/src/components/System/Logs.js deleted file mode 100644 index 4a118414..00000000 --- a/src/web/src/components/System/Logs.js +++ /dev/null @@ -1,83 +0,0 @@ -import React, { Component } from 'react'; -import { Table, Loader, Dimmer } from 'semantic-ui-react'; - -import { createLogsHubConnection } from '../../lib/hubFactory'; - -import './System.css'; - -const initialState = { - logs: [], - connected: false, -}; - -const levels = { - 'Debug': 'DBG', - 'Warning': 'WRN', - 'Error': 'ERR', - 'Information': 'INF', -}; - -const maxLogs = 500; - -class Logs extends Component { - state = initialState; - - componentDidMount = () => { - const logsHub = createLogsHubConnection(); - - logsHub.on('buffer', (buffer) => { - this.setState({ connected: true, logs: buffer.reverse().slice(0, maxLogs) }); - }); - - logsHub.on('log', (log) => { - this.setState({ connected: true, logs: [log].concat(this.state.logs).slice(0, maxLogs) }); - }); - - logsHub.onreconnecting(() => this.setState({ connected: false })); - logsHub.onclose(() => this.setState({ connected: false })); - logsHub.onreconnected(() => this.setState({ connected: true })); - - 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 - }; - - render = () => { - const { connected, logs } = this.state; - - return ( - <div className='logs'> - {!connected && <Dimmer active inverted><Loader inverted/></Dimmer>} - {connected && - <Table compact='very' className='logs-table'> - <Table.Header> - <Table.Row> - <Table.HeaderCell>Timestamp</Table.HeaderCell> - <Table.HeaderCell>Level</Table.HeaderCell> - <Table.HeaderCell>Message</Table.HeaderCell> - </Table.Row> - </Table.Header> - <Table.Body className='logs-table-body'> - {logs.map((log, index) => - <Table.Row - key={index} - disabled={log.level === 'Debug'} - warning={log.level === 'Warning'} - negative={log.level === 'Error'} - > - <Table.Cell>{this.formatTimestamp(log.timestamp)}</Table.Cell> - <Table.Cell>{levels[log.level] || log.level}</Table.Cell> - <Table.Cell className='logs-table-message'>{log.message}</Table.Cell> - </Table.Row>)} - </Table.Body> - </Table> - } - </div> - ); - }; -} - -export default Logs;
\ No newline at end of file diff --git a/src/web/src/components/System/Options/DebugModal.js b/src/web/src/components/System/Options/DebugModal.js new file mode 100644 index 00000000..2f01eb16 --- /dev/null +++ b/src/web/src/components/System/Options/DebugModal.js @@ -0,0 +1,67 @@ +import React, { useState, useEffect } from 'react'; +import { toast } from 'react-toastify'; + +import { + Modal, + Button, + Icon, +} from 'semantic-ui-react'; + +import { getCurrentDebugView } from '../../../lib/options'; +import { CodeEditor, PlaceholderSegment, Switch } from '../../Shared'; + +const DebugModal = ({ open, onClose }) => { + const [loading, setLoading] = useState(true); + const [debugView, setDebugView] = useState(); + + useEffect(() => { + if (open) { + get(); + } + }, [open]); + + const get = async () => { + setLoading(true); + + try { + setDebugView(await getCurrentDebugView()); + } catch (error) { + console.error(error); + toast.error(error?.response?.data ?? error?.message ?? error); + } finally { + setLoading(false); + } + }; + + return ( + <Modal + size='large' + open={open} + onClose={onClose} + > + <Modal.Header> + <Icon name='bug'/> + Options (Debug View) + </Modal.Header> + <Modal.Content className='debug-view-content' scrolling> + <Switch + loading={loading && <PlaceholderSegment loading={true} />} + > + <div className='view-code-container'> + <CodeEditor + value={debugView} + basicSetup={false} + editable={false} + /> + </div> + </Switch> + </Modal.Content> + <Modal.Actions> + <Button onClick={onClose}> + Close + </Button> + </Modal.Actions> + </Modal>); +}; + +export default DebugModal;
\ No newline at end of file diff --git a/src/web/src/components/System/Options/Edit.js b/src/web/src/components/System/Options/EditModal.js index f9657022..b02cbf99 100644 --- a/src/web/src/components/System/Options/Edit.js +++ b/src/web/src/components/System/Options/EditModal.js @@ -1,20 +1,25 @@ import React, { useState, useEffect } from 'react'; -import { Button, Icon, Message } from 'semantic-ui-react'; +import { Button, Icon, Message, Modal } from 'semantic-ui-react'; import CodeEditor from '../../Shared/CodeEditor'; import { getYamlLocation, getYaml, validateYaml, updateYaml } from '../../../lib/options'; -import PlaceholderSegment from '../../Shared/PlaceholderSegment'; +import { + PlaceholderSegment, + Switch, +} from '../../Shared'; -const Edit = ({ cancelAction }) => { +const EditModal = ({ open, onClose }) => { const [{ loading, error }, setLoading] = useState({ loading: true, error: false }); const [{ location, yaml, isDirty }, setYaml] = useState({ location: undefined, yaml: undefined, isDirty: false }); const [yamlError, setYamlError] = useState(); const [updateError, setUpdateError] = useState(); useEffect(() => { - get(); - }, []); + if (open) { + get(); + } + }, [open]); const get = async () => { setLoading({ loading: true, error: false }); @@ -45,7 +50,7 @@ const Edit = ({ cancelAction }) => { if (!yamlError) { try { await updateYaml({ yaml }); - cancelAction(); + onClose(); } catch (error) { setUpdateError(error.response.data); @@ -53,35 +58,44 @@ const Edit = ({ cancelAction }) => { } }; - if (loading) { - return <PlaceholderSegment loading={true}/>; - } - - if (error) { - return <PlaceholderSegment icon='close'/>; - } - return ( - <> - <Message className='no-grow' warning> - <Icon name='warning sign'/>Editing {location} - </Message> - <div - {...{ className: (yamlError || updateError) ? 'edit-code-container-error' : 'edit-code-container' }} - > - <CodeEditor - value={yaml} - onChange={(value) => update(value)} - /> - </div> - {(yamlError || updateError) && - <Message className='no-grow' negative><Icon name='x' />{(yamlError ?? '') + (updateError ?? '')}</Message>} - <div className='footer-buttons'> + <Modal + size='large' + open={open} + onClose={onClose} + > + <Modal.Header> + <Icon name='edit'/> + Edit Options + <Message className='no-grow edit-code-header' warning> + <Icon name='warning sign'/>Editing {location} + </Message> + </Modal.Header> + <Modal.Content className='edit-code-content' scrolling> + <Switch + loading={loading && <PlaceholderSegment loading={true}/>} + error={error && <PlaceholderSegment icon='close'/>} + > + <div + {...{ className: (yamlError || updateError) ? 'edit-code-container-error' : 'edit-code-container' }} + > + <CodeEditor + value={yaml} + onChange={(value) => update(value)} + /> + </div> + </Switch> + </Modal.Content> + <Modal.Actions> + {(yamlError || updateError) && + <Message className='no-grow left-align' negative> + <Icon name='x' />{(yamlError ?? '') + (updateError ?? '')} + </Message>} <Button primary disabled={!isDirty} onClick={() => save(yaml)}><Icon name='save'/>Save</Button> - <Button negative onClick={() => cancelAction()}><Icon name='close'/>Cancel</Button> - </div> - </> + <Button negative onClick={onClose}><Icon name='close'/>Cancel</Button> + </Modal.Actions> + </Modal> ); }; -export default Edit;
\ No newline at end of file +export default EditModal;
\ No newline at end of file diff --git a/src/web/src/components/System/Options/View.js b/src/web/src/components/System/Options/View.js deleted file mode 100644 index 9d4d7635..00000000 --- a/src/web/src/components/System/Options/View.js +++ /dev/null @@ -1,30 +0,0 @@ -import React from 'react'; -import YAML from 'yaml'; - -import { Icon, Button } from 'semantic-ui-react'; -import CodeEditor from '../../Shared/CodeEditor'; - -const View = ({ options, editAction }) => { - const { remoteConfiguration } = options; - - const optionsAsYaml = YAML.stringify(options, { simpleKeys: true, sortMapEntries: true }); - - return ( - <> - <div className='view-code-container'> - <CodeEditor - value={optionsAsYaml} - basicSetup={false} - editable={false} - /> - </div> - <div className='footer-buttons'> - {remoteConfiguration ? - <Button primary onClick={() => editAction()}><Icon name='edit'/>Edit</Button> : - <Button disabled icon='x'><Icon name='lock'/>Remote Configuration Disabled</Button>} - </div> - </> - ); -}; - -export default View;
\ No newline at end of file diff --git a/src/web/src/components/System/Options/index.js b/src/web/src/components/System/Options/index.js index 92535b95..894199e9 100644 --- a/src/web/src/components/System/Options/index.js +++ b/src/web/src/components/System/Options/index.js @@ -1,16 +1,76 @@ import React, { useState } from 'react'; +import YAML from 'yaml'; -import Edit from './Edit'; -import View from './View'; +import DebugModal from './DebugModal'; +import EditModal from './EditModal'; + +import { Divider } from 'semantic-ui-react'; +import { + CodeEditor, + ShrinkableButton, +} from '../../Shared'; const Index = ({ options }) => { - const [mode, setMode] = useState('view'); + const [debugModal, setDebugModal] = useState(false); + const [editModal, setEditModal] = useState(false); + + const { remoteConfiguration, debug } = options; + + const optionsAsYaml = YAML.stringify(options, { simpleKeys: true, sortMapEntries: true }); + + const DebugButton = () => { + if (!debug) return <></>; + + return <ShrinkableButton + icon='bug' + mediaQuery='(max-width: 516px)' + onClick={() => setDebugModal(true)} + > + Debug View + </ShrinkableButton>; + }; + + const EditButton = () => { + if (!remoteConfiguration) { + return <ShrinkableButton + disabled + icon='lock' + mediaQuery='(max-width: 516px)' + >Remote Configuration Disabled</ShrinkableButton>; + } - if (mode === 'edit') { - return <Edit cancelAction={() => setMode('view')}/>; - } + return <ShrinkableButton + primary + icon='edit' + mediaQuery='(max-width: 516px)' + onClick={() => setEditModal(true)} + >Edit</ShrinkableButton>; + }; - return <View options={options} editAction={() => setMode('edit')}/>; + return ( + <> + <div className='header-buttons'> + <DebugButton/> + <EditButton/> + </div> + <Divider/> + <div className='view-code-container'> + <CodeEditor + value={optionsAsYaml} + basicSetup={false} + editable={false} + /> + </div> + <DebugModal + open={debugModal} + onClose={() => setDebugModal(false)} + /> + <EditModal + open={editModal} + onClose={() => setEditModal(false)} + /> + </> + ); }; export default Index;
\ No newline at end of file diff --git a/src/web/src/components/System/System.css b/src/web/src/components/System/System.css index 560cc21a..cbcddf81 100644 --- a/src/web/src/components/System/System.css +++ b/src/web/src/components/System/System.css @@ -24,15 +24,24 @@ min-height: 100px; } -.full-height { - height: calc(100vh - 175px); -} - .no-grow { white-space: nowrap; overflow: overlay; } +.edit-code-header { + margin-top: 1em !important; + font-size: initial !important; +} + +.edit-code-content, .debug-view-content { + padding: 0px !important; +} + +.left-align { + text-align: left; +} + .footer-buttons { margin-top: 14px; text-align: right; @@ -43,23 +52,6 @@ text-align: right; } -.state-code-editor, .view-code-container, .edit-code-container, .edit-code-container-error { - border: 1px solid #d4d4d5; +.edit-code-container, .edit-code-container-error { overflow: auto; -} - -.state-code-container { - height: calc(100vh - 204px); -} - -.view-code-container { - height: calc(100vh - 254px); -} - -.edit-code-container { - height: calc(100vh - 316px); -} - -.edit-code-container-error { - height: calc(100vh - 378px); }
\ No newline at end of file diff --git a/src/web/src/components/System/System.js b/src/web/src/components/System/System.js index 8955b212..73904006 100644 --- a/src/web/src/components/System/System.js +++ b/src/web/src/components/System/System.js @@ -15,15 +15,42 @@ const System = ({ state = {}, options = {} }) => { const history = useHistory(); const panes = [ - { route: 'info', menuItem: 'Info', render: () => <Tab.Pane><Info state={state}/></Tab.Pane> }, + { + route: 'info', + menuItem: { + key: 'info', + icon: 'info', + content: 'Info', + }, + render: () => <Tab.Pane><Info state={state}/></Tab.Pane>, + }, { - route: 'options', menuItem: 'Options', render: () => - <Tab.Pane className='full-height'><Options options={options} /></Tab.Pane>, + route: 'options', + menuItem: { + key: 'options', + icon: 'options', + content: 'Options', + }, + render: () => <Tab.Pane className='full-height'><Options options={options} /></Tab.Pane>, }, { - route: 'shares', menuItem: 'Shares', render: () => <Tab.Pane><Shares/></Tab.Pane>, + route: 'shares', + menuItem: { + key: 'shares', + icon: 'share external', + content: 'Shares', + }, + render: () => <Tab.Pane><Shares/></Tab.Pane>, + }, + { + route: 'logs', + menuItem: { + key: 'logs', + icon: 'file outline', + content: 'Logs', + }, + render: () => <Tab.Pane><Logs/></Tab.Pane>, }, - { route: 'logs', menuItem: 'Logs', render: () => <Tab.Pane><Logs/></Tab.Pane> }, ]; const activeIndex = panes.findIndex(pane => pane.route === tab); |
