fix(useDataLoader): cancel in-flight section fetches when company changes (P2-45)

Add currentCompanyId to effect deps so switching company aborts the
previous company's pending inbox/metrics/standup fetches before stale
results can overwrite new state.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 22:58:39 -04:00
1 parent 83462a4663
commit 67035d1a8e
1 file changed
+7 -5
+7 -5
View File
@@ -36,10 +36,12 @@ export function useDataLoader(
}
}, [selectedConnectorId, selectedRunId]);
// Section-based data loading
// Section-based data loading; resets when section OR company changes so
// in-flight requests for the previous company cannot overwrite new state.
const currentCompanyId = snapshot?.currentCompanyId ?? null;
useEffect(() => {
if (!snapshot?.currentCompanyId) return;
const companyId = snapshot.currentCompanyId;
if (!currentCompanyId) return;
const companyId = currentCompanyId;
let cancelled = false;
const store = useAppStore.getState();
@@ -62,13 +64,13 @@ export function useDataLoader(
}
if (section === "social") {
store.setSocialAccounts(snapshot.socialAccounts?.filter((a) => a.companyId === companyId) ?? []);
store.setSocialAccounts(snapshot?.socialAccounts?.filter((a) => a.companyId === companyId) ?? []);
}
return () => {
cancelled = true;
};
}, [section, snapshot]);
}, [section, currentCompanyId, snapshot]);
return { refresh };
}