mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-22 07:56:18 -05:00
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
|
|
import React, {useEffect} from 'react';
|
||
|
|
import {toast} from "react-semantic-toasts";
|
||
|
|
import {useRecurringTimeout} from "./hooks/customHooks";
|
||
|
|
import {getEvents} from "./api";
|
||
|
|
|
||
|
|
|
||
|
|
function eventToast(title, description, type = 'success', time = 5000) {
|
||
|
|
toast({type: type, title: title, description: description, time: time});
|
||
|
|
}
|
||
|
|
|
||
|
|
function handleEvents(events) {
|
||
|
|
let processedSubjects = [];
|
||
|
|
|
||
|
|
events.forEach(e => {
|
||
|
|
const {event, message, subject} = e;
|
||
|
|
console.debug('event', e);
|
||
|
|
|
||
|
|
if (subject && processedSubjects.indexOf(subject) >= 0) {
|
||
|
|
console.debug(`Already handled subject "${subject}" this batch.`);
|
||
|
|
return;
|
||
|
|
} else if (event === 'global_refresh_completed') {
|
||
|
|
eventToast('Refresh completed', 'All files have been refreshed.');
|
||
|
|
} else if (event === 'global_directory_refresh_started') {
|
||
|
|
const description = message || 'Refresh of directory has started.';
|
||
|
|
eventToast('Refresh started', description);
|
||
|
|
} else if (event === 'global_directory_refresh_completed') {
|
||
|
|
const description = message || 'Refresh of directory has completed.';
|
||
|
|
eventToast('Refresh completed', description);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (subject) {
|
||
|
|
processedSubjects = [...processedSubjects, subject];
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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(() => {
|
||
|
|
if (events && events.length > 1) {
|
||
|
|
handleEvents(events);
|
||
|
|
}
|
||
|
|
}, [JSON.stringify(events)]);
|
||
|
|
|
||
|
|
return {events, fetchEvents}
|
||
|
|
}
|