Skip to content

Commit

Permalink
check every 2 weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
violetadev committed Sep 21, 2024
1 parent 835be54 commit 473d1a6
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState, useEffect } from 'react';
import { gt } from 'semver';
import useLocalStorage from '../useLocalStorage/useLocalStorage';

const fetchAssets = async () => {
try {
Expand Down Expand Up @@ -30,29 +31,47 @@ const useCheckVersion = () => {
const [isNewVersionAvailable, setIsNewVersionAvailable] = useState<
null | boolean
>(null);
const [latestVersion, setLatestVersion] = useState('');
const [latestVersion, setLatestVersion] = useLocalStorage<undefined | string>(
'latestVersion'
);
const [currentVersion, setCurrentVersion] = useState('');
const [lastCheck, setLastCheck] = useLocalStorage<number | undefined>(
'lastCheck'
);
const biWeeklyCheckInMs = 1209600000;

useEffect(() => {
const checkVersion = async () => {
const currentVersionResult = await getAppVersion();
const latestVersionResult = await fetchAssets();

setCurrentVersion(currentVersionResult);
setLatestVersion(latestVersionResult);

const now = Date.now();
if (!latestVersion || !lastCheck || now - lastCheck > biWeeklyCheckInMs) {
const latestVersionResult = await fetchAssets();
if (latestVersionResult) {
setLatestVersion(latestVersionResult);
setLastCheck(now);
}
}
const isNewVersion =
currentVersionResult &&
latestVersionResult &&
gt(latestVersionResult, currentVersionResult);
latestVersion &&
gt(latestVersion, currentVersionResult);

setIsNewVersionAvailable(isNewVersion);
setIsNewVersionAvailable(Boolean(isNewVersion));
};

if (isNewVersionAvailable === null) {
checkVersion();
}
}, [currentVersion, isNewVersionAvailable, latestVersion]);
}, [
isNewVersionAvailable,
lastCheck,
latestVersion,
setLastCheck,
setLatestVersion,
]);

return {
isNewVersionAvailable,
Expand Down

0 comments on commit 473d1a6

Please sign in to comment.