Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:electron: Restart server silently when adding self signed cert and add some logs #3431

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@MikesGlitch MikesGlitch Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds a bit extra logging - this will help with cert/network/other errors

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
Loading