Skip to content

A local-first personal finance app

[AI] Fix bug when linking accounts (#8006)

Mats Nilsson6b802b46a64a
diff --git a/packages/desktop-client/src/components/modals/SelectLinkedAccountsModal.tsx b/packages/desktop-client/src/components/modals/SelectLinkedAccountsModal.tsx
index 929778c1e9..088c2d8b0a 100644
--- a/packages/desktop-client/src/components/modals/SelectLinkedAccountsModal.tsx
+++ b/packages/desktop-client/src/components/modals/SelectLinkedAccountsModal.tsx
@@ -147,42 +147,54 @@ export function SelectLinkedAccountsModal({
const dispatch = useDispatch();
const { data: allAccounts = [] } = useAccounts();
const localAccounts = allAccounts.filter(a => a.closed === 0);
- const [draftLinkAccounts, setDraftLinkAccounts] = useState<
- Map<string, 'linking' | 'unlinking'>
- >(() => {
- const externalAccountIds = new Set(externalAccounts.map(a => a.account_id));
- const initial = new Map<string, 'linking' | 'unlinking'>();
+ const { initialDraftLinkAccounts, initiallyChosenAccounts } = useMemo(() => {
+ const externalAccountIds = new Set(
+ externalAccounts?.map(a => a.account_id) || [],
+ );
+ const initialDraftLinkAccounts = new Map<string, 'linking' | 'unlinking'>();
for (const acc of localAccounts) {
if (acc.account_id && externalAccountIds.has(acc.account_id)) {
- initial.set(acc.account_id, 'linking');
+ initialDraftLinkAccounts.set(acc.account_id, 'linking');
}
}
- return initial;
- });
- const [chosenAccounts, setChosenAccounts] = useState<Record<string, string>>(
- () => {
- const initiallyChosenAccounts = Object.fromEntries(
- localAccounts
- .filter(acc => acc.account_id)
- .map(acc => [acc.account_id, acc.id]),
+
+ const initiallyChosenAccounts = Object.fromEntries(
+ localAccounts
+ .filter(acc => acc.account_id)
+ .map(acc => [acc.account_id, acc.id]),
+ );
+
+ const preselectedExternalAccount =
+ propsWithSortedExternalAccounts.externalAccounts.find(
+ account => initiallyChosenAccounts[account.account_id] == null,
);
- const preselectedExternalAccount =
- propsWithSortedExternalAccounts.externalAccounts.find(
- account => initiallyChosenAccounts[account.account_id] == null,
- );
+ if (
+ upgradingAccountId &&
+ preselectedExternalAccount &&
+ !Object.values(initiallyChosenAccounts).includes(upgradingAccountId)
+ ) {
+ initiallyChosenAccounts[preselectedExternalAccount.account_id] =
+ upgradingAccountId;
+ initialDraftLinkAccounts.set(
+ preselectedExternalAccount.account_id,
+ 'linking',
+ );
+ }
- if (
- upgradingAccountId &&
- preselectedExternalAccount &&
- !Object.values(initiallyChosenAccounts).includes(upgradingAccountId)
- ) {
- initiallyChosenAccounts[preselectedExternalAccount.account_id] =
- upgradingAccountId;
- }
+ return { initialDraftLinkAccounts, initiallyChosenAccounts };
+ }, [
+ localAccounts,
+ externalAccounts,
+ propsWithSortedExternalAccounts.externalAccounts,
+ upgradingAccountId,
+ ]);
- return initiallyChosenAccounts;
- },
+ const [draftLinkAccounts, setDraftLinkAccounts] = useState<
+ Map<string, 'linking' | 'unlinking'>
+ >(initialDraftLinkAccounts);
+ const [chosenAccounts, setChosenAccounts] = useState<Record<string, string>>(
+ initiallyChosenAccounts,
);
const [customStartingDates, setCustomStartingDates] = useState<
Record<string, StartingBalanceInfo>
diff --git a/upcoming-release-notes/8006.md b/upcoming-release-notes/8006.md
new file mode 100644
index 0000000000..b0b6bb10fb
--- /dev/null
+++ b/upcoming-release-notes/8006.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [mnil]
+---
+
+Fixes a bug where selecting Link Account from the account page required reselecting the same account in the modal even though it’s prepopulated.