Files
sorsfonalak/services/frontend/src/components/VersionWatcher.tsx
T

90 lines
3.4 KiB
TypeScript
Raw Normal View History

"use client";
import { useEffect, useState } from "react";
/** How often an open page asks (only while it is in front). */
const EVERY_MS = 5 * 60 * 1000;
/** No key, click or touch for this long counts as idle. */
const IDLE_MS = 2 * 60 * 1000;
/** Typed text that a reload would lose (a reply being written, a form filled in). */
function unsaved(): boolean {
const fields = document.querySelectorAll<HTMLInputElement | HTMLTextAreaElement>(
"textarea, input:not([type]), input[type=text], input[type=email], input[type=search], input[type=password]",
);
return Array.from(fields).some(
(f) => !f.readOnly && !f.disabled && f.value.trim() !== "" && f.value !== f.defaultValue,
);
}
/**
* When a new version of the site is out (its /api/version differs from the
* one this page was built from, Next's `data-dpl-id`): an idle page with
* nothing typed in reloads itself; otherwise a small notice offers the reload.
* (A click on a link reloads anyway: Next's skew protection.)
*/
export function VersionWatcher() {
const [fresh, setFresh] = useState(false);
useEffect(() => {
const mine = document.documentElement.dataset.dplId;
if (!mine) return;
let lastInput = Date.now();
let dismissed = false;
const touched = () => {
lastInput = Date.now();
};
const check = async () => {
if (document.visibilityState !== "visible" || dismissed) return;
try {
const res = await fetch("/api/version", { cache: "no-store" });
const { version } = (await res.json()) as { version: string | null };
if (!version || version === mine) return;
const idle = Date.now() - lastInput > IDLE_MS;
if (idle && !unsaved() && !document.querySelector("dialog[open]")) window.location.reload();
else setFresh(true);
} catch {
// Offline or mid-deployment: the next check tries again.
}
};
const timer = window.setInterval(check, EVERY_MS);
const onVisible = () => document.visibilityState === "visible" && void check();
document.addEventListener("visibilitychange", onVisible);
window.addEventListener("focus", onVisible);
window.addEventListener("online", onVisible);
for (const e of ["keydown", "pointerdown", "touchstart"]) window.addEventListener(e, touched, { passive: true });
const later = () => {
dismissed = true;
setFresh(false);
window.setTimeout(() => (dismissed = false), 30 * 60 * 1000);
};
window.addEventListener("version-watcher:later", later);
return () => {
window.clearInterval(timer);
document.removeEventListener("visibilitychange", onVisible);
window.removeEventListener("focus", onVisible);
window.removeEventListener("online", onVisible);
for (const e of ["keydown", "pointerdown", "touchstart"]) window.removeEventListener(e, touched);
window.removeEventListener("version-watcher:later", later);
};
}, []);
if (!fresh) return null;
return (
<div className="update-note" role="status">
<span>Frissült az oldal: elérhető az új változata.</span>
<button type="button" className="btn btn-primary btn-sm" onClick={() => window.location.reload()}>
Frissítés
</button>
<button
type="button"
className="update-note-later"
aria-label="Később"
onClick={() => window.dispatchEvent(new Event("version-watcher:later"))}
>
Később
</button>
</div>
);
}