diff --git a/src/main/main.ts b/src/main/main.ts index 9731e4aae..ce7ca1f53 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -3,6 +3,7 @@ import log from 'electron-log'; import { menubar } from 'menubar'; import { APPLICATION } from '../shared/constants'; +import { namespacedEvent } from '../shared/events'; import { isMacOS, isWindows } from '../shared/platform'; import { onFirstRunMaybe } from './first-run'; import { TrayIcons } from './icons'; @@ -93,28 +94,31 @@ app.whenReady().then(async () => { nativeTheme.on('updated', () => { if (nativeTheme.shouldUseDarkColors) { - mb.window.webContents.send('gitify:update-theme', 'DARK'); + mb.window.webContents.send(namespacedEvent('update-theme'), 'DARK'); } else { - mb.window.webContents.send('gitify:update-theme', 'LIGHT'); + mb.window.webContents.send(namespacedEvent('update-theme'), 'LIGHT'); } }); /** * Gitify custom IPC events */ - ipc.handle('gitify:version', () => app.getVersion()); + ipc.handle(namespacedEvent('version'), () => app.getVersion()); - ipc.on('gitify:window-show', () => mb.showWindow()); + ipc.on(namespacedEvent('window-show'), () => mb.showWindow()); - ipc.on('gitify:window-hide', () => mb.hideWindow()); + ipc.on(namespacedEvent('window-hide'), () => mb.hideWindow()); - ipc.on('gitify:quit', () => mb.app.quit()); + ipc.on(namespacedEvent('quit'), () => mb.app.quit()); - ipc.on('gitify:use-alternate-idle-icon', (_, useAlternateIdleIcon) => { - shouldUseAlternateIdleIcon = useAlternateIdleIcon; - }); + ipc.on( + namespacedEvent('use-alternate-idle-icon'), + (_, useAlternateIdleIcon) => { + shouldUseAlternateIdleIcon = useAlternateIdleIcon; + }, + ); - ipc.on('gitify:icon-active', () => { + ipc.on(namespacedEvent('icon-active'), () => { if (!mb.tray.isDestroyed()) { mb.tray.setImage( menuBuilder.isUpdateAvailable() @@ -124,7 +128,7 @@ app.whenReady().then(async () => { } }); - ipc.on('gitify:icon-idle', () => { + ipc.on(namespacedEvent('icon-idle'), () => { if (!mb.tray.isDestroyed()) { if (shouldUseAlternateIdleIcon) { mb.tray.setImage( @@ -142,14 +146,14 @@ app.whenReady().then(async () => { } }); - ipc.on('gitify:update-title', (_, title) => { + ipc.on(namespacedEvent('update-title'), (_, title) => { if (!mb.tray.isDestroyed()) { mb.tray.setTitle(title); } }); ipc.on( - 'gitify:update-keyboard-shortcut', + namespacedEvent('update-keyboard-shortcut'), (_, { enabled, keyboardShortcut }) => { if (!enabled) { globalShortcut.unregister(keyboardShortcut); @@ -166,7 +170,7 @@ app.whenReady().then(async () => { }, ); - ipc.on('gitify:update-auto-launch', (_, settings) => { + ipc.on(namespacedEvent('update-auto-launch'), (_, settings) => { app.setLoginItemSettings(settings); }); }); diff --git a/src/main/utils.ts b/src/main/utils.ts index fc531ad33..03e9de8b3 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -5,6 +5,7 @@ import { dialog, shell } from 'electron'; import log from 'electron-log'; import type { Menubar } from 'menubar'; +import { namespacedEvent } from '../shared/events'; import { logError, logInfo } from '../shared/logger'; export function takeScreenshot(mb: Menubar) { @@ -34,7 +35,7 @@ export function resetApp(mb: Menubar) { }); if (response === resetButtonId) { - mb.window.webContents.send('gitify:reset-app'); + mb.window.webContents.send(namespacedEvent('reset-app')); mb.app.quit(); } } diff --git a/src/renderer/__mocks__/electron.js b/src/renderer/__mocks__/electron.js index c2d0e68c2..5187ed011 100644 --- a/src/renderer/__mocks__/electron.js +++ b/src/renderer/__mocks__/electron.js @@ -1,3 +1,5 @@ +const { namespacedEvent } = require('../../shared/events'); + // @ts-ignore window.Notification = function (title) { this.title = title; @@ -39,7 +41,7 @@ module.exports = { switch (channel) { case 'get-platform': return Promise.resolve('darwin'); - case 'gitify:version': + case namespacedEvent('version'): return Promise.resolve('0.0.1'); default: return Promise.reject(new Error(`Unknown channel: ${channel}`)); diff --git a/src/renderer/components/settings/AppearanceSettings.tsx b/src/renderer/components/settings/AppearanceSettings.tsx index 7be3180fe..74e64ca0e 100644 --- a/src/renderer/components/settings/AppearanceSettings.tsx +++ b/src/renderer/components/settings/AppearanceSettings.tsx @@ -9,6 +9,7 @@ import { } from '@primer/octicons-react'; import { ipcRenderer, webFrame } from 'electron'; import { type FC, useContext, useEffect, useState } from 'react'; +import { namespacedEvent } from '../../../shared/events'; import { AppContext } from '../../context/App'; import { Size, Theme } from '../../types'; import { hasMultipleAccounts } from '../../utils/auth/utils'; @@ -29,11 +30,14 @@ export const AppearanceSettings: FC = () => { ); useEffect(() => { - ipcRenderer.on('gitify:update-theme', (_, updatedTheme: Theme) => { - if (settings.theme === Theme.SYSTEM) { - setTheme(updatedTheme); - } - }); + ipcRenderer.on( + namespacedEvent('update-theme'), + (_, updatedTheme: Theme) => { + if (settings.theme === Theme.SYSTEM) { + setTheme(updatedTheme); + } + }, + ); }, [settings.theme]); window.addEventListener('resize', () => { diff --git a/src/renderer/context/App.tsx b/src/renderer/context/App.tsx index 3322430fb..51d840a23 100644 --- a/src/renderer/context/App.tsx +++ b/src/renderer/context/App.tsx @@ -7,6 +7,7 @@ import { useMemo, useState, } from 'react'; +import { namespacedEvent } from '../../shared/events'; import { useInterval } from '../hooks/useInterval'; import { useNotifications } from '../hooks/useNotifications'; import { @@ -172,7 +173,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => { }, [settings.keyboardShortcut]); useEffect(() => { - ipcRenderer.on('gitify:reset-app', () => { + ipcRenderer.on(namespacedEvent('reset-app'), () => { clearState(); setAuth(defaultAuth); setSettings(defaultSettings); diff --git a/src/renderer/utils/comms.test.ts b/src/renderer/utils/comms.test.ts index a45e8d979..294ef7629 100644 --- a/src/renderer/utils/comms.test.ts +++ b/src/renderer/utils/comms.test.ts @@ -1,4 +1,6 @@ import { ipcRenderer, shell } from 'electron'; + +import { namespacedEvent } from '../../shared/events'; import { mockSettings } from '../__mocks__/state-mocks'; import type { Link } from '../types'; import { @@ -63,50 +65,60 @@ describe('renderer/utils/comms.ts', () => { it('should get app version', async () => { await getAppVersion(); expect(ipcRenderer.invoke).toHaveBeenCalledTimes(1); - expect(ipcRenderer.invoke).toHaveBeenCalledWith('gitify:version'); + expect(ipcRenderer.invoke).toHaveBeenCalledWith(namespacedEvent('version')); }); it('should quit the app', () => { quitApp(); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:quit'); + expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('quit')); }); it('should show the window', () => { showWindow(); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-show'); + expect(ipcRenderer.send).toHaveBeenCalledWith( + namespacedEvent('window-show'), + ); }); it('should hide the window', () => { hideWindow(); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:window-hide'); + expect(ipcRenderer.send).toHaveBeenCalledWith( + namespacedEvent('window-hide'), + ); }); it('should setAutoLaunch (true)', () => { setAutoLaunch(true); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', { - openAtLogin: true, - openAsHidden: true, - }); + expect(ipcRenderer.send).toHaveBeenCalledWith( + namespacedEvent('update-auto-launch'), + { + openAtLogin: true, + openAsHidden: true, + }, + ); }); it('should setAutoLaunch (false)', () => { setAutoLaunch(false); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:update-auto-launch', { - openAsHidden: false, - openAtLogin: false, - }); + expect(ipcRenderer.send).toHaveBeenCalledWith( + namespacedEvent('update-auto-launch'), + { + openAsHidden: false, + openAtLogin: false, + }, + ); }); it('should setAlternateIdleIcon', () => { setAlternateIdleIcon(true); expect(ipcRenderer.send).toHaveBeenCalledWith( - 'gitify:use-alternate-idle-icon', + namespacedEvent('use-alternate-idle-icon'), true, ); }); @@ -115,7 +127,7 @@ describe('renderer/utils/comms.ts', () => { setKeyboardShortcut(true); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); expect(ipcRenderer.send).toHaveBeenCalledWith( - 'gitify:update-keyboard-shortcut', + namespacedEvent('update-keyboard-shortcut'), { enabled: true, keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT, @@ -127,7 +139,7 @@ describe('renderer/utils/comms.ts', () => { setKeyboardShortcut(false); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); expect(ipcRenderer.send).toHaveBeenCalledWith( - 'gitify:update-keyboard-shortcut', + namespacedEvent('update-keyboard-shortcut'), { enabled: false, keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT, @@ -139,13 +151,15 @@ describe('renderer/utils/comms.ts', () => { const notificationsLength = 3; updateTrayIcon(notificationsLength); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-active'); + expect(ipcRenderer.send).toHaveBeenCalledWith( + namespacedEvent('icon-active'), + ); }); it('should send mark the icons as idle', () => { const notificationsLength = 0; updateTrayIcon(notificationsLength); expect(ipcRenderer.send).toHaveBeenCalledTimes(1); - expect(ipcRenderer.send).toHaveBeenCalledWith('gitify:icon-idle'); + expect(ipcRenderer.send).toHaveBeenCalledWith(namespacedEvent('icon-idle')); }); }); diff --git a/src/renderer/utils/comms.ts b/src/renderer/utils/comms.ts index 54f870d98..c33d878ae 100644 --- a/src/renderer/utils/comms.ts +++ b/src/renderer/utils/comms.ts @@ -1,4 +1,5 @@ import { ipcRenderer, shell } from 'electron'; +import { namespacedEvent } from '../../shared/events'; import { defaultSettings } from '../context/App'; import { type Link, OpenPreference } from '../types'; import { Constants } from './constants'; @@ -20,34 +21,34 @@ export function openExternalLink(url: Link): void { } export async function getAppVersion(): Promise { - return await ipcRenderer.invoke('gitify:version'); + return await ipcRenderer.invoke(namespacedEvent('version')); } export function quitApp(): void { - ipcRenderer.send('gitify:quit'); + ipcRenderer.send(namespacedEvent('quit')); } export function showWindow(): void { - ipcRenderer.send('gitify:window-show'); + ipcRenderer.send(namespacedEvent('window-show')); } export function hideWindow(): void { - ipcRenderer.send('gitify:window-hide'); + ipcRenderer.send(namespacedEvent('window-hide')); } export function setAutoLaunch(value: boolean): void { - ipcRenderer.send('gitify:update-auto-launch', { + ipcRenderer.send(namespacedEvent('update-auto-launch'), { openAtLogin: value, openAsHidden: value, }); } export function setAlternateIdleIcon(value: boolean): void { - ipcRenderer.send('gitify:use-alternate-idle-icon', value); + ipcRenderer.send(namespacedEvent('use-alternate-idle-icon'), value); } export function setKeyboardShortcut(keyboardShortcut: boolean): void { - ipcRenderer.send('gitify:update-keyboard-shortcut', { + ipcRenderer.send(namespacedEvent('update-keyboard-shortcut'), { enabled: keyboardShortcut, keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT, }); @@ -55,12 +56,12 @@ export function setKeyboardShortcut(keyboardShortcut: boolean): void { export function updateTrayIcon(notificationsLength = 0): void { if (notificationsLength > 0) { - ipcRenderer.send('gitify:icon-active'); + ipcRenderer.send(namespacedEvent('icon-active')); } else { - ipcRenderer.send('gitify:icon-idle'); + ipcRenderer.send(namespacedEvent('icon-idle')); } } export function updateTrayTitle(title = ''): void { - ipcRenderer.send('gitify:update-title', title); + ipcRenderer.send(namespacedEvent('update-title'), title); } diff --git a/src/shared/constants.ts b/src/shared/constants.ts index ccaa3e4d4..e294f6979 100644 --- a/src/shared/constants.ts +++ b/src/shared/constants.ts @@ -3,5 +3,7 @@ export const APPLICATION = { NAME: 'Gitify', + EVENT_PREFIX: 'gitify:', + WEBSITE: 'https://gitify.io', }; diff --git a/src/shared/events.ts b/src/shared/events.ts new file mode 100644 index 000000000..a311b0a57 --- /dev/null +++ b/src/shared/events.ts @@ -0,0 +1,5 @@ +import { APPLICATION } from './constants'; + +export function namespacedEvent(event: string) { + return `${APPLICATION.EVENT_PREFIX}${event}`; +}