mirror of
https://github.com/stoatchat/for-legacy-web.git
synced 2026-08-03 10:14:43 +00:00
feat: handle system alerts and poll rate changes
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import isEqual from "lodash.isequal";
|
||||
import semver from "semver";
|
||||
import { ulid } from "ulid";
|
||||
import { registerSW } from "virtual:pwa-register";
|
||||
|
||||
import { internalEmit } from "./lib/eventEmitter";
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
|
||||
import { internalEmit, internalSubscribe } from "./lib/eventEmitter";
|
||||
|
||||
import { modalController } from "./context/modals";
|
||||
|
||||
@@ -33,15 +36,67 @@ export const updateSW = registerSW({
|
||||
},
|
||||
});
|
||||
|
||||
let currentPollRate: number;
|
||||
let scheduledTask: number;
|
||||
|
||||
/**
|
||||
* Schedule version checker
|
||||
* @param poll_rate Set poll rate in milliseconds
|
||||
*/
|
||||
function schedule(poll_rate = INTERVAL_HOUR) {
|
||||
if (poll_rate !== currentPollRate) {
|
||||
currentPollRate = poll_rate;
|
||||
clearInterval(scheduledTask);
|
||||
scheduledTask = setInterval(
|
||||
checkVersion,
|
||||
poll_rate,
|
||||
) as unknown as number;
|
||||
}
|
||||
}
|
||||
|
||||
let currentAlert: SystemAlert | undefined;
|
||||
type SystemAlert = {
|
||||
text: string;
|
||||
dismissable?: boolean;
|
||||
actions?: {
|
||||
text: string;
|
||||
type: "internal" | "external";
|
||||
href: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the current system alert
|
||||
*/
|
||||
export function useSystemAlert() {
|
||||
const [alert, setAlert] = useState(currentAlert);
|
||||
useEffect(() => internalSubscribe("System", "alert", setAlert as any), []);
|
||||
return alert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the client is out of date
|
||||
*/
|
||||
async function checkVersion() {
|
||||
const { version } = (await fetch("https://api.revolt.chat/release").then(
|
||||
(res) => res.json(),
|
||||
)) as { version: string };
|
||||
const { version, poll_rate, alert } = (await fetch(
|
||||
"https://api.revolt.chat/release",
|
||||
).then((res) => res.json())) as {
|
||||
version: string;
|
||||
poll_rate?: number;
|
||||
alert?: SystemAlert;
|
||||
};
|
||||
|
||||
if (!semver.satisfies(APP_VERSION, version) && APP_VERSION !== version) {
|
||||
// Re-schedule if necessary
|
||||
schedule(poll_rate);
|
||||
|
||||
// Apply any active alerts
|
||||
if (!isEqual(alert, currentAlert)) {
|
||||
currentAlert = alert;
|
||||
internalEmit("System", "alert", alert);
|
||||
}
|
||||
|
||||
// Check if we need to update
|
||||
if (version !== "0.5.3-7" && !semver.satisfies(APP_VERSION, version)) {
|
||||
// Let the worker know we should immediately refresh
|
||||
forceUpdate = true;
|
||||
|
||||
@@ -59,6 +114,6 @@ async function checkVersion() {
|
||||
|
||||
if (import.meta.env.VITE_API_URL === "https://api.revolt.chat") {
|
||||
// Check for critical updates hourly
|
||||
schedule();
|
||||
checkVersion();
|
||||
setInterval(checkVersion, INTERVAL_HOUR);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user