From 619a88c429a137e5e8d3dd3195f1d25c663615b1 Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 7 Sep 2023 20:13:36 -0600 Subject: [PATCH] Reworking default/empty/error states for all pages. Null is pending, undefined is error. --- app/src/Tags.js | 12 +- app/src/components/Apps.js | 6 +- app/src/components/Archive.js | 33 ++-- app/src/components/Channels.js | 20 ++- app/src/components/Common.js | 22 ++- app/src/components/FileBrowser.js | 6 +- app/src/components/Files.js | 33 ++-- app/src/components/Map.js | 33 ++-- app/src/components/Videos.js | 17 ++- app/src/components/Zim.js | 30 ++-- app/src/components/admin/Downloads.js | 40 ++--- app/src/components/admin/Settings.js | 212 +++++++++++++------------- app/src/components/admin/Status.js | 58 ++++--- app/src/hooks/customHooks.js | 30 ++-- wrolpi/status.py | 2 +- 15 files changed, 294 insertions(+), 260 deletions(-) diff --git a/app/src/Tags.js b/app/src/Tags.js index 791277bf..7938d57e 100644 --- a/app/src/Tags.js +++ b/app/src/Tags.js @@ -13,7 +13,7 @@ import { TableHeaderCell, TableRow, } from "semantic-ui-react"; -import {APIButton, contrastingColor, scrollToTopOfElement} from "./components/Common"; +import {APIButton, contrastingColor, ErrorMessage, scrollToTopOfElement} from "./components/Common"; import { Button, FormInput, @@ -54,8 +54,8 @@ export function useTags() { setTags(t); setTagNames(t.map(i => i['name'])); } catch (e) { - setTags([]); - setTagNames([]); + setTags(undefined); + setTagNames(undefined); console.error(e); } } @@ -284,7 +284,7 @@ function EditTagsModal() { - @@ -420,8 +420,10 @@ export const TagsDashboard = () => { const {tagNames, TagsLinkGroup} = React.useContext(TagsContext); let availableTagsGroup = ; - if (tagNames && tagNames.length) { + if (tagNames && tagNames.length >= 1) { availableTagsGroup = ; + } else if (tagNames === undefined) { + availableTagsGroup = Could not fetch tags } return diff --git a/app/src/components/Apps.js b/app/src/components/Apps.js index 058f5b90..f5c60c87 100644 --- a/app/src/components/Apps.js +++ b/app/src/components/Apps.js @@ -2,7 +2,7 @@ import React, {useContext} from 'react'; import {Divider, Input, SegmentGroup, StatisticLabel, StatisticValue, TableCell, TableRow} from "semantic-ui-react"; import {Link, Route, Routes} from "react-router-dom"; import {decryptOTP, encryptOTP} from "../api"; -import {APIButton, humanFileSize, mimetypeColor, PageContainer, toLocaleString, useTitle} from "./Common"; +import {APIButton, ErrorMessage, humanFileSize, mimetypeColor, PageContainer, toLocaleString, useTitle} from "./Common"; import {ThemeContext} from "../contexts/contexts"; import {Button, Header, Loader, Segment, Statistic, StatisticGroup, Table, TextArea} from "./Theme"; import {useStatistics, useVINDecoder} from "../hooks/customHooks"; @@ -208,8 +208,8 @@ function StatisticsPage() { if (statistics === undefined) { return <>
Statistics
-

Failed to fetch statistics

- ; + Failed to fetch statistics + } if (statistics['global_statistics']) { diff --git a/app/src/components/Archive.js b/app/src/components/Archive.js index 51f574fa..40b1d36a 100644 --- a/app/src/components/Archive.js +++ b/app/src/components/Archive.js @@ -18,7 +18,7 @@ import { BackButton, CardLink, cardTitleWrapper, - encodeMediaPath, + encodeMediaPath, ErrorMessage, ExternalCardLink, FileIcon, findPosterPath, @@ -27,7 +27,6 @@ import { isoDatetimeToString, mimetypeColor, PageContainer, - PreviewLink, PreviewPath, SearchInput, SortButton, @@ -301,7 +300,8 @@ export function DomainsPage() { const [domains] = useDomains(); const [searchStr, setSearchStr] = useState(''); - if (!domains) { + if (domains === null) { + // Request is pending. return <> @@ -310,7 +310,9 @@ export function DomainsPage() { ; - } else if (_.isEmpty(domains)) { + } else if (domains === undefined) { + return Could not fetch domains + } else if (domains && domains.length === 0) { return No domains yet. Archive some webpages! @@ -366,25 +368,25 @@ export function SearchDomain() { setDomain(value); } + let domainOptions = []; + if (domains && domains.length > 0) { - const domainOptions = domains.map(i => { + domainOptions = domains.map(i => { return {key: i['domain'], value: i['domain'], text: i['domain']} }); - return <> - - } - return <> + return <> + + } function ArchivesPage() { const [selectedArchives, setSelectedArchives] = useState([]); - const [deleteOpen, setDeleteOpen] = useState(false); useTitle('Archives'); @@ -416,7 +418,6 @@ function ArchivesPage() { } const onDelete = async () => { - setDeleteOpen(false); const archiveIds = archives.filter(i => selectedArchives.indexOf(i['primary_path']) >= 0) .map(i => i['data']['id']); await deleteArchives(archiveIds); diff --git a/app/src/components/Channels.js b/app/src/components/Channels.js index 42e026f5..c11080a5 100644 --- a/app/src/components/Channels.js +++ b/app/src/components/Channels.js @@ -17,6 +17,7 @@ import { APIButton, BackButton, DirectoryInput, + ErrorMessage, frequencyOptions, humanFileSize, RequiredAsterisk, @@ -506,7 +507,7 @@ function MobileChannelRow({channel}) { } -export function Channels() { +export function ChannelsPages() { useTitle('Channels'); const {channels} = useChannels(); @@ -552,7 +553,15 @@ export function Channels() { {key: 'manage', text: 'Manage'}, ]; - if (channels === null) { + if (channels && channels.length === 0) { + return <> + {header} + + No channels exist yet! + Create one. + + + } else if (channels === null) { // Placeholders while fetching return <> {header} @@ -569,13 +578,10 @@ export function Channels() { - } else if (_.isEmpty(channels)) { + } else if (channels === undefined) { return <> {header} - - No channels exist yet! - Create one. - + Could not fetch Channels } diff --git a/app/src/components/Common.js b/app/src/components/Common.js index db0a2a91..581a6991 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -1404,11 +1404,27 @@ export function APIButton({ return button; } -export function InfoMessage({children}) { - return +export function InfoMessage({children, size = null}) { + return {children} -} \ No newline at end of file +} + +export function WarningMessage({children, size = null}) { + return + + + {children} + + +} + +export function ErrorMessage({children, size = null}) { + return + + {children} + +} diff --git a/app/src/components/FileBrowser.js b/app/src/components/FileBrowser.js index 39f3f6b7..d8651317 100644 --- a/app/src/components/FileBrowser.js +++ b/app/src/components/FileBrowser.js @@ -11,7 +11,7 @@ import { TableHeaderCell, TableRow } from "semantic-ui-react"; -import {APIButton, DirectorySearch, FileIcon, humanFileSize} from "./Common"; +import {APIButton, DirectorySearch, ErrorMessage, FileIcon, humanFileSize} from "./Common"; import React, {useEffect, useState} from "react"; import {deleteFile, makeDirectory, movePaths, renamePath} from "../api"; import _ from 'lodash'; @@ -129,7 +129,6 @@ function Path({path, onFolderClick, onFileClick, sortData, selectedPaths, onSele export function FileBrowser() { const [selectedPaths, setSelectedPaths] = React.useState([]); - const [deleteOpen, setDeleteOpen] = React.useState(false); const [renameOpen, setRenameOpen] = React.useState(false); const [moveOpen, setMoveOpen] = React.useState(false); const [makeDirectoryOpen, setMakeDirectoryOpen] = React.useState(false); @@ -178,7 +177,6 @@ export function FileBrowser() { } const reset = async () => { - setDeleteOpen(false); setSelectedPaths([]); setPending(true); try { @@ -268,6 +266,8 @@ export function FileBrowser() { ; + } else if (browseFiles === undefined) { + return Could not fetch files } return <> diff --git a/app/src/components/Files.js b/app/src/components/Files.js index c90ae710..dbbb52b1 100644 --- a/app/src/components/Files.js +++ b/app/src/components/Files.js @@ -18,6 +18,7 @@ import { CardPoster, cardTitleWrapper, encodeMediaPath, + ErrorMessage, ExternalCardLink, FileIcon, findPosterPath, @@ -37,14 +38,15 @@ import { useQuery, useSearchFiles, useSearchFilter, - useSearchView, useStatusFlag, + useSearchView, + useStatusFlag, useWROLMode } from "../hooks/customHooks"; import {Route, Routes} from "react-router-dom"; import {CardPlaceholder} from "./Placeholder"; import {ArchiveCard, ArchiveRowCells} from "./Archive"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; -import {Media, StatusContext, ThemeContext} from "../contexts/contexts"; +import {Media, ThemeContext} from "../contexts/contexts"; import { Button, Card, @@ -60,7 +62,6 @@ import { } from "./Theme"; import {SelectableTable} from "./Tables"; import {VideoCard, VideoRowCells} from "./Videos"; -import _ from 'lodash'; import {FileBrowser} from "./FileBrowser"; import {refreshFiles} from "../api"; import {useSubscribeEventName} from "../Events"; @@ -161,15 +162,17 @@ function FileCard({file}) { } export function FileCards({files}) { - if (!_.isEmpty(files)) { + if (files && files.length >= 1) { return {files.map(i => )} } else if (files && files.length === 0) { return No results! - } else { - return + } else if (files === undefined) { + return Could not search! } + // Response is pending. + return } function ImageRowCells({file}) { @@ -246,13 +249,7 @@ function FileRow({file}) { } export function FileTable({files, selectOn, onSelect, footer, selectedKeys}) { - if (!files) { - return - - - - - } else if (files && files.length > 0) { + if (files && files.length > 0) { const headerContents = ['Poster', 'Title']; const rows = files.map(i => ); return ; - } else { + } else if (files) { return No results! + } else if (files === undefined) { + return Could not search! } + + return + + + + } export function FileRowTagIcon({file}) { diff --git a/app/src/components/Map.js b/app/src/components/Map.js index d1e7dfc2..8d74f740 100644 --- a/app/src/components/Map.js +++ b/app/src/components/Map.js @@ -1,6 +1,7 @@ import React, {useContext} from "react"; import { APIButton, + ErrorMessage, HelpPopup, humanFileSize, InfoMessage, @@ -136,6 +137,7 @@ class ManageMap extends React.Component { }); } catch (e) { console.error(e); + this.setState({files: undefined}); } } @@ -216,24 +218,27 @@ class ManageMap extends React.Component { {!_.isEmpty(selectedPaths) ? 'Import Selected' : 'Import All'} ; - let rows; - if (!files) { - // Fetch request is not complete. - rows = - - - - - - - - ; - } else if (_.isEmpty(files)) { + let rows = + + + + + + + + ; + if (files && files.length === 0) { rows = No PBF map files were found in map/pbf ; - } else { + } else if (files && files.length >= 1) { rows = files.map(i => this.tableRow(i, disabled)); + } else if (files === undefined) { + rows = + + Could not fetch map files + + } let spaceHelpPopup = } -function Statistics() { +function VideosStatistics() { useTitle('Video Statistics'); const {statistics} = useVideoStatistics(); - const {videos, historical, channels} = statistics; - if (!videos) { + if (statistics === null) { + // Request is pending. return + } else if (statistics === undefined) { + return Unable to fetch Video Statistics } + const {videos, historical, channels} = statistics; + const videoNames = [ {key: 'videos', label: 'Videos'}, {key: 'sum_size', label: 'Total Size'}, @@ -270,8 +275,8 @@ export function VideosRoute(props) { }/> - }/> - }/> + }/> + }/> }/> }/> }/> diff --git a/app/src/components/Zim.js b/app/src/components/Zim.js index 12f4be7c..d2e5f1c5 100644 --- a/app/src/components/Zim.js +++ b/app/src/components/Zim.js @@ -31,6 +31,7 @@ import React, {useContext, useState} from "react"; import { APIButton, encodeMediaPath, + ErrorMessage, humanFileSize, InfoMessage, normalizeEstimate, @@ -38,7 +39,8 @@ import { Paginator, TabLinks, TagIcon, - useTitle + useTitle, + WarningMessage } from "./Common"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; import {TagsSelector} from "../Tags"; @@ -400,10 +402,10 @@ class ManageZim extends React.Component { constructor(props) { super(props); this.state = { - catalog: undefined, - iso_639_codes: undefined, - subscriptions: undefined, - zims: undefined, + catalog: null, + iso_639_codes: null, + subscriptions: null, + zims: null, } } @@ -418,6 +420,7 @@ class ManageZim extends React.Component { this.setState({zims}); } catch (e) { console.error(e); + this.setState({zims: undefined}); // Display error. } } @@ -427,6 +430,7 @@ class ManageZim extends React.Component { this.setState({subscriptions, catalog, iso_639_codes}); } catch (e) { console.error(e); + this.setState({subscriptions: undefined, catalog: undefined}); // Display error. } } @@ -452,19 +456,19 @@ class ManageZim extends React.Component { ; - if (zims && zims.length > 0) { + if (zims && zims.length >= 1) { zimFilesBody = ; - } else if (zims === null || (zims && zims.length === 0)) { - zimFilesBody = - - You have not subscribed to any Kiwix projects, or your files have not been refreshed. - - ; + } else if (zims && zims.length === 0) { + zimFilesBody = + You have not subscribed to any Kiwix projects, or your files have not been refreshed. + ; + } else if (zims === undefined) { + zimFilesBody = Could not fetch Zim files; } const kiwixCatalogHeaders = [ @@ -496,6 +500,8 @@ class ManageZim extends React.Component { rowKey='name' tableHeaders={kiwixCatalogHeaders} /> + } else if (catalog === undefined) { + kiwixCatalog = Could not fetch catalog; } return diff --git a/app/src/components/admin/Downloads.js b/app/src/components/admin/Downloads.js index c682d03f..4d1f67e0 100644 --- a/app/src/components/admin/Downloads.js +++ b/app/src/components/admin/Downloads.js @@ -4,6 +4,7 @@ import {Link} from "react-router-dom"; import { APIButton, DisableDownloadsToggle, + ErrorMessage, isoDatetimeToElapsedPopup, secondsToFrequency, textEllipsis, @@ -23,7 +24,6 @@ import { } from "semantic-ui-react"; import {Button, Header, Modal, ModalActions, ModalContent, ModalHeader, Placeholder, Segment, Table} from "../Theme"; import {useDownloads} from "../../hooks/customHooks"; -import _ from "lodash"; function ClearCompleteDownloads({callback}) { async function localClearDownloads() { @@ -245,9 +245,7 @@ class StoppableRow extends React.Component { } export function OnceDownloadsTable({downloads, fetchDownloads}) { - if (downloads && _.isEmpty(downloads)) { - return No downloads are scheduled.; - } else if (downloads) { + if (downloads && downloads.length >= 1) { return <> @@ -269,19 +267,20 @@ export function OnceDownloadsTable({downloads, fetchDownloads}) {
- ; - } else { - return - - - ; + + } else if (downloads) { + return No downloads are scheduled. + } else if (downloads === undefined) { + return Unable to fetch downloads } + return + + + } export function RecurringDownloadsTable({downloads, fetchDownloads}) { - if (downloads && _.isEmpty(downloads)) { - return No downloads are scheduled.; - } else if (downloads) { + if (downloads && downloads.length >= 1) { return @@ -295,13 +294,16 @@ export function RecurringDownloadsTable({downloads, fetchDownloads}) { {downloads.map(i => )} -
; - } else { - return - - - ; + + } else if (downloads) { + return No downloads are scheduled. + } else if (downloads === undefined) { + return Unable to fetch downloads } + return + + + } export function DownloadsPage() { diff --git a/app/src/components/admin/Settings.js b/app/src/components/admin/Settings.js index 3192fb4e..2a79bb11 100644 --- a/app/src/components/admin/Settings.js +++ b/app/src/components/admin/Settings.js @@ -2,8 +2,7 @@ import React from "react"; import {getSettings, postRestart, postShutdown, saveSettings} from "../../api"; import {Button, Form, FormGroup, FormInput, Header, Loader, Modal, ModalContent, ModalHeader, Segment} from "../Theme"; import {Container, Dimmer, Icon} from "semantic-ui-react"; -import {ThemeContext} from "../../contexts/contexts"; -import {APIButton, HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common"; +import {APIButton, ErrorMessage, HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common"; import QRCode from "react-qr-code"; import {useDockerized} from "../../hooks/customHooks"; import {toast} from "react-semantic-toasts-2"; @@ -155,6 +154,7 @@ export class SettingsPage extends React.Component { }, this.handleHotspotChange); } catch (e) { console.error(e); + this.setState({ready: undefined}); } } @@ -207,6 +207,8 @@ export class SettingsPage extends React.Component { render() { if (this.state.ready === false) { return + } else if (this.state.ready === undefined) { + return Unable to fetch settings } let { @@ -224,124 +226,120 @@ export class SettingsPage extends React.Component { const qrButton = ; - return - {({i, t}) => - + return + - - - - + + + + - -
- Settings -
-

- Any changes will be written to config/wrolpi.yaml. -

+ +
+ Settings +
+

Any changes will be written to config/wrolpi.yaml.

-
-
- this.handleInputChange(null, 'download_on_startup', checked)} - /> -
+ +
+ this.handleInputChange(null, 'download_on_startup', checked)} + /> +
-
- this.handleInputChange(null, 'throttle_on_startup', checked)} - /> -
+
+ this.handleInputChange(null, 'throttle_on_startup', checked)} + /> +
-
- this.handleInputChange(null, 'ignore_outdated_zims', checked)} - /> -
+
+ this.handleInputChange(null, 'ignore_outdated_zims', checked)} + /> +
- - - Download Timeout - - } - value={download_timeout} - disabled={disabled || download_timeout === null} - onChange={(e, d) => this.handleTimeoutChange(e, 'download_timeout', d.value)} - /> - + } + value={download_timeout} + disabled={disabled || download_timeout === null} + onChange={(e, d) => this.handleTimeoutChange(e, 'download_timeout', d.value)} + /> + - - this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)} - /> - this.setState({hotspot_password: d.value}, this.handleHotspotChange)} - /> - this.handleInputChange(e, 'hotspot_device', d.value)} - /> - + + this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)} + /> + this.setState({hotspot_password: d.value}, this.handleHotspotChange)} + /> + this.handleInputChange(e, 'hotspot_device', d.value)} + /> + - this.setState({qrOpen: false})} - onOpen={this.handleQrOpen} - open={this.state.qrOpen} - trigger={qrButton} - > - - Scan this code to join the hotspot - - -
- -
-
-
+ this.setState({qrOpen: false})} + onOpen={this.handleQrOpen} + open={this.state.qrOpen} + trigger={qrButton} + > + + Scan this code to join the hotspot + + +
+ +
+
+
-
+
- Save + Save - - - + + + -
-
+ +
- -
Control WROLPi
- - -
-
} -
+ +
Control WROLPi
+ + +
+ } } diff --git a/app/src/components/admin/Status.js b/app/src/components/admin/Status.js index 160e08a4..6e1d8c97 100644 --- a/app/src/components/admin/Status.js +++ b/app/src/components/admin/Status.js @@ -196,6 +196,13 @@ export function StatusPage() { memoryPercent = Math.round(memory_stats['used'] / memory_stats['total'] * 100); } + const SizedHeader = ({children}) => { + return
+
{children}
+
{children}
+
+ } + return <> @@ -212,22 +219,6 @@ export function StatusPage() { - - -
Drive Bandwidth
- {disk_bandwidth.map((disk) => )} -
- - -
Network Bandwidth
- {bandwidth ? bandwidth.map(i => ) : - } -
- - -
Drive Usage
- {drives.map((drive) => )} -
@@ -245,22 +236,25 @@ export function StatusPage() { - - -
Drive Bandwidth
- {disk_bandwidth.map(disk => )} -
- - -
Network Bandwidth
- {bandwidth ? bandwidth.map(i => ) : - } -
- - -
Drive Usage
- {drives.map(drive => )} -
+ + + Drive Bandwidth + {disk_bandwidth && disk_bandwidth.length > 0 ? disk_bandwidth.map((disk) => ) + : } + + + + Network Bandwidth + {bandwidth ? bandwidth.map(i => ) + : } + + + + Drive Usage + {drives && drives.length > 0 ? drives.map((drive) => ) + : } + } diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index 77593f2f..4d3b3531 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -121,7 +121,7 @@ export const useDomains = () => { setDomains(domains); setTotal(total); } catch (e) { - setDomains([]); + setDomains(undefined); // Display error. setTotal(0); } } @@ -214,7 +214,7 @@ export const useSearchArchives = (defaultLimit) => { description: 'Could not get archives', time: 5000, }); - setArchives([]); + setArchives(undefined); // Could not get Archives, display error. } } @@ -256,7 +256,7 @@ export const useSearchVideos = (defaultLimit, channelId, order_by) => { const {view} = useSearchView(); const headline = view === 'headline'; - const [videos, setVideos] = useState(); + const [videos, setVideos] = useState(null); const [totalPages, setTotalPages] = useState(0); const localSearchVideos = async () => { @@ -268,13 +268,7 @@ export const useSearchVideos = (defaultLimit, channelId, order_by) => { setTotalPages(calculateTotalPages(total, limit)); } catch (e) { console.error(e); - toast({ - type: 'error', - title: 'Unexpected server response', - description: 'Could not get videos', - time: 5000, - }); - setVideos([]); + setVideos(undefined);// Could not get Videos, display error. } } @@ -405,12 +399,7 @@ export const useChannels = () => { setChannels(c); } catch (e) { console.error(e); - toast({ - type: 'error', - title: 'Unexpected server response', - description: 'Could not get channels', - time: 5000, - }); + setChannels(undefined); // Could not get Channels, display error. } } @@ -482,7 +471,7 @@ export const useBrowseFiles = () => { setBrowseFiles(files); } catch (e) { console.log(e); - setBrowseFiles([]); + setBrowseFiles(undefined); // Display error. } } @@ -561,6 +550,10 @@ export const useDownloads = () => { setPendingOnceDownloads(data['pending_once_downloads']); } catch (e) { console.error(e); + // Display errors. + setOnceDownloads(undefined); + setRecurringDownloads(undefined); + setPendingOnceDownloads(undefined); } } @@ -753,7 +746,7 @@ export const useCPUTemperature = () => { export const useVideoStatistics = () => { - const [statistics, setStatistics] = useState({}); + const [statistics, setStatistics] = useState(null); const fetchStatistics = async () => { try { @@ -765,6 +758,7 @@ export const useVideoStatistics = () => { setStatistics(stats); } catch (e) { console.error(e); + setStatistics(undefined); // Display error. } } diff --git a/wrolpi/status.py b/wrolpi/status.py index d22a372f..7f428a54 100755 --- a/wrolpi/status.py +++ b/wrolpi/status.py @@ -412,7 +412,7 @@ async def get_bandwidth_info() -> Tuple[List[NICBandwidthInfo], List[DiskBandwid try: maximum_read_ps = max(disk['bytes_read_ps'], MAX_DISKS_BANDWIDTH[name]['maximum_read_ps']) maximum_write_ps = max(disk['bytes_write_ps'], MAX_DISKS_BANDWIDTH[name]['maximum_write_ps']) - except KeyError as e: + except KeyError: # Use a low first value. Hopefully all drives are capable of this speed. maximum_read_ps = 500_000 maximum_write_ps = 500_000