fix(identity): sync default sender identity per account (#507)

The default sender identity (`preferredPrimaryId`) lived only in the
browser-local `identity-storage` Zustand store and was never written to
the server-side synced settings. As a result the choice was lost when
clearing site data or switching browsers, and never appeared in the
exported settings JSON.

Persist the default identity in the synced settings store, keyed per
account (`preferredIdentityIds: Record<accountId, identityId>`), mirroring
the existing per-account `allMailFolderIds`. Per-account keying is required
because JMAP identity ids are account-scoped and would otherwise collide
across accounts / the unified mailbox.

- settings-store: add `preferredIdentityIds` to state, defaults, export
  (so it shows in exported JSON), import (with a non-record guard),
  rehydrate coercion, and a v6->v7 migration.
- auth-store: add `applyPreferredIdentity()`, invoked in every
  `loadFromServer().finally()` (login / OAuth / SSO / switch / restore) so
  the synced default reorders the active account's identities once server
  settings load (the composer defaults From to identities[0]).
- identity-manager-modal: the star action also writes the choice to the
  synced per-account map, triggering server sync + export inclusion.
- identity-store: keep `preferredPrimaryId` in local persist as a sync-off
  fallback; synced settings are the durable cross-device source of truth.
- tests: per-account independence, export/import round-trip, non-record
  import guard, and v6->v7 migration.
This commit is contained in:
Stefan Hildebrandt
2026-07-11 21:14:56 +02:00
parent 2e42693228
commit 034f7a4b9b
5 changed files with 154 additions and 10 deletions
+16 -3
View File
@@ -262,6 +262,12 @@ interface SettingsState {
// -> defaults to inbox + custom folders; an explicit [] = "no own folders".
allMailFolderIds: Record<string, string[]>;
// Per-account default sender identity, keyed by AccountEntry.id -> JMAP
// Identity id. Synced (and exported) so the chosen default survives clearing
// site data and follows the user across browsers/devices (issue #507). Kept
// per account because JMAP identity ids are account-scoped and would collide.
preferredIdentityIds: Record<string, string>;
// Email Display
disableThreading: boolean; // Show emails as individual messages instead of grouped by conversation
@@ -455,6 +461,7 @@ const DEFAULT_SETTINGS = {
unifiedCrossAccount: false,
allMailFolderIds: {} as Record<string, string[]>,
preferredIdentityIds: {} as Record<string, string>,
enableCrossUnreadView: false,
enableCrossStarredView: false,
@@ -640,6 +647,7 @@ export const useSettingsStore = create<SettingsState>()(
includeGroupInUnified: state.includeGroupInUnified,
unifiedCrossAccount: state.unifiedCrossAccount,
allMailFolderIds: state.allMailFolderIds,
preferredIdentityIds: state.preferredIdentityIds,
enableCrossUnreadView: state.enableCrossUnreadView,
enableCrossStarredView: state.enableCrossStarredView,
enableCrossAllView: state.enableCrossAllView,
@@ -697,8 +705,8 @@ export const useSettingsStore = create<SettingsState>()(
if (key === 'allMailFolderIds' && !isPlainRecord(settings[key])) {
return;
}
// Defensive: a non-record (e.g. a legacy scalar) would break the
// per-account map lookups - ignore it.
// Per-account map (accountId -> identityId); ignore any legacy
// global/non-record value rather than corrupting the map.
if (key === 'preferredIdentityIds' && !isPlainRecord(settings[key])) {
return;
}
@@ -900,7 +908,7 @@ export const useSettingsStore = create<SettingsState>()(
}),
{
name: 'settings-storage',
version: 6,
version: 7,
migrate: migrateSettings,
onRehydrateStorage: () => {
return (state) => {
@@ -979,6 +987,11 @@ export function migrateSettings(persisted: unknown, version: number): SettingsSt
// configuration (matches the new-install default).
state.includeGroupInUnified = true;
}
// v7: introduced the per-account default-identity map (issue #507).
// Coerce any missing/legacy value to an empty record.
if (version < 7 || !isPlainRecord(state.preferredIdentityIds)) {
state.preferredIdentityIds = {};
}
return state as unknown as SettingsState;
}