Skip to content

Commit

Permalink
Fix: update toolbar server status on sync (#4075)
Browse files Browse the repository at this point in the history
* fix/#3128: listen to 'sync-event' to update user data when sync status changed, so 'userData.offline' is up to date and server status displayed is valid

* add upcoming release note

* fix: use of optional chaining for 'userData.offline' to fix typecheck errors

* fix: replace 'userData?.offline' by 'userDate' in useEffect's dependencies

* fix: add error handling for 'initializeUserData' method

* fix: remove optional chaining for 'userData.offline' and directly check if userData is not null

* fix: eslint warning "`loot-core/client/actions` import should occur before import of `loot-core/src/platform/client/fetch`"
  • Loading branch information
p-payet authored Jan 19, 2025
1 parent 1f2c654 commit cdbf3e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
34 changes: 31 additions & 3 deletions packages/desktop-client/src/components/LoggedInUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Trans, useTranslation } from 'react-i18next';
import { useLocation } from 'react-router-dom';

import { closeBudget, getUserData, signOut } from 'loot-core/client/actions';
import { listen } from 'loot-core/src/platform/client/fetch';
import { type RemoteFile, type SyncedLocalFile } from 'loot-core/types/file';
import { type TransObjectLiteral } from 'loot-core/types/util';

Expand Down Expand Up @@ -52,14 +53,41 @@ export function LoggedInUser({
const currentFile = remoteFiles.find(f => f.cloudFileId === cloudFileId);
const hasSyncedPrefs = useSelector(state => state.prefs.synced);

useEffect(() => {
async function init() {
const initializeUserData = async () => {
try {
await dispatch(getUserData());
} catch (error) {
console.error('Failed to initialize user data:', error);
} finally {
setLoading(false);
}
};

init().then(() => setLoading(false));
useEffect(() => {
initializeUserData();
}, []);

useEffect(() => {
return listen('sync-event', ({ type }) => {
if (type === 'start') {
setLoading(true);

return;
}

const shouldReinitialize =
userData &&
((type === 'success' && userData.offline) ||
(type === 'error' && !userData.offline));

if (shouldReinitialize) {
initializeUserData();
} else {
setLoading(false);
}
});
}, [userData]);

async function onCloseBudget() {
await dispatch(closeBudget());
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4075.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [p-payet]
---

Fix to ensure that the toolbar's server status updates correctly during synchronization

0 comments on commit cdbf3e0

Please sign in to comment.