diff --git a/stores/__tests__/email-store-multi-account.test.ts b/stores/__tests__/email-store-multi-account.test.ts index 02a5cacbb..0f83bd241 100644 --- a/stores/__tests__/email-store-multi-account.test.ts +++ b/stores/__tests__/email-store-multi-account.test.ts @@ -105,6 +105,19 @@ describe('useEmailStore multi-account state', () => { expect(state.expandedThreadIds.size).toBe(0); }); + it('selectAccountMailbox drops a search scope belonging to the account being left', () => { + // The scope names a folder of the previous account and resolves against + // resolveActionMailboxes(), which now returns the new account's list. Kept, + // it would query that foreign folder id against the wrong account and come + // back empty, while the dropdown has no matching option and reads "All + // folders". (#1082) + useEmailStore.setState({ searchMailboxId: 'a-archive' }); + + useEmailStore.getState().selectAccountMailbox('account-b', 'b-inbox'); + + expect(useEmailStore.getState().searchMailboxId).toBe(''); + }); + it('selectAccountMailbox with null accountId switches back to the active account', () => { useEmailStore.getState().selectAccountMailbox('account-b', 'b-inbox'); expect(useEmailStore.getState().viewingAccountId).toBe('account-b'); diff --git a/stores/email-store.ts b/stores/email-store.ts index 0479355c6..66cd903e3 100644 --- a/stores/email-store.ts +++ b/stores/email-store.ts @@ -1436,6 +1436,13 @@ export const useEmailStore = create((set, get) => ({ selectAccountMailbox: (accountId, mailboxId) => set({ viewingAccountId: accountId, selectedMailbox: mailboxId, + // The search panel's folder scope names a folder of the account we are + // leaving. It resolves against `resolveActionMailboxes()`, which now + // returns the NEW account's list, so the id no longer matches: the search + // would query that foreign folder id against the wrong account and come + // back empty, while the dropdown (which renders no matching option) reads + // "All folders". Reset it so the scope matches what is shown. (#1082) + searchMailboxId: "", isLoadingMore: false, selectedEmail: null, selectedEmailIds: new Set(),