Displaying plug icon when API fails to respond. Using window.apiDown to communicate to other hooks to stop fetching, the status interval will keep checking if the API is back up.

This commit is contained in:
Roland 2025-01-24 15:35:26 -07:00
parent e7415873a5
commit 01338e4d89
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
7 changed files with 76 additions and 21 deletions

View file

@ -1,5 +1,6 @@
import React, {useContext, useEffect, useRef, useState} from "react";
import {
ApiDownError,
createChannel,
fetchDecoded,
fetchDomains,
@ -804,6 +805,9 @@ export const useSettings = () => {
const [pending, setPending] = useState(false);
const fetchSettings = async () => {
if (window.apiDown) {
return;
}
setPending(true);
try {
setSettings(await getSettings())
@ -840,9 +844,15 @@ export const useStatus = () => {
const fetchStatus = async () => {
try {
const s = await getStatus();
setStatus(s);
setStatus(await getStatus());
window.apiDown = false;
} catch (e) {
if (e instanceof ApiDownError) {
// API is down, do not log this error.
window.apiDown = true;
setStatus({});
return;
}
// Ignore SyntaxError because they happen when the API is down.
if (!(e instanceof SyntaxError)) {
console.error(e);
@ -850,6 +860,11 @@ export const useStatus = () => {
}
}
useEffect(() => {
// Used to tell other hooks to stop fetching intervals.
window.apiDown = false;
}, []);
return {status, fetchStatus}
}