diff --git a/components/mail-transfer/__tests__/mail-import-dialog.test.tsx b/components/mail-transfer/__tests__/mail-import-dialog.test.tsx index c15a88713..f191fd56e 100644 --- a/components/mail-transfer/__tests__/mail-import-dialog.test.tsx +++ b/components/mail-transfer/__tests__/mail-import-dialog.test.tsx @@ -71,6 +71,17 @@ describe('MailImportDialog', () => { expect(startImport).toHaveBeenCalledWith(expect.objectContaining({ accountId: 'acc1', targetMailboxId: null, keepFolders: true })); }); + it('loads the folders when opened from Settings before the mail view did, then picks the Inbox', async () => { + const fetchMailboxes = vi.fn(async () => {}); + const loaded = useEmailStore.getState().mailboxes; + useEmailStore.setState({ mailboxes: [], fetchMailboxes } as never); + render(); + act(() => openMailImport()); + expect(fetchMailboxes).toHaveBeenCalled(); + act(() => useEmailStore.setState({ mailboxes: loaded } as never)); + await waitFor(() => expect((screen.getByTestId('mail-import-target') as HTMLSelectElement).value).toBe('inbox')); + }); + it('cannot start without files', () => { render(); act(() => openMailImport()); diff --git a/components/mail-transfer/mail-import-dialog.tsx b/components/mail-transfer/mail-import-dialog.tsx index 026fe1d00..37da46feb 100644 --- a/components/mail-transfer/mail-import-dialog.tsx +++ b/components/mail-transfer/mail-import-dialog.tsx @@ -72,6 +72,17 @@ export function MailImportDialog({ request }: { request: ImportRequest }) { const targetId = target === TOP ? null : targetMailbox ? targetMailbox.originalId || targetMailbox.id : null; const effectiveKeep = anyFolders && keepFolders; + // Opened from Settings, the mail view may not have loaded the folders yet. + useEffect(() => { + if (client && mailboxes.length === 0) void useEmailStore.getState().fetchMailboxes(client).catch(() => {}); + }, [client, mailboxes.length]); + + // Keep the choice valid when the folders arrive or the files change. + useEffect(() => { + const valid = target === TOP ? anyFolders : targets.some((x) => x.mailbox.id === target); + if (!valid && target !== initialTarget) setTarget(initialTarget); + }, [targets, target, anyFolders, initialTarget]); + // An earlier import of the same file into the same place that stopped part way. useEffect(() => { let current = true;