diff --git a/app/src/Events.js b/app/src/Events.js index 132c6d8f..b3066fc1 100644 --- a/app/src/Events.js +++ b/app/src/Events.js @@ -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); +} diff --git a/app/src/components/Archive.js b/app/src/components/Archive.js index 8a5071d5..5009417a 100644 --- a/app/src/components/Archive.js +++ b/app/src/components/Archive.js @@ -394,14 +394,14 @@ function Archives() { /> invertSelection()} + onClick={invertSelection} disabled={_.isEmpty(archives)} > Invert clearSelection()} + onClick={clearSelection} disabled={_.isEmpty(archives) || _.isEmpty(selectedArchives)} > Clear diff --git a/app/src/components/Files.js b/app/src/components/Files.js index d36a9491..9a2b0b03 100644 --- a/app/src/components/Files.js +++ b/app/src/components/Files.js @@ -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: , @@ -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 <> - + onClick={handleClick} + disabled={loading || refreshing}> + Refresh Files @@ -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 <> - + onClick={handleClick} + disabled={loading || refreshing}> + Refresh Directory diff --git a/app/src/components/VideoPlayer.js b/app/src/components/VideoPlayer.js index 2c38e710..b8229afd 100644 --- a/app/src/components/VideoPlayer.js +++ b/app/src/components/VideoPlayer.js @@ -126,7 +126,7 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { let captionsPane = { menuItem: 'Captions', render: () => - {video.caption || 'No captions available'} + {video.caption || 'No captions available.'} };
{video.caption || 'No captions available'}
{video.caption || 'No captions available.'}