File size: 1,824 Bytes
c0a9bce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { useState, useEffect } from 'react';
import { checkForUpdates, acknowledgeUpdate } from '~/lib/api/updates';

const LAST_ACKNOWLEDGED_VERSION_KEY = 'bolt_last_acknowledged_version';

export const useUpdateCheck = () => {
  const [hasUpdate, setHasUpdate] = useState(false);
  const [currentVersion, setCurrentVersion] = useState<string>('');
  const [lastAcknowledgedVersion, setLastAcknowledgedVersion] = useState<string | null>(() => {
    try {
      return localStorage.getItem(LAST_ACKNOWLEDGED_VERSION_KEY);
    } catch {
      return null;
    }
  });

  useEffect(() => {
    const checkUpdate = async () => {
      try {
        const { available, version } = await checkForUpdates();
        setCurrentVersion(version);

        // Only show update if it's a new version and hasn't been acknowledged
        setHasUpdate(available && version !== lastAcknowledgedVersion);
      } catch (error) {
        console.error('Failed to check for updates:', error);
      }
    };

    // Check immediately and then every 30 minutes
    checkUpdate();

    const interval = setInterval(checkUpdate, 30 * 60 * 1000);

    return () => clearInterval(interval);
  }, [lastAcknowledgedVersion]);

  const handleAcknowledgeUpdate = async () => {
    try {
      const { version } = await checkForUpdates();
      await acknowledgeUpdate(version);

      // Store in localStorage
      try {
        localStorage.setItem(LAST_ACKNOWLEDGED_VERSION_KEY, version);
      } catch (error) {
        console.error('Failed to persist acknowledged version:', error);
      }

      setLastAcknowledgedVersion(version);
      setHasUpdate(false);
    } catch (error) {
      console.error('Failed to acknowledge update:', error);
    }
  };

  return { hasUpdate, currentVersion, acknowledgeUpdate: handleAcknowledgeUpdate };
};