📧✨ Beautiful email. For everyone.
fix: fall back to an available compose account (#38)
Paul Koeckbfad4fe766ed
diff --git a/apps/desktop/src/renderer/src/App.tsx b/apps/desktop/src/renderer/src/App.tsxindex 3460bce..4763b9c 100644--- a/apps/desktop/src/renderer/src/App.tsx+++ b/apps/desktop/src/renderer/src/App.tsx@@ -99,6 +99,7 @@ import { emptyListState } from './empty-states'import { AccountMark } from './AccountMark'import { HintIconButton } from './HintIconButton'import { targetAccounts, targetLimit } from './account-limits'+import { newMessageAccount } from './compose-account'import { SubmissionList } from './SendingStatus'import { IndexingMeter } from './IndexingMeter'import { UpdateNotice } from './UpdateNotice'@@ -493,7 +494,9 @@ export function App() {kind: 'reply' | 'replyAll' | 'forward' = 'reply',partial: Partial<Draft> = {},) => {- const id = message?.accountId || route.account || settings.defaultAccountId || accounts[0]?.id+ const id =+ message?.accountId ||+ newMessageAccount(accounts, route.account, settings.defaultAccountId)?.idif (!id) {setSettingsTab('accounts')setSettingsOpen(true)diff --git a/apps/desktop/src/renderer/src/Settings.tsx b/apps/desktop/src/renderer/src/Settings.tsxindex a5656de..a3d64f8 100644--- a/apps/desktop/src/renderer/src/Settings.tsx+++ b/apps/desktop/src/renderer/src/Settings.tsx@@ -36,6 +36,7 @@ import { FolderMappingsDialog } from './FolderMappings'import { AliasesDialog } from './Aliases'import { IndexingMeter } from './IndexingMeter'import { ShortcutEditor } from './ShortcutEditor'+import { newMessageAccount } from './compose-account'/** A miniature of the app window in one theme. */function ThemePreview({ theme, className }: { theme: 'dark' | 'light'; className?: string }) {@@ -455,7 +456,11 @@ export function SettingsPanel({aria-label="Default sending account"className="setting-select w-auto min-w-60 shrink-0"value={- bootstrap.settings.defaultAccountId || bootstrap.accounts[0]?.id || ''+ newMessageAccount(+ bootstrap.accounts,+ undefined,+ bootstrap.settings.defaultAccountId,+ )?.id || ''}options={bootstrap.accounts.map((a) => ({value: a.id,diff --git a/apps/desktop/src/renderer/src/compose-account.ts b/apps/desktop/src/renderer/src/compose-account.tsnew file mode 100644index 0000000..e14a83b--- /dev/null+++ b/apps/desktop/src/renderer/src/compose-account.ts@@ -0,0 +1,14 @@+import type { Account } from '@inlark/core'++/** Remembered locations and defaults can refer to accounts that have since been removed. */+export function newMessageAccount(+ accounts: Account[],+ selectedAccountId?: string,+ defaultAccountId?: string,+): Account | undefined {+ return (+ accounts.find((a) => a.id === selectedAccountId) ||+ accounts.find((a) => a.id === defaultAccountId) ||+ accounts[0]+ )+}diff --git a/tests/compose-account.test.ts b/tests/compose-account.test.tsnew file mode 100644index 0000000..b0a3d10--- /dev/null+++ b/tests/compose-account.test.ts@@ -0,0 +1,44 @@+import { describe, expect, it } from 'vitest'+import type { Account } from '../packages/core/src'+import { newMessageAccount } from '../apps/desktop/src/renderer/src/compose-account'++describe('new message sending account', () => {+ const accounts: Account[] = ['personal', 'work'].map((id) => ({+ id,+ connectionId: id,+ remoteId: id,+ name: id,+ email: id + '@example.com',+ color: '#aaa',+ status: 'connected',+ }))++ it('prefers the selected account over the default sending account', () => {+ expect(newMessageAccount(accounts, 'work', 'personal')).toBe(accounts[1])+ })++ it('uses the default sending account in the unified inbox', () => {+ expect(newMessageAccount(accounts, undefined, 'work')).toBe(accounts[1])+ })++ it('uses the default when the remembered location names a removed account', () => {+ expect(newMessageAccount(accounts, 'removed', 'work')).toBe(accounts[1])+ })++ it('uses the first account when the default sending account was removed', () => {+ expect(newMessageAccount(accounts, undefined, 'removed')).toBe(accounts[0])+ })++ it('uses the first account when both remembered accounts were removed', () => {+ expect(newMessageAccount(accounts, 'removed', 'also-removed')).toBe(accounts[0])+ })++ it('keeps an existing selected account even when it needs reconnecting', () => {+ const offline: Account = { ...accounts[1], status: 'offline' }+ expect(newMessageAccount([accounts[0], offline], 'work', 'personal')).toBe(offline)+ })++ it('returns no account when setup is needed, even with stale preferences', () => {+ expect(newMessageAccount([], 'removed', 'also-removed')).toBeUndefined()+ })+})