Preserving loading state of Refresh button by creating global event subscription functions.

This commit is contained in:
Roland 2023-01-04 11:16:40 -07:00
parent ad63dd932b
commit 5a0f961cd1
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 47 additions and 18 deletions

View file

@ -3,6 +3,7 @@ import {toast} from "react-semantic-toasts";
import {useRecurringTimeout} from "./hooks/customHooks";
import {getEvents} from "./api";
const apiEventName = 'apiEvent';
function eventToast(title, description, type = 'success', time = 5000) {
toast({type: type, title: title, description: description, time: time});
@ -13,7 +14,7 @@ function handleEvents(events) {
events.forEach(e => {
const {event, message, subject, dt} = e;
console.debug('event', e);
document.dispatchEvent(new CustomEvent(apiEventName, {detail: e}));
if (subject && newestEvents[subject] && newestEvents[subject] > dt) {
console.debug(`Already handled newer event of "${subject}".`);
@ -65,3 +66,24 @@ export function useEventsInterval() {
return {events, fetchEvents}
}
export function useSubscribeEvents(listener) {
// Subscribe to all API sent events.
document.addEventListener(apiEventName, listener);
const unsubscribe = () => document.removeEventListener(apiEventName, listener);
useEffect(() => {
return unsubscribe;
}, []);
}
export function useSubscribeEventName(name, listener) {
// Subscribe to only API sent events that match `name`.
const localListener = (event) => {
if (event && event.detail['event'] === name) {
listener(event);
}
}
useSubscribeEvents(localListener);
}

View file

@ -394,14 +394,14 @@ function Archives() {
/>
<Button
color='grey'
onClick={() => invertSelection()}
onClick={invertSelection}
disabled={_.isEmpty(archives)}
>
Invert
</Button>
<Button
color='yellow'
onClick={() => clearSelection()}
onClick={clearSelection}
disabled={_.isEmpty(archives) || _.isEmpty(selectedArchives)}
>
Clear

View file

@ -39,11 +39,12 @@ import {Route, Routes} from "react-router-dom";
import {CardPlacholder} from "./Placeholder";
import {ArchiveCard, ArchiveRowCells} from "./Archive";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import {ThemeContext} from "../contexts/contexts";
import {Button, Card, CardIcon, Header, Icon, Placeholder, Segment} from "./Theme";
import {StatusContext, ThemeContext} from "../contexts/contexts";
import {Button, Card, CardIcon, Icon, Placeholder, Segment} from "./Theme";
import {SelectableTable} from "./Tables";
import {VideoCard, VideoRowCells} from "./Videos";
import _ from 'lodash';
import {useSubscribeEvents, useSubscribeEventName} from "../Events";
const icons = {
File: <Icon name='file'/>,
@ -561,24 +562,27 @@ export function FilesSearchView({
function FilesRefreshButton() {
const {t} = useContext(ThemeContext);
const {status} = useContext(StatusContext);
const refreshing = status && status['flags'] && status['flags'].indexOf('refreshing') >= 0;
const [loading, setLoading] = React.useState(false);
const [name, setName] = React.useState('refresh');
const handleClick = async () => {
setLoading(true);
await refreshFiles();
setLoading(false);
setName('check');
}
// Clear loading when global refresh event completes.
useSubscribeEventName('global_refresh_completed', () => setLoading(false));
return <>
<Button icon
labelPosition='left'
loading={loading}
loading={loading || refreshing}
id='refresh_files'
onClick={handleClick}>
<Icon name={name}/>
onClick={handleClick}
disabled={loading || refreshing}>
<Icon name='refresh'/>
Refresh Files
</Button>
<label htmlFor='refresh_files' {...t}>
@ -589,24 +593,27 @@ function FilesRefreshButton() {
function DirectoryFilesRefreshButton({directory}) {
const {t} = useContext(ThemeContext);
const {status} = useContext(StatusContext);
const refreshing = status && status['flags'] && status['flags'].indexOf('refreshing') >= 0;
const [loading, setLoading] = React.useState(false);
const [name, setName] = React.useState('refresh');
const handleClick = async () => {
setLoading(true);
await refreshDirectoryFiles(directory);
setLoading(false);
setName('check');
}
// Clear loading when directory refresh event completes.
useSubscribeEventName('directory_refresh_completed', () => setLoading(false));
return <>
<Button icon primary
labelPosition='left'
loading={loading}
loading={loading || refreshing}
id='refresh_files'
onClick={handleClick}>
<Icon name={name}/>
onClick={handleClick}
disabled={loading || refreshing}>
<Icon name='refresh'/>
Refresh Directory
</Button>
<label htmlFor='refresh_files' {...t}>

View file

@ -126,7 +126,7 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) {
let captionsPane = {
menuItem: 'Captions', render: () => <TabPane>
<pre>{video.caption || 'No captions available'}</pre>
<pre>{video.caption || 'No captions available.'}</pre>
</TabPane>
};