Configs are displayed in new ConfigsPage under Admin.

- Configs can be imported and saved.
- Configs are validated before imported.
- API will refuse to save a config if it was not imported successfully.
This commit is contained in:
Roland 2024-09-28 17:28:33 -06:00
parent d34301a085
commit acf3dee179
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
22 changed files with 767 additions and 157 deletions

View file

@ -8,6 +8,7 @@ import {Header, Loader, Segment} from "../Theme";
import {DownloadsPage} from "./Downloads";
import {SettingsPage} from "./Settings";
import {StatusPage} from "./Status";
import {ConfigsPage} from "./Configs";
class WROLMode extends React.Component {
@ -71,6 +72,7 @@ export default function AdminRoute() {
{text: 'Downloads', to: '/admin', key: 'admin', end: true},
{text: 'Settings', to: '/admin/settings', key: 'settings'},
{text: 'Status', to: '/admin/status', key: 'status'},
{text: 'Configs', to: '/admin/configs', key: 'configs'},
{text: 'WROL Mode', to: '/admin/wrol', key: 'wrol'},
];
@ -80,6 +82,7 @@ export default function AdminRoute() {
<Route path='/' exact element={<DownloadsPage/>}/>
<Route path='settings' exact element={<SettingsPage/>}/>
<Route path='status' exact element={<StatusPage/>}/>
<Route path='configs' exact element={<ConfigsPage/>}/>
<Route path='wrol' exact element={<WROLMode/>}/>
</Routes>
</PageContainer>

View file

@ -0,0 +1,148 @@
import {useConfigs} from "../../hooks/customHooks";
import {APIButton, PageContainer} from "../Common";
import {Header, Icon, Popup, Table} from "../Theme";
import React from "react";
import {
Grid,
GridColumn,
GridRow,
TableBody,
TableCell,
TableHeader,
TableHeaderCell,
TableRow
} from "semantic-ui-react";
import {TableRowPlaceholder} from "../Placeholder";
import {useNavigate} from "react-router-dom";
function WarningIcon({ok}) {
return <Icon color={ok ? 'violet' : 'red'} name={ok ? 'check' : 'close'}/>
}
function ConfigTableRow({config, importConfig, saveConfig, fetchConfigs}) {
const {file_name, rel_path, valid, successful_import} = config;
const navigate = useNavigate();
const localImportConfig = async () => {
await importConfig(file_name);
await fetchConfigs();
}
const localSaveConfig = async () => {
await saveConfig(file_name);
await fetchConfigs();
}
const importButton = <APIButton
icon='upload'
disabled={!valid}
onClick={localImportConfig}
/>;
let saveButton;
if (successful_import) {
saveButton = <APIButton
color='grey'
icon='download'
onClick={localSaveConfig}
/>;
} else {
// Warn the user they may overwrite because import was not successful.
saveButton = <APIButton
color='grey'
icon='download'
onClick={localSaveConfig}
confirmButton='Overwrite'
confirmContent='Config was not imported successfully, overwrite the config? Data may be lost!'
/>;
}
return <TableRow>
<TableCell><span onClick={() => navigate(`?preview=${rel_path}`)}>{file_name}</span></TableCell>
<TableCell><WarningIcon ok={successful_import}/></TableCell>
<TableCell><WarningIcon ok={valid}/></TableCell>
<TableCell>{importButton}</TableCell>
<TableCell>{saveButton}</TableCell>
</TableRow>
}
function ConfigsTable({configs, loading, importConfig, saveConfig, fetchConfigs}) {
let body = <TableRow>
<TableCell colSpan={4}><TableRowPlaceholder/></TableCell>
</TableRow>
if (loading) {
// Configs are being fetched again.
} else if (configs === undefined) {
body = <TableRow>
<TableCell colSpan={2}>Failed to fetch configs</TableCell>
</TableRow>;
} else if (configs && Object.keys(configs).length === 0) {
body = <TableRow>
<TableCell colSpan={2}>No configs exist. Try tagging some files.</TableCell>
</TableRow>;
} else if (configs) {
body = Object.entries(configs).map(([key, value]) =>
<ConfigTableRow
key={key}
config={value}
importConfig={importConfig}
saveConfig={saveConfig}
fetchConfigs={fetchConfigs}
/>
);
}
return <Table unstackable striped>
<TableHeader>
<TableRow>
<TableHeaderCell width={10}>File Name</TableHeaderCell>
<TableHeaderCell width={1}>Imported</TableHeaderCell>
<TableHeaderCell width={1}>Valid</TableHeaderCell>
<TableHeaderCell width={1}>Import</TableHeaderCell>
<TableHeaderCell width={1}>Save</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>
{body}
</TableBody>
</Table>
}
export function ConfigsPage() {
const {configs, loading, importConfig, saveConfig, fetchConfigs} = useConfigs();
React.useEffect(() => {
fetchConfigs();
}, []);
const refreshButton = <APIButton
icon='refresh'
onClick={fetchConfigs}
/>;
const refreshPopupButton = <Popup
content='Check if configs are valid'
on='hover'
trigger={refreshButton}
/>;
return <PageContainer>
<Grid columns={2}>
<GridRow>
<GridColumn>
<Header as='h1'>Configs</Header>
</GridColumn>
<GridColumn textAlign='right'>{refreshPopupButton}</GridColumn>
</GridRow>
</Grid>
<ConfigsTable
configs={configs}
loading={loading}
importConfig={importConfig}
saveConfig={saveConfig}
fetchConfigs={fetchConfigs}
/>
</PageContainer>
}

View file

@ -297,8 +297,17 @@ export class SettingsPage extends React.Component {
}
render() {
const controlSegment = <Segment>
<Header as='h3'>Control WROLPi</Header>
<RestartButton/>
<ShutdownButton/>
</Segment>;
if (this.state.ready === false) {
return <Loader active inline='centered'/>
return <>
<Loader active inline='centered'/>
{controlSegment}
</>
} else if (this.state.ready === undefined) {
return <ErrorMessage>Unable to fetch settings</ErrorMessage>
}
@ -539,11 +548,7 @@ export class SettingsPage extends React.Component {
</Form>
</Segment>
<Segment>
<Header as='h3'>Control WROLPi</Header>
<RestartButton/>
<ShutdownButton/>
</Segment>
{controlSegment}
</Container>;
}
}</SettingsContext.Consumer>