All files / app/hooks use-check-for-updates.ts

0% Statements 0/31
0% Branches 0/4
0% Functions 0/9
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                             
import { useInterval } from './use-interval';
import { checkForNewRelease, GithubReleases } from '@api/check-for-new-release';
import { WALLET_VERSION } from '@constants/index';
import { whenNetwork } from '@utils/network-utils';
import { safeAwait } from '@utils/safe-await';
import compareVersions from 'compare-versions';
import { useCallback, useEffect, useState } from 'react';
 
const UPDATE_CHECK_INTERVAL = 300_000;
const NEW_WALLET_STARTING_MAJOR_VERSION = whenNetwork<string>({
  mainnet: '4.0.0',
  testnet: '4.0.0-beta.0',
});
 
export function useCheckForUpdates() {
  const [newerReleaseAvailable, setNewerReleaseAvailable] = useState(false);
  const [latestRelease, setLatestRelease] = useState<null | GithubReleases[0]>(null);
 
  const checkForUpdate = useCallback(async () => {
    const [err, resp] = await safeAwait(checkForNewRelease());
    Iif (err || !resp) return;
 
    const latestReleases = resp
      // Prevent runtime errors incase an invalid tag makes it into upstream
      .filter(release => compareVersions.validate(release.tag_name))
      .filter(release => whenNetwork({ mainnet: !release.prerelease, testnet: release.prerelease }))
      .filter(release => release.tag_name.startsWith('v'))
      .filter(release =>
        compareVersions.compare(release.tag_name, NEW_WALLET_STARTING_MAJOR_VERSION, '>')
      );
 
    Iif (latestReleases[0]) setLatestRelease(latestReleases[0]);
 
    const isThereNewerRelease = latestReleases.some(release =>
      compareVersions.compare(release.tag_name, WALLET_VERSION, '>')
    );
 
    setNewerReleaseAvailable(isThereNewerRelease);
  }, []);
 
  useEffect(() => void checkForUpdate(), [checkForUpdate]);
 
  useInterval(() => void checkForUpdate(), UPDATE_CHECK_INTERVAL);
 
  return { isNewerReleaseAvailable: newerReleaseAvailable, latestRelease };
}