- CI: ci/changed-services.sh compares HEAD with the last deployment's v* tag: the Rust tests run only when a Rust service changed, only the changed images are built, and the server's .env gets a tag per built service (TAG_FRONTEND, TAG_BACKEND, ...; TAG stays the fallback), so only their containers restart. Everything when in doubt. - The frontend image knows its version (APP_VERSION, the commit): Next's deploymentId (a page from an older version reloads on navigation) and /api/version. VersionWatcher checks every 5 minutes and when the tab comes to the front: idle with nothing typed, the page reloads; otherwise a "Frissült az oldal" notice offers it. - Home page: "Nem sablonszöveg, hanem a te elemzésed" in the hero, the "Miért más?" section now about analyses written for each chart; the two hero buttons side by side on phones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
90 lines
3.4 KiB
TypeScript
90 lines
3.4 KiB
TypeScript
"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>
|
|
);
|
|
}
|