|
| 1 | +import { useState, useEffect, useRef } from "react"; |
| 2 | + |
| 3 | +const VERSION_STORAGE_KEY = "dioo-version"; |
| 4 | +const LAST_CHECK_KEY = "dioo-version-last-check"; |
| 5 | +const GITHUB_TAGS_URL = "https://api.github.com/repos/globalworming/dioo/tags"; |
| 6 | +const CHECK_INTERVAL_MS = 60 * 60 * 1000; // 1 hour interval to check if day changed |
| 7 | + |
| 8 | +interface VersionState { |
| 9 | + currentVersion: string | null; |
| 10 | + latestVersion: string | null; |
| 11 | + hasUpdate: boolean; |
| 12 | + loading: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +const getDateString = (date: Date) => date.toISOString().split("T")[0]; |
| 16 | + |
| 17 | +const shouldCheckToday = () => { |
| 18 | + const lastCheck = localStorage.getItem(LAST_CHECK_KEY); |
| 19 | + const today = getDateString(new Date()); |
| 20 | + return lastCheck !== today; |
| 21 | +}; |
| 22 | + |
| 23 | +export const useVersionCheck = () => { |
| 24 | + const [state, setState] = useState<VersionState>({ |
| 25 | + currentVersion: null, |
| 26 | + latestVersion: null, |
| 27 | + hasUpdate: false, |
| 28 | + loading: true, |
| 29 | + }); |
| 30 | + const intervalRef = useRef<NodeJS.Timeout | null>(null); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const checkVersion = async () => { |
| 34 | + // Skip if already checked today |
| 35 | + if (!shouldCheckToday()) { |
| 36 | + const storedVersion = localStorage.getItem(VERSION_STORAGE_KEY); |
| 37 | + setState({ |
| 38 | + currentVersion: storedVersion, |
| 39 | + latestVersion: storedVersion, |
| 40 | + hasUpdate: false, |
| 41 | + loading: false, |
| 42 | + }); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + const storedVersion = localStorage.getItem(VERSION_STORAGE_KEY); |
| 48 | + |
| 49 | + const response = await fetch(GITHUB_TAGS_URL, { |
| 50 | + headers: { Accept: "application/vnd.github+json" }, |
| 51 | + }); |
| 52 | + |
| 53 | + if (!response.ok) { |
| 54 | + setState(prev => ({ ...prev, loading: false })); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + const tags = await response.json(); |
| 59 | + const latestTag = tags[0]?.name ?? null; |
| 60 | + |
| 61 | + if (!latestTag) { |
| 62 | + setState(prev => ({ ...prev, loading: false })); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // Mark today as checked |
| 67 | + localStorage.setItem(LAST_CHECK_KEY, getDateString(new Date())); |
| 68 | + |
| 69 | + // If no stored version, store the latest and don't show update |
| 70 | + if (!storedVersion) { |
| 71 | + localStorage.setItem(VERSION_STORAGE_KEY, latestTag); |
| 72 | + setState({ |
| 73 | + currentVersion: latestTag, |
| 74 | + latestVersion: latestTag, |
| 75 | + hasUpdate: false, |
| 76 | + loading: false, |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Compare versions |
| 82 | + const hasUpdate = storedVersion !== latestTag; |
| 83 | + |
| 84 | + setState({ |
| 85 | + currentVersion: storedVersion, |
| 86 | + latestVersion: latestTag, |
| 87 | + hasUpdate, |
| 88 | + loading: false, |
| 89 | + }); |
| 90 | + } catch (error) { |
| 91 | + console.error("Failed to check version:", error); |
| 92 | + setState(prev => ({ ...prev, loading: false })); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + // Initial check |
| 97 | + checkVersion(); |
| 98 | + |
| 99 | + // Set up interval to check if day changed |
| 100 | + intervalRef.current = setInterval(() => { |
| 101 | + if (shouldCheckToday()) { |
| 102 | + checkVersion(); |
| 103 | + } |
| 104 | + }, CHECK_INTERVAL_MS); |
| 105 | + |
| 106 | + return () => { |
| 107 | + if (intervalRef.current) { |
| 108 | + clearInterval(intervalRef.current); |
| 109 | + } |
| 110 | + }; |
| 111 | + }, []); |
| 112 | + |
| 113 | + const updateVersion = () => { |
| 114 | + if (state.latestVersion) { |
| 115 | + localStorage.setItem(VERSION_STORAGE_KEY, state.latestVersion); |
| 116 | + window.location.reload(); |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + const clearVersion = () => { |
| 121 | + localStorage.removeItem(VERSION_STORAGE_KEY); |
| 122 | + }; |
| 123 | + |
| 124 | + return { ...state, updateVersion, clearVersion }; |
| 125 | +}; |
0 commit comments