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

Fix: update toolbar server status on sync #4075

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
33 changes: 30 additions & 3 deletions packages/desktop-client/src/components/LoggedInUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useLocation } from 'react-router-dom';

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

import { useAuth } from '../auth/AuthProvider';
Expand Down Expand Up @@ -52,14 +53,40 @@ export function LoggedInUser({
const currentFile = remoteFiles.find(f => f.cloudFileId === cloudFileId);
const hasSyncedPrefs = useSelector((state: State) => state.prefs.synced);

useEffect(() => {
async function init() {
const initializeUserData = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be defined inside the useEffect hook

Copy link
Author

Choose a reason for hiding this comment

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

There is now 2 different useEffect. The first one is calling initializeUserData after first rendering, and the second one is used to listen to sync-event and call initializeUserData when needed, and is dependant to userData.

Both are using the initializeUserData method, it is for this reason that initializeUserData is defined outside the useEffect hooks.

Can you explain in more detail how initializeUserData definition should be done?

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 =
(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
Loading