Skip to content

Commit

Permalink
feat: check for updates
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jul 22, 2024
1 parent 7d8f5fb commit b2ed9b2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
15 changes: 6 additions & 9 deletions src/components/settings/SettingsFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { ipcRenderer } from 'electron';
import { type FC, useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { BUTTON_CLASS_NAME } from '../../styles/gitify';
import { AutoUpdaterCheck, IconColor, Size } from '../../types';
import { IconColor, Size } from '../../types';
import { getAppVersion, quitApp } from '../../utils/comms';
import { openGitifyReleaseNotes } from '../../utils/links';

export const SettingsFooter: FC = () => {
const [appVersion, setAppVersion] = useState<string | null>(null);
const [newVersionAvailable, setNewVersionAvailable] = useState(false);
const [isUpdateAvailable, setIsUpdateAvailable] = useState(false);
const navigate = useNavigate();

useEffect(() => {
Expand All @@ -29,12 +29,9 @@ export const SettingsFooter: FC = () => {
}, []);

useEffect(() => {
ipcRenderer.on(
'gitify:auto-updater',
(_, version: AutoUpdaterCheck, _info: string) => {
setNewVersionAvailable(version === AutoUpdaterCheck.UPDATE_AVAILABLE);
},
);
ipcRenderer.on('gitify:auto-updater', (_, isUpdateAvailable: boolean) => {
setIsUpdateAvailable(isUpdateAvailable);
});
}, []);

return (
Expand All @@ -47,7 +44,7 @@ export const SettingsFooter: FC = () => {
>
<span className="flex items-center gap-1">
<span aria-label="app-version">Gitify {appVersion}</span>
{newVersionAvailable ? (
{isUpdateAvailable ? (
<span title="New version available">
<AlertFillIcon size={Size.XSMALL} className={IconColor.YELLOW} />
</span>
Expand Down
55 changes: 32 additions & 23 deletions src/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,37 @@ const browserWindowOpts = {
},
};

let isUpdateAvailable = false;
let isUpdateDownloaded = false;

const contextMenu = Menu.buildFromTemplate([
{
role: 'reload',
label: 'Check for updates',
click: () => {
checkForUpdates();
},
},
{
role: 'toggleDevTools',
label: 'An update is available',
enabled: false,
visible: isUpdateAvailable,
},
{ type: 'separator' },
{
label: 'Check for updates',
label: 'Restart to update',
visible: isUpdateDownloaded,
click: () => {
checkForUpdates();
autoUpdater.quitAndInstall();
},
},
{ type: 'separator' },

{
role: 'reload',
},
{
role: 'toggleDevTools',
},
{ type: 'separator' },
{
label: 'Quit',
click: () => {
Expand Down Expand Up @@ -105,29 +122,21 @@ app.whenReady().then(async () => {
checkForUpdates();
setInterval(checkForUpdates, 24 * 60 * 60 * 1000); // 24 hours

autoUpdater.on('update-available', (info) => {
log.info('Auto Updater: New update available', info);
mb.window.webContents.send(
'gitify:auto-updater',
'UPDATE_AVAILABLE',
info,
);
autoUpdater.on('update-available', () => {
log.info('Auto Updater: New update available');
isUpdateAvailable = true;
mb.window.webContents.send('gitify:auto-updater', isUpdateAvailable);
});

autoUpdater.on('update-not-available', (info) => {
autoUpdater.on('update-not-available', () => {
log.info('Auto Updater: Already on the latest version');
mb.window.webContents.send(
'gitify:auto-updater',
'UPDATE_NOT_AVAILABLE',
info,
);
isUpdateAvailable = false;
mb.window.webContents.send('gitify:auto-updater', isUpdateAvailable);
});

autoUpdater.on('update-downloaded', (_info) => {
log.info('Auto Updater: Update downloaded; will install in 5 seconds');
setTimeout(() => {
autoUpdater.quitAndInstall();
}, 5000);
autoUpdater.on('update-downloaded', () => {
log.info('Auto Updater: Update downloaded');
isUpdateDownloaded = true;
});
});

Expand Down
5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ export enum Theme {
DARK = 'DARK',
}

export enum AutoUpdaterCheck {
UPDATE_AVAILABLE = 'UPDATE_AVAILABLE',
UPDATE_NOT_AVAILABLE = 'UPDATE_NOT_AVAILABLE',
}

export enum OpenPreference {
FOREGROUND = 'FOREGROUND',
BACKGROUND = 'FOREGROUND',
Expand Down

0 comments on commit b2ed9b2

Please sign in to comment.