2022-12-23 16:04:09 -07:00
|
|
|
import React, {useEffect} from 'react';
|
|
|
|
|
import {toast} from "react-semantic-toasts";
|
|
|
|
|
import {useRecurringTimeout} from "./hooks/customHooks";
|
|
|
|
|
import {getEvents} from "./api";
|
|
|
|
|
|
2023-01-04 11:16:40 -07:00
|
|
|
const apiEventName = 'apiEvent';
|
2022-12-23 16:04:09 -07:00
|
|
|
|
|
|
|
|
function eventToast(title, description, type = 'success', time = 5000) {
|
|
|
|
|
toast({type: type, title: title, description: description, time: time});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleEvents(events) {
|
2022-12-23 16:30:06 -07:00
|
|
|
const newestEvents = {};
|
2022-12-23 16:04:09 -07:00
|
|
|
|
|
|
|
|
events.forEach(e => {
|
2022-12-23 16:30:06 -07:00
|
|
|
const {event, message, subject, dt} = e;
|
2023-01-04 11:16:40 -07:00
|
|
|
document.dispatchEvent(new CustomEvent(apiEventName, {detail: e}));
|
2022-12-23 16:04:09 -07:00
|
|
|
|
2022-12-23 16:30:06 -07:00
|
|
|
if (subject && newestEvents[subject] && newestEvents[subject] > dt) {
|
|
|
|
|
console.debug(`Already handled newer event of "${subject}".`);
|
2022-12-23 16:04:09 -07:00
|
|
|
return;
|
|
|
|
|
} else if (event === 'global_refresh_completed') {
|
|
|
|
|
eventToast('Refresh completed', 'All files have been refreshed.');
|
2022-12-23 16:23:27 -07:00
|
|
|
} else if (event === 'directory_refresh_started') {
|
2022-12-23 16:04:09 -07:00
|
|
|
const description = message || 'Refresh of directory has started.';
|
|
|
|
|
eventToast('Refresh started', description);
|
2022-12-23 16:23:27 -07:00
|
|
|
} else if (event === 'directory_refresh_completed') {
|
2022-12-23 16:04:09 -07:00
|
|
|
const description = message || 'Refresh of directory has completed.';
|
|
|
|
|
eventToast('Refresh completed', description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (subject) {
|
2022-12-23 16:30:06 -07:00
|
|
|
newestEvents[subject] = dt;
|
2022-12-23 16:04:09 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useEventsInterval() {
|
|
|
|
|
const [events, setEvents] = React.useState(null);
|
|
|
|
|
|
|
|
|
|
// `now` is the last time we successfully fetched events.
|
|
|
|
|
const now = React.useRef(localStorage.getItem('events_now'));
|
|
|
|
|
const setNow = (newNow) => {
|
|
|
|
|
now.current = newNow;
|
|
|
|
|
localStorage.setItem('events_now', now.current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchEvents = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const response = await getEvents(now.current);
|
|
|
|
|
setNow(response['now']);
|
|
|
|
|
setEvents(response['events']);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
setEvents(null);
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useRecurringTimeout(fetchEvents, 1000 * 5);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-23 16:34:23 -07:00
|
|
|
if (events && events.length >= 1) {
|
2022-12-23 16:04:09 -07:00
|
|
|
handleEvents(events);
|
|
|
|
|
}
|
|
|
|
|
}, [JSON.stringify(events)]);
|
|
|
|
|
|
|
|
|
|
return {events, fetchEvents}
|
|
|
|
|
}
|
2023-01-04 11:16:40 -07:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|