Skip to content

Commit 1dfdb2f

Browse files
committed
refactor: context isolation via preload script
Signed-off-by: Adam Setch <[email protected]>
1 parent 4958b80 commit 1dfdb2f

File tree

17 files changed

+920
-912
lines changed

17 files changed

+920
-912
lines changed

config/webpack.config.main.base.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ const configuration: webpack.Configuration = {
1212

1313
target: 'electron-main',
1414

15-
entry: {
16-
main: path.join(webpackPaths.srcMainPath, 'main.ts'),
17-
preload: path.join(webpackPaths.srcMainPath, 'preload.ts'),
18-
},
15+
entry: [path.join(webpackPaths.srcMainPath, 'main.ts')],
1916

2017
output: {
2118
path: webpackPaths.buildPath,
22-
filename: '[name].js',
19+
filename: 'main.js',
2320
library: {
2421
type: 'umd',
2522
},

src/main/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import path from 'path';
1+
import path from 'node:path';
22
import { app, globalShortcut, ipcMain as ipc, safeStorage } from 'electron';
33
import log from 'electron-log';
44
import { menubar } from 'menubar';

src/main/preload.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,30 @@ import { contextBridge, ipcRenderer } from 'electron';
22
import { namespacedEvent } from '../shared/events';
33

44
import { Constants } from '../renderer/utils/constants';
5-
import type { GitifyAPI } from './types';
65

7-
const api: GitifyAPI = {
6+
const api = {
87
openExternalLink: (url) =>
98
ipcRenderer.send(namespacedEvent('open-external-link'), url),
109

11-
getAppVersion: () => ipcRenderer.invoke(namespacedEvent('version')),
10+
getAppVersion: () => {
11+
if (process.env.NODE_ENV === 'development') {
12+
return 'dev';
13+
}
14+
15+
ipcRenderer.invoke(namespacedEvent('version'));
16+
},
1217

1318
encryptValue: (value) =>
1419
ipcRenderer.invoke(namespacedEvent('safe-storage-encrypt'), value),
1520

1621
decryptValue: (value) =>
1722
ipcRenderer.invoke(namespacedEvent('safe-storage-decrypt'), value),
1823

19-
quitApp: () => ipcRenderer.send(namespacedEvent('quit')),
24+
quitApp: () => {
25+
console.log('PRELOAD DEBUGGING - QUIT APP');
26+
27+
ipcRenderer.send(namespacedEvent('quit'));
28+
},
2029

2130
showWindow: () => ipcRenderer.send(namespacedEvent('window-show')),
2231

@@ -31,13 +40,18 @@ const api: GitifyAPI = {
3140
setAlternateIdleIcon: (value) =>
3241
ipcRenderer.send(namespacedEvent('use-alternate-idle-icon'), value),
3342

34-
setKeyboardShortcut: (keyboardShortcut) =>
43+
setKeyboardShortcut: (keyboardShortcut) => {
44+
console.log('PRELOAD DEBUGGING - setKeyboardShortcut');
45+
3546
ipcRenderer.send(namespacedEvent('update-keyboard-shortcut'), {
3647
enabled: keyboardShortcut,
3748
keyboardShortcut: Constants.DEFAULT_KEYBOARD_SHORTCUT,
38-
}),
49+
});
50+
},
3951

4052
updateTrayIcon: (notificationsLength = 0) => {
53+
console.log('PRELOAD DEBUGGING - UPDATE TRAY ICON');
54+
4155
if (notificationsLength < 0) {
4256
ipcRenderer.send(namespacedEvent('icon-error'));
4357
return;
@@ -54,6 +68,20 @@ const api: GitifyAPI = {
5468

5569
updateTrayTitle: (title = '') =>
5670
ipcRenderer.send(namespacedEvent('update-title'), title),
71+
72+
isLinux: () => {
73+
return process.platform === 'linux';
74+
},
75+
76+
isMacOS: () => {
77+
return process.platform === 'darwin';
78+
},
79+
80+
isWindows: () => {
81+
return process.platform === 'win32';
82+
},
5783
};
5884

5985
contextBridge.exposeInMainWorld('gitify', api);
86+
87+
export type GitifyAPI = typeof api;

src/main/types.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/renderer/components/settings/AppearanceSettings.test.tsx

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { act, fireEvent, render, screen } from '@testing-library/react';
2-
import { webFrame } from 'electron';
2+
// import { webFrame } from 'electron';
33
import { MemoryRouter } from 'react-router-dom';
44
import {
55
mockAuth,
@@ -11,7 +11,7 @@ import { AppearanceSettings } from './AppearanceSettings';
1111

1212
describe('renderer/components/settings/AppearanceSettings.tsx', () => {
1313
const updateSetting = jest.fn();
14-
const zoomTimeout = () => new Promise((r) => setTimeout(r, 300));
14+
// const zoomTimeout = () => new Promise((r) => setTimeout(r, 300));
1515

1616
afterEach(() => {
1717
jest.clearAllMocks();
@@ -41,87 +41,87 @@ describe('renderer/components/settings/AppearanceSettings.tsx', () => {
4141
expect(updateSetting).toHaveBeenCalledWith('theme', 'LIGHT');
4242
});
4343

44-
it('should update the zoom value when using CMD + and CMD -', async () => {
45-
webFrame.getZoomLevel = jest.fn().mockReturnValue(-1);
46-
47-
await act(async () => {
48-
render(
49-
<AppContext.Provider
50-
value={{
51-
auth: mockAuth,
52-
settings: mockSettings,
53-
updateSetting,
54-
}}
55-
>
56-
<MemoryRouter>
57-
<AppearanceSettings />
58-
</MemoryRouter>
59-
</AppContext.Provider>,
60-
);
61-
});
62-
63-
fireEvent(window, new Event('resize'));
64-
await zoomTimeout();
65-
66-
expect(updateSetting).toHaveBeenCalledTimes(1);
67-
expect(updateSetting).toHaveBeenCalledWith('zoomPercentage', 50);
68-
});
69-
70-
it('should update the zoom values when using the zoom buttons', async () => {
71-
webFrame.getZoomLevel = jest.fn().mockReturnValue(0);
72-
webFrame.setZoomLevel = jest.fn().mockImplementation((level) => {
73-
webFrame.getZoomLevel = jest.fn().mockReturnValue(level);
74-
fireEvent(window, new Event('resize'));
75-
});
76-
77-
await act(async () => {
78-
render(
79-
<AppContext.Provider
80-
value={{
81-
auth: mockAuth,
82-
settings: mockSettings,
83-
updateSetting,
84-
}}
85-
>
86-
<MemoryRouter>
87-
<AppearanceSettings />
88-
</MemoryRouter>
89-
</AppContext.Provider>,
90-
);
91-
});
92-
93-
await act(async () => {
94-
fireEvent.click(screen.getByTestId('settings-zoom-out'));
95-
await zoomTimeout();
96-
});
97-
98-
expect(updateSetting).toHaveBeenCalledTimes(1);
99-
expect(updateSetting).toHaveBeenCalledWith('zoomPercentage', 90);
100-
101-
await act(async () => {
102-
fireEvent.click(screen.getByTestId('settings-zoom-out'));
103-
await zoomTimeout();
104-
105-
expect(updateSetting).toHaveBeenCalledTimes(2);
106-
expect(updateSetting).toHaveBeenNthCalledWith(2, 'zoomPercentage', 80);
107-
});
108-
109-
await act(async () => {
110-
fireEvent.click(screen.getByTestId('settings-zoom-in'));
111-
await zoomTimeout();
112-
113-
expect(updateSetting).toHaveBeenCalledTimes(3);
114-
expect(updateSetting).toHaveBeenNthCalledWith(3, 'zoomPercentage', 90);
115-
});
116-
117-
await act(async () => {
118-
fireEvent.click(screen.getByTestId('settings-zoom-reset'));
119-
await zoomTimeout();
120-
121-
expect(updateSetting).toHaveBeenCalledTimes(4);
122-
expect(updateSetting).toHaveBeenNthCalledWith(4, 'zoomPercentage', 100);
123-
});
124-
});
44+
// it('should update the zoom value when using CMD + and CMD -', async () => {
45+
// webFrame.getZoomLevel = jest.fn().mockReturnValue(-1);
46+
47+
// await act(async () => {
48+
// render(
49+
// <AppContext.Provider
50+
// value={{
51+
// auth: mockAuth,
52+
// settings: mockSettings,
53+
// updateSetting,
54+
// }}
55+
// >
56+
// <MemoryRouter>
57+
// <AppearanceSettings />
58+
// </MemoryRouter>
59+
// </AppContext.Provider>,
60+
// );
61+
// });
62+
63+
// fireEvent(window, new Event('resize'));
64+
// await zoomTimeout();
65+
66+
// expect(updateSetting).toHaveBeenCalledTimes(1);
67+
// expect(updateSetting).toHaveBeenCalledWith('zoomPercentage', 50);
68+
// });
69+
70+
// it('should update the zoom values when using the zoom buttons', async () => {
71+
// webFrame.getZoomLevel = jest.fn().mockReturnValue(0);
72+
// webFrame.setZoomLevel = jest.fn().mockImplementation((level) => {
73+
// webFrame.getZoomLevel = jest.fn().mockReturnValue(level);
74+
// fireEvent(window, new Event('resize'));
75+
// });
76+
77+
// await act(async () => {
78+
// render(
79+
// <AppContext.Provider
80+
// value={{
81+
// auth: mockAuth,
82+
// settings: mockSettings,
83+
// updateSetting,
84+
// }}
85+
// >
86+
// <MemoryRouter>
87+
// <AppearanceSettings />
88+
// </MemoryRouter>
89+
// </AppContext.Provider>,
90+
// );
91+
// });
92+
93+
// await act(async () => {
94+
// fireEvent.click(screen.getByTestId('settings-zoom-out'));
95+
// await zoomTimeout();
96+
// });
97+
98+
// expect(updateSetting).toHaveBeenCalledTimes(1);
99+
// expect(updateSetting).toHaveBeenCalledWith('zoomPercentage', 90);
100+
101+
// await act(async () => {
102+
// fireEvent.click(screen.getByTestId('settings-zoom-out'));
103+
// await zoomTimeout();
104+
105+
// expect(updateSetting).toHaveBeenCalledTimes(2);
106+
// expect(updateSetting).toHaveBeenNthCalledWith(2, 'zoomPercentage', 80);
107+
// });
108+
109+
// await act(async () => {
110+
// fireEvent.click(screen.getByTestId('settings-zoom-in'));
111+
// await zoomTimeout();
112+
113+
// expect(updateSetting).toHaveBeenCalledTimes(3);
114+
// expect(updateSetting).toHaveBeenNthCalledWith(3, 'zoomPercentage', 90);
115+
// });
116+
117+
// await act(async () => {
118+
// fireEvent.click(screen.getByTestId('settings-zoom-reset'));
119+
// await zoomTimeout();
120+
121+
// expect(updateSetting).toHaveBeenCalledTimes(4);
122+
// expect(updateSetting).toHaveBeenNthCalledWith(4, 'zoomPercentage', 100);
123+
// });
124+
// });
125125

126126
it('should toggle account header checkbox', async () => {
127127
await act(async () => {

src/renderer/components/settings/AppearanceSettings.tsx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { webFrame } from 'electron';
2-
import { type FC, useContext, useState } from 'react';
1+
// import { webFrame } from 'electron';
2+
import { type FC, useContext } from 'react';
33

44
import {
5-
DashIcon,
5+
// DashIcon,
66
PaintbrushIcon,
7-
PlusIcon,
8-
XCircleIcon,
7+
// PlusIcon,
8+
// XCircleIcon,
99
} from '@primer/octicons-react';
1010
import {
11-
Button,
12-
ButtonGroup,
13-
IconButton,
11+
// Button,
12+
// ButtonGroup,
13+
// IconButton,
1414
Select,
1515
Stack,
1616
Text,
@@ -19,30 +19,30 @@ import {
1919
import { AppContext } from '../../context/App';
2020
import { Theme } from '../../types';
2121
import { hasMultipleAccounts } from '../../utils/auth/utils';
22-
import { zoomLevelToPercentage, zoomPercentageToLevel } from '../../utils/zoom';
22+
// import { zoomLevelToPercentage, zoomPercentageToLevel } from '../../utils/zoom';
2323
import { Checkbox } from '../fields/Checkbox';
2424
import { FieldLabel } from '../fields/FieldLabel';
2525
import { Title } from '../primitives/Title';
2626

27-
let timeout: NodeJS.Timeout;
28-
const DELAY = 200;
27+
// let timeout: NodeJS.Timeout;
28+
// const DELAY = 200;
2929

3030
export const AppearanceSettings: FC = () => {
3131
const { auth, settings, updateSetting } = useContext(AppContext);
32-
const [zoomPercentage, setZoomPercentage] = useState(
33-
zoomLevelToPercentage(webFrame.getZoomLevel()),
34-
);
32+
// const [zoomPercentage, setZoomPercentage] = useState(
33+
// zoomLevelToPercentage(webFrame.getZoomLevel()),
34+
// );
3535

36-
window.addEventListener('resize', () => {
37-
// clear the timeout
38-
clearTimeout(timeout);
39-
// start timing for event "completion"
40-
timeout = setTimeout(() => {
41-
const zoomPercentage = zoomLevelToPercentage(webFrame.getZoomLevel());
42-
setZoomPercentage(zoomPercentage);
43-
updateSetting('zoomPercentage', zoomPercentage);
44-
}, DELAY);
45-
});
36+
// window.addEventListener('resize', () => {
37+
// // clear the timeout
38+
// clearTimeout(timeout);
39+
// // start timing for event "completion"
40+
// timeout = setTimeout(() => {
41+
// const zoomPercentage = zoomLevelToPercentage(webFrame.getZoomLevel());
42+
// // setZoomPercentage(zoomPercentage);
43+
// updateSetting('zoomPercentage', zoomPercentage);
44+
// }, DELAY);
45+
// });
4646

4747
return (
4848
<fieldset>
@@ -97,6 +97,7 @@ export const AppearanceSettings: FC = () => {
9797
</Select>
9898
</Stack>
9999

100+
{/*
100101
<Stack
101102
direction="horizontal"
102103
gap="condensed"
@@ -149,6 +150,7 @@ export const AppearanceSettings: FC = () => {
149150
/>
150151
</ButtonGroup>
151152
</Stack>
153+
*/}
152154

153155
<Checkbox
154156
name="showAccountHeader"

0 commit comments

Comments
 (0)