|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | +import { toast } from "sonner"; |
| 3 | + |
| 4 | +const VERSION_URL = "/version.json"; |
| 5 | +const INITIAL_CHECK_DELAY_MS = 30_000; |
| 6 | +const CHECK_INTERVAL_MS = 5 * 60_000; |
| 7 | +const FOCUS_CHECK_THROTTLE_MS = 60_000; |
| 8 | + |
| 9 | +type AppVersion = typeof __PINTO_APP_VERSION__; |
| 10 | + |
| 11 | +const fetchLatestVersion = async (): Promise<Partial<AppVersion> | undefined> => { |
| 12 | + const response = await fetch(`${VERSION_URL}?t=${Date.now()}`, { |
| 13 | + cache: "no-store", |
| 14 | + headers: { |
| 15 | + Accept: "application/json", |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | + if (!response.ok) { |
| 20 | + return undefined; |
| 21 | + } |
| 22 | + |
| 23 | + return response.json(); |
| 24 | +}; |
| 25 | + |
| 26 | +const reloadApp = () => { |
| 27 | + window.location.reload(); |
| 28 | +}; |
| 29 | + |
| 30 | +export default function AppVersionGuard() { |
| 31 | + const isCheckingRef = useRef(false); |
| 32 | + const isStaleRef = useRef(false); |
| 33 | + const hasNotifiedRef = useRef(false); |
| 34 | + const lastCheckAtRef = useRef(0); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (!import.meta.env.PROD) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + let isDisposed = false; |
| 42 | + |
| 43 | + const handleStaleVersion = () => { |
| 44 | + if (isDisposed || isStaleRef.current) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + isStaleRef.current = true; |
| 49 | + |
| 50 | + if (document.visibilityState === "hidden") { |
| 51 | + reloadApp(); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (hasNotifiedRef.current) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + hasNotifiedRef.current = true; |
| 60 | + toast.warning("A new Pinto version is available.", { |
| 61 | + duration: Infinity, |
| 62 | + action: { |
| 63 | + label: "Reload", |
| 64 | + onClick: reloadApp, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }; |
| 68 | + |
| 69 | + const checkForLatestVersion = async (force = false) => { |
| 70 | + if (isDisposed || isStaleRef.current || isCheckingRef.current) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const now = Date.now(); |
| 75 | + if (!force && now - lastCheckAtRef.current < FOCUS_CHECK_THROTTLE_MS) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + lastCheckAtRef.current = now; |
| 80 | + isCheckingRef.current = true; |
| 81 | + |
| 82 | + try { |
| 83 | + const latestVersion = await fetchLatestVersion(); |
| 84 | + if (latestVersion?.buildId && latestVersion.buildId !== __PINTO_APP_VERSION__.buildId) { |
| 85 | + handleStaleVersion(); |
| 86 | + } |
| 87 | + } catch { |
| 88 | + // Version checks are best-effort; temporary network failures should not affect app usage. |
| 89 | + } finally { |
| 90 | + isCheckingRef.current = false; |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const handleFocus = () => { |
| 95 | + void checkForLatestVersion(); |
| 96 | + }; |
| 97 | + |
| 98 | + const handleVisibilityChange = () => { |
| 99 | + if (document.visibilityState === "hidden" && isStaleRef.current) { |
| 100 | + reloadApp(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (document.visibilityState === "visible") { |
| 105 | + void checkForLatestVersion(true); |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + const handlePageShow = (event: PageTransitionEvent) => { |
| 110 | + if (event.persisted) { |
| 111 | + void checkForLatestVersion(true); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + const initialCheckId = window.setTimeout(() => void checkForLatestVersion(true), INITIAL_CHECK_DELAY_MS); |
| 116 | + const intervalId = window.setInterval(() => void checkForLatestVersion(), CHECK_INTERVAL_MS); |
| 117 | + |
| 118 | + window.addEventListener("focus", handleFocus); |
| 119 | + window.addEventListener("pageshow", handlePageShow); |
| 120 | + document.addEventListener("visibilitychange", handleVisibilityChange); |
| 121 | + |
| 122 | + return () => { |
| 123 | + isDisposed = true; |
| 124 | + window.clearTimeout(initialCheckId); |
| 125 | + window.clearInterval(intervalId); |
| 126 | + window.removeEventListener("focus", handleFocus); |
| 127 | + window.removeEventListener("pageshow", handlePageShow); |
| 128 | + document.removeEventListener("visibilitychange", handleVisibilityChange); |
| 129 | + }; |
| 130 | + }, []); |
| 131 | + |
| 132 | + return null; |
| 133 | +} |
0 commit comments