Skip to content

Commit

Permalink
check for new version 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 a192e05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
6 changes: 3 additions & 3 deletions desktop-app/src/renderer/components/ToolBar/Menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const Menu = () => {
<div className="relative mr-2 flex items-center" ref={ref}>
<Button onClick={handleFlyout} isActive={isMenuFlyoutOpen}>
<Icon icon="carbon:overflow-menu-vertical" />
{notifications?.length > 0 && Boolean(hasNewNotifications) && (
<NotificationsBubble />
)}
{notifications &&
notifications?.length > 0 &&
Boolean(hasNewNotifications) && <NotificationsBubble />}
</Button>
<div style={{ visibility: isMenuFlyoutOpen ? 'visible' : 'hidden' }}>
<MenuFlyout closeFlyout={onClose} />
Expand Down
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 a192e05

Please sign in to comment.