Skip to content

Commit

Permalink
:electron: Restart server silently when adding self signed cert and add some l…
Browse files Browse the repository at this point in the history
…ogs (#3431)

* restart server silently on add self signed cert and add some logging

* release notes

* fix name

* updating names to be more specific

* removing setloading

* feedback
  • Loading branch information
MikesGlitch authored Sep 17, 2024
1 parent 6c87d85 commit 3743a32
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/desktop-client/src/browser-preload.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ global.Actual = {
window.location.reload();
},

restartElectronServer: () => {},

openFileDialog: async ({ filters = [] }) => {
return new Promise(resolve => {
let createdElement = false;
Expand Down
10 changes: 8 additions & 2 deletions packages/desktop-client/src/components/manager/ConfigServer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-strict-ignore
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import {
Expand Down Expand Up @@ -34,8 +34,15 @@ export function ConfigServer() {
}, [currentUrl]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

const restartElectronServer = useCallback(() => {
globalThis.window.Actual.restartElectronServer();
setError(null);
}, []);

const [_serverSelfSignedCert, setServerSelfSignedCert] = useGlobalPref(
'serverSelfSignedCert',
restartElectronServer,
);

function getErrorMessage(error: string) {
Expand Down Expand Up @@ -101,7 +108,6 @@ export function ConfigServer() {

if (selfSignedCertificateLocation) {
setServerSelfSignedCert(selfSignedCertificateLocation[0]);
globalThis.window.Actual.relaunch(); // relaunch to use the certificate
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/desktop-client/src/hooks/useGlobalPref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ type SetGlobalPrefAction<K extends keyof GlobalPrefs> = (

export function useGlobalPref<K extends keyof GlobalPrefs>(
prefName: K,
onSaveGlobalPrefs?: () => void,
): [GlobalPrefs[K], SetGlobalPrefAction<K>] {
const dispatch = useDispatch();
const setGlobalPref = useCallback<SetGlobalPrefAction<K>>(
value => {
dispatch(saveGlobalPrefs({ [prefName]: value } as GlobalPrefs));
dispatch(
saveGlobalPrefs(
{
[prefName]: value,
} as GlobalPrefs,
onSaveGlobalPrefs,
),
);
},
[prefName, dispatch],
[prefName, dispatch, onSaveGlobalPrefs],
);
const globalPref = useSelector(
(state: State) => state.prefs.global?.[prefName] as GlobalPrefs[K],
Expand Down
6 changes: 6 additions & 0 deletions packages/desktop-electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ ipcMain.on('get-bootstrap-data', event => {
event.returnValue = payload;
});

ipcMain.handle('restart-server', () => {
serverProcess.kill();
serverProcess = null;
createBackgroundProcess();
});

ipcMain.handle('relaunch', () => {
app.relaunch();
app.exit();
Expand Down
4 changes: 4 additions & 0 deletions packages/desktop-electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ contextBridge.exposeInMainWorld('Actual', {
ipcRenderer.invoke('relaunch');
},

restartElectronServer: () => {
ipcRenderer.invoke('restart-server');
},

openFileDialog: (opts: OpenFileDialogPayload) => {
return ipcRenderer.invoke('open-file-dialog', opts);
},
Expand Down
6 changes: 5 additions & 1 deletion packages/loot-core/src/client/actions/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ export function loadGlobalPrefs() {
};
}

export function saveGlobalPrefs(prefs: prefs.GlobalPrefs) {
export function saveGlobalPrefs(
prefs: prefs.GlobalPrefs,
onSaveGlobalPrefs?: () => void,
) {
return async (dispatch: Dispatch) => {
await send('save-global-prefs', prefs);
dispatch({
type: constants.MERGE_GLOBAL_PREFS,
globalPrefs: prefs,
});
onSaveGlobalPrefs?.();
};
}
24 changes: 16 additions & 8 deletions packages/loot-core/src/platform/server/fetch/index.electron.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// @ts-strict-ignore
import nodeFetch from 'node-fetch';

export const fetch = (input: RequestInfo | URL, options?: RequestInit) => {
return nodeFetch(input, {
...options,
headers: {
...options?.headers,
origin: 'app://actual',
},
});
export const fetch = async (
input: RequestInfo | URL,
options?: RequestInit,
) => {
try {
return await nodeFetch(input, {
...options,
headers: {
...options?.headers,
origin: 'app://actual',
},
});
} catch (error) {
console.error(error); // log error
throw error;
}
};
1 change: 1 addition & 0 deletions packages/loot-core/typings/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare global {
opts: Parameters<import('electron').Dialog['showOpenDialogSync']>[0],
) => Promise<string[]>;
relaunch: () => void;
restartElectronServer: () => void;
};

__navigate?: import('react-router').NavigateFunction;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3431.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [MikesGlitch]
---

Restart server silently when adding self signed cert and add some logs

0 comments on commit 3743a32

Please sign in to comment.