Skip to content

Commit

Permalink
Automatically reload when API call is redirected
Browse files Browse the repository at this point in the history
  • Loading branch information
TimQuelch committed Aug 23, 2024
1 parent 132fc34 commit d2ef2c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/desktop-client/src/global-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,8 @@ export function handleGlobalEvents(actions: BoundActions, store: Store<State>) {
actions.closeBudgetUI();
actions.setAppState({ loadingText: null });
});

listen('api-fetch-redirected', () => {
actions.reloadApp();
});
}
6 changes: 6 additions & 0 deletions packages/loot-core/src/client/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export function focused() {
return send('app-focused');
};
}

export function reloadApp() {
return () => {
global.Actual.reload();
};
}
18 changes: 17 additions & 1 deletion packages/loot-core/src/platform/server/fetch/index.web.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export const fetch = globalThis.fetch;
import * as connection from '../connection';

export const fetch = async (
input: RequestInfo | URL,
options?: RequestInit,
): Promise<Response> => {
const response = await globalThis.fetch(input, options);

const originalHost = new URL(input instanceof Request ? input.url : input)
.host;

if (response.redirected && new URL(response.url).host !== originalHost) {
connection.send('api-fetch-redirected');
}

return response;
};
1 change: 1 addition & 0 deletions packages/loot-core/src/types/server-events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface ServerEvents {
'start-load': unknown;
'sync-event': { type; subtype; meta; tables; syncDisabled };
'undo-event': UndoState;
'api-fetch-redirected': unknown;
}

0 comments on commit d2ef2c4

Please sign in to comment.