Skip to content

Commit

Permalink
Add advanced setting to reload app from server
Browse files Browse the repository at this point in the history
  • Loading branch information
TimQuelch committed Aug 18, 2024
1 parent af5fd5b commit d7e5b6a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
16 changes: 16 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,22 @@ global.Actual = {
window.location.reload();
},

reload: () => {
if (window.navigator.serviceWorker == null) return;

// Unregister the service worker handling routing and then reload. This should force the reload
// to query the actual server rather than delegating to the worker
return window.navigator.serviceWorker
.getRegistration('/')
.then(registration => {
if (registration == null) return;
return registration.unregister();
})
.then(() => {
window.location.reload();
});
},

openFileDialog: async ({ filters = [] }) => {
return new Promise(resolve => {
let createdElement = false;
Expand Down
48 changes: 48 additions & 0 deletions packages/desktop-client/src/components/settings/ReloadApp.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useState, useCallback } from 'react';

import { ButtonWithLoading } from '../common/Button2';
import { Text } from '../common/Text';

import { Setting } from './UI';

export function ReloadApp() {
const [reloading, setReloading] = useState(false);

const reload: undefined | (() => Promise<void>) = window.Actual?.reload;
const isReloadable = reload != null;

const onReloadApp = useCallback(async () => {
setReloading(true);
if (reload) {
await reload();
}
setReloading(false);
}, [setReloading, reload]);

return (
<Setting
primaryAction={
<ButtonWithLoading
isLoading={reloading}
isDisabled={!isReloadable}
onPress={onReloadApp}
>
Reload application
</ButtonWithLoading>
}
>
{isReloadable ? (
<Text>
<strong>Reload application</strong> will force the application to
reload from the server. This is done automatically when there are new
versions available, however forcing the application to reload may
resolve issues with redirects with custom authentication
</Text>
) : (
<Text>
<strong>Reload application</strong> is only available on browser
</Text>
)}
</Setting>
);
}
2 changes: 2 additions & 0 deletions packages/desktop-client/src/components/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { ExportBudget } from './Export';
import { FixSplits } from './FixSplits';
import { FormatSettings } from './Format';
import { GlobalSettings } from './Global';
import { ReloadApp } from './ReloadApp';
import { ResetCache, ResetSync } from './Reset';
import { ThemeSettings } from './Themes';
import { AdvancedToggle, Setting } from './UI';
Expand Down Expand Up @@ -184,6 +185,7 @@ export function Settings() {
<ResetCache />
<ResetSync />
<FixSplits />
<ReloadApp />
<ExperimentalFeatures />
</AdvancedToggle>
</View>
Expand Down
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;
reload: (() => Promise<void>) | undefined;
};

__navigate?: import('react-router').NavigateFunction;
Expand Down

0 comments on commit d7e5b6a

Please sign in to comment.