mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 09:48:44 +00:00
server notifications working
This commit is contained in:
1 parent
6a0886ca9b
commit
be2efc8b44
2 files changed
+38
-36
No files matched your search
@@ -9,7 +9,8 @@ export interface AlertObject {
|
||||
|
||||
export interface StatusAlertCheckResponse {
|
||||
success?: boolean,
|
||||
maybe_alert?: AlertObject
|
||||
maybe_alert?: AlertObject,
|
||||
refresh_interval_millis?: number
|
||||
}
|
||||
|
||||
export const StatusAlertCheck = MakeRequest<string, StatusAlertCheckRequest, StatusAlertCheckResponse,{}>({
|
||||
|
||||
@@ -1,43 +1,44 @@
|
||||
import {
|
||||
// useEffect,
|
||||
useState
|
||||
} from "react";
|
||||
import {
|
||||
// StatusAlertCheck,
|
||||
StatusAlertCheckResponse
|
||||
} from "@storyteller/components/src/api/server/StatusAlertCheck";
|
||||
// import { useInterval, useNotifications } from "hooks";
|
||||
import { useEffect, useState } from "react";
|
||||
import { StatusAlertCheck, StatusAlertCheckResponse } from "@storyteller/components/src/api/server/StatusAlertCheck";
|
||||
import { useInterval, useNotifications } from "hooks";
|
||||
|
||||
export default function useStatusPoll() {
|
||||
const [serverStatus,
|
||||
// serverStatusSet
|
||||
] = useState<StatusAlertCheckResponse>({});
|
||||
// const [initialized,initializedSet] = useState(false);
|
||||
// const [alerted,alertedSet] = useState(false);
|
||||
// const notifications = useNotifications();
|
||||
const [serverStatus, serverStatusSet] = useState<StatusAlertCheckResponse>({});
|
||||
const [initialized,initializedSet] = useState(false);
|
||||
const [downAlerted,downAlertedSet] = useState(false);
|
||||
const [upAlerted,upAlertedSet] = useState(false);
|
||||
const notifications = useNotifications();
|
||||
const defaultInt = 60000;
|
||||
const interval = Math.max(defaultInt, serverStatus.refresh_interval_millis || defaultInt);
|
||||
|
||||
// const onTick = () => {
|
||||
// StatusAlertCheck("",{})
|
||||
// .then((res: StatusAlertCheckResponse) => {
|
||||
// serverStatusSet(res);
|
||||
// });
|
||||
// };
|
||||
const onTick = () => {
|
||||
StatusAlertCheck("",{})
|
||||
.then((res: StatusAlertCheckResponse) => {
|
||||
serverStatusSet(res);
|
||||
});
|
||||
};
|
||||
|
||||
// useEffect(() => {
|
||||
// if (!initialized) {
|
||||
// initializedSet(true);
|
||||
// onTick();
|
||||
// }
|
||||
// if (serverStatus.maybe_alert && !alerted) {
|
||||
// alertedSet(true);
|
||||
// notifications.create({ autoRemove: false, title: "Server down for maintenence", });
|
||||
// } else if (serverStatus.maybe_alert && alerted) {
|
||||
// alertedSet(false);
|
||||
// notifications.create({ autoRemove: false, title: "Server is back online", });
|
||||
// }
|
||||
// },[alerted,initialized,notifications,serverStatus]);
|
||||
useEffect(() => {
|
||||
if (!initialized) {
|
||||
initializedSet(true);
|
||||
onTick();
|
||||
}
|
||||
if (serverStatus.maybe_alert && !downAlerted) {
|
||||
downAlertedSet(true);
|
||||
upAlertedSet(false);
|
||||
notifications.create({
|
||||
autoRemove: false,
|
||||
title: "Server down for maintenence",
|
||||
content: "Please check back shortly"
|
||||
});
|
||||
} else if (!serverStatus.maybe_alert && downAlerted && !upAlerted) {
|
||||
upAlertedSet(true);
|
||||
downAlertedSet(false);
|
||||
notifications.create({ autoRemove: false, title: "Server is back online", });
|
||||
}
|
||||
},[downAlerted,initialized,notifications,serverStatus,upAlerted]);
|
||||
|
||||
// useInterval({ interval: 15000, onTick, locked: !initialized, });
|
||||
useInterval({ interval, onTick, locked: !initialized, });
|
||||
|
||||
return serverStatus;
|
||||
};
|
||||
Reference in new issue
Block a user