peppa/apps/web/components/VersionWatch.tsx

32 lines
1.5 KiB
TypeScript

"use client";
import { useState } from "react";
import { useLive } from "@/lib/live";
import { RefreshCw } from "lucide-react";
import { useT } from "@/components/I18n";
/**
* PEP-114: a page opened before the site was updated may press buttons the new server doesn't
* know — nothing happens and nothing is said. The live channel (`version`, PEP-225) brings the
* server's boot id on every (re)connect: a restarted web means a new id, and the page asks to
* reload. Never reloads by itself: the person may be in the middle of typing.
*/
export function VersionWatch({ boot }: { boot: string }) {
const t = useT();
const [stale, setStale] = useState(false);
useLive("version", (e) => {
const now = e.type === "data" ? (e.data as { boot?: string }).boot : undefined;
if (now && now !== boot) setStale(true);
});
if (!stale) return null;
return (
<div role="alert" className="sticky top-0 z-40 flex flex-wrap items-center justify-center gap-3 border-b border-primary/50 bg-primary/15 px-4 py-2 text-sm">
<span>{t("Peppa FACTORY обновилась. Обновите страницу — иначе кнопки на ней могут не сработать.")}</span>
<button type="button" onClick={() => window.location.reload()} className="inline-flex items-center gap-1.5 rounded-md bg-primary px-3 py-1 font-medium text-primary-foreground">
<RefreshCw className="size-3.5" /> {t("Обновить страницу")}
</button>
</div>
);
}