fix(mobile): apply saved home filters before the first render

Merged
SA
3mo ago
+53-4

What was wrong

Not a missing-persistence bug — workspacesFilterStore and newSessionPreferencesStore were already wrapped in zustand persist over AsyncStorage, nothing resets them programmatically, and the values are written and do come back.

It's lead #1: the async rehydration window. Instrumented on the simulator, a cold start looks like this:

[dbg] workspacesFilterStore module eval  +0ms
[dbg] rehydrate start                    +3ms
[dbg] HomeScreen render  {"projectFilter":null,  "selectedProjectId":null}   <- default
[dbg] HomeScreen render  {"projectFilter":null,  "selectedProjectId":null}   <- default
[dbg] raw workspaces-filter on disk:   +163ms  {"projectFilter":"…","hostFilter":"…","sort":"createdAt"}
[dbg] rehydrate done                   +164ms
[dbg] HomeScreen render  {"projectFilter":"…", "selectedProjectId":"…"}      <- correct

For those ~165 ms every consumer reads the defaults, so the home screen resolves the first online host instead of the saved one, lists that host's most recently updated project instead of the filtered one, and fires the workspace / terminal / project queries against it before swapping. That is what reads as "the filters reset on restart". Whether the window is even visible depends on whether the auth session or AsyncStorage resolves first, which varies run to run.

Lead #2 (a stale projects/hosts list invalidating the saved id) was ruled out: nothing validates or clears the persisted id, and the only callers of the setters are the three filter screens.

newSessionPreferencesStore has the identical bug — the composer picks a default target over the last used one in the same window, and a send landing there would create the workspace on the wrong project. Fixed too.

The fix

Both stores expose hasHydrated, flipped from onRehydrateStorage — including on read errors, so a failed read falls back to the defaults instead of hanging. It's excluded from partialize so it never reaches disk. Consumers wait on it:

  • useSelectedHost returns null (all four call sites already handle null)
  • HomeScreen holds its project pick and suppresses the empty state
  • useNewChatTargets holds its default target until both stores have answered

Verification

On the iOS simulator, against RCTAsyncLocalStorage_V1/manifest.json and the Metro console:

  • Set sort to "Date created" → written to disk
  • xcrun simctl terminate booted sh.superset.mobile, relaunch
  • Sort screen still shows "Date created" checked; hasHydrated flips with the saved values intact and never appears on disk

Caveat: this simulator has no hosts/projects/workspaces (the worktree's local stack isn't running), so the project/host filter rows couldn't be visually confirmed against real data — those were verified at the store level (persisted values rehydrate and reach HomeScreen's derived selectedProjectId) plus the disk manifest.

bun run lint:fix and bun run typecheck clean in apps/mobile (after build:types in packages/host-service). Root scripts/lint.sh reports 2 errors, both in untouched apps/desktop files inherited from main; all four guard scripts pass.

No tests added, per repo preference.


Summary by cubic

Apply saved home filters and composer preferences before the first render to remove the cold-start reset flash and prevent queries against the wrong host. Previously consumers read defaults until storage rehydrated (~165 ms) and resolved the first online host/project; now consumers wait for hydration.

  • Add hasHydrated to the workspaces filter store and the new-session preferences store; flip it in onRehydrateStorage (also on read errors) and exclude it from persistence.
  • HomeScreen defers selectedProjectId and suppresses the empty state until filters hydrate.
  • useSelectedHost returns null until the filter store hydrates.
  • useNewChatTargets defers defaultTarget until both stores hydrate to avoid creating a workspace on the wrong project.

Summary by CodeRabbit

  • Bug Fixes
    • Improved home screen loading to prevent incorrect projects, workspaces, or hosts from appearing before saved preferences are restored.
    • Delayed empty-workspace messaging until workspace data and filters are fully ready.
    • Ensured new chats use the correct default target after preferences finish loading.
    • Improved handling of persisted filter settings when storage restoration encounters an error.