Client state and preferences
The frontend state model coordinates the current application flow, the active vault, workspace changes, backend events, and the surfaces shown to the user. It separates data that must survive a lock from data that must be removed with the unlocked vault. User preferences follow a third lifetime: they are durable settings owned by the app and reflected in the frontend. This page complements Application shell, Frontend bootstrap, and Main workspace interface.
State model
The stores are grouped by lifetime rather than by screen. src/…/index.ts exposes the shared state and actions, while the Rust backend remains the source of truth for the vault, session, sync, and durable settings.
State lifetimes
The state model uses three boundaries:
| Lifetime | Main state | Purpose |
|---|---|---|
| Durable | Preferences | Keep appearance, list, security, region, and generator choices across launches and locks. |
| Process | App state | Keep the current flow, backend status, sync status, update status, and pending file handoff while the app runs. |
| Unlocked session | Vault and UI state | Show the active vault and its open views without allowing data or live secret-bearing surfaces to cross a lock. |
This split keeps a lock useful as a security boundary. A new unlock does not inherit the previous vault's rows, selection, filters, dialogs, or share state, while the user does not lose preferences or the app's knowledge of its current flow.
References: src/…/index.ts• L1-20 src/…/app.ts• L239-253 src/…/vault.ts• L17-22 src/…/ui.ts• L7-12 src/…/prefs.ts• L21-29
Store relationships
The stores form a small coordination layer around backend answers. App status describes the active workspace and platform capabilities. Vault actions update the visible entry metadata and request backend changes. UI actions change the current surface or filter. Workspace and Drive actions use app state to represent transitions that can replace the active vault.
The main rule is that components do not need to copy these facts into local state. They read a store selector and dispatch a shared action from a component, shortcut, event handler, or test. This keeps one transition path for actions such as locking, switching workspaces, saving an entry, or opening Settings.
The following flow shows the broad direction of state ownership and updates.
flowchart LR
Backend[Backend and OS] -->|status and events| App[App state]
App --> Workspace[Workspace selectors]
App -->|unlock result| Vault[Vault state]
Vault --> UI[UI state]
UI --> Shell[Visible shell]
Prefs[Persisted preferences] -->|hydration and updates| PrefStore[Preferences state]
PrefStore --> Shell
Vault -->|writes and sync requests| Backend
UI -->|user actions| Vault
References: src/…/index.ts• L11-20 src/…/app.ts• L157-160 src/…/workspace.ts• L23-28 src/…/vault.ts• L107-155 src/…/ui.ts• L148-188
Application and session state
The app, vault, workspace, Drive, and event layers cooperate to move the user between setup, authentication, and the unlocked workspace. Each layer owns a distinct kind of fact so that a backend event can update the right part of the screen without reconstructing the whole shell.
App state
The app store represents process-wide flow and backend status. It holds the current setup, auth, or main flow, the last application probe, sync status, first-run Drive discovery, remote vaults, an OS-opened backup, and staged update information. It also records when a workspace switch is in progress so the lock screen does not act on a stale workspace answer.
The application probe is the common snapshot for facts needed by more than one screen. It supplies the active workspace, workspace list, biometric gate, scan capability, locale, preferences, and sync state. Later sync events can replace the sync part, while workspace changes re-probe the full status so related fields change together.
References: src/…/app.ts• L19-107 src/…/app.ts• L116-160 src/…/app.ts• L51-100
Vault state
The vault store is the unlocked view of the active password vault. It holds entry metadata, archive rows, the selected entry, editor or new-entry state, scan prefill data, and audit results. It turns backend entry changes into visible list changes and keeps selection aligned with filters and incoming merges.
Entry writes, deletes, restores, purges, and favorite changes update the visible state and request a later sync when the vault is connected. The store also starts an audit after changes that can affect entry health. The archive is loaded when needed because deleted rows are not part of the normal unlock payload.
References: src/…/vault.ts• L23-49 src/…/vault.ts• L60-103 src/…/vault.ts• L107-124 src/…/vault.ts• L143-208
Workspace state
Workspaces identify separate vaults that can be opened from one app install. They are derived from app status rather than kept in a second workspace store. This keeps the workspace list, active workspace, biometric gate, and other probe data tied to one backend snapshot.
Switching a workspace is a session boundary. The current vault and UI state are cleared first when the next workspace opens, then the new unlock result fills the vault and a fresh probe updates the active workspace. A workspace that is not yet open on the device can instead lead to its lock screen. Create and restore operations use the same landing path because each opens a new active vault.
References: src/…/workspace.ts• L23-45 src/…/workspace.ts• L55-71 src/…/workspace.ts• L96-110 src/…/workspace.ts• L113-166
Backend event coordination
src/…/events.ts is the event bridge between Rust and the stores. It converts sync status, vault merges, Drive setup results, remote workspace discovery, workspace changes, locks, and opened files into the shared actions that update the shell.
The lock event is the key common boundary. It clears the vault and UI session, resets related cached state, and routes the app to authentication regardless of whether the lock came from the user, inactivity, the tray, or a workspace transition. This prevents each lock source from implementing a different cleanup path.
References: src/…/events.ts• L18-35 src/…/events.ts• L37-70 src/…/app.ts• L247-262
Drive setup state
Drive setup state describes account consent and vault discovery before a vault exists or while another account vault is being restored. It distinguishes waiting for consent, finding restore candidates, an empty account, an error, and an active restore. The same state supports first-run setup and adding another workspace from an account.
The Drive actions start the account flow and map immediate command failures into the same error state used by backend events. A restore keeps the selected candidate and holds the relevant Settings surface until the backend has either opened the new workspace or returned control to the user.
References: src/…/app.ts• L31-49 src/…/app.ts• L339-385 src/…/drive.ts• L7-14 src/…/drive.ts• L19-52
UI state and layout
UI state describes how the current unlocked data is presented. It does not replace vault data or durable preferences. Instead, it records the active list view, filters, selected overlays, settings navigation, transient feedback, and in-progress visual actions that the shell needs to coordinate.
Views and filters
The UI store chooses among All Items, Favorites, Health, Archive, and Tags. It also carries the entry type, tag, and text filters that narrow the list. Entering a new view clears the current selection and loads Archive data when required. Changing a filter drops a selection only when the selected entry would no longer be visible.
This relationship keeps the detail pane and list honest. A save that changes an entry so it no longer matches the current filter leaves the filtered view rather than hiding the saved result behind an unchanged selection.
References: src/…/ui.ts• L14-19 src/…/ui.ts• L148-188 src/…/vault.ts• L143-155
Overlays and transient feedback
The UI store coordinates Settings, the command palette, add-entry and generator surfaces, sharing dialogs, scan status, search focus, copy feedback, notices, and unfinished share cleanup. Dialog presence is tracked separately so keyboard shortcuts and file drops can stand down whenever any modal surface owns the window.
Settings can be locked to its current section while a workspace restore or another backend operation is in flight. This prevents navigation from looking like cancellation when the backend will still complete the operation. These controls are session state and are reset with the vault on lock.
References: src/…/ui.ts• L34-89 src/…/ui.ts• L190-243 src/…/ui.ts• L245-276
Responsive layout
The layout hook selects the shell from the window width. A compact layout serves phone-sized windows with one screen at a time and a bottom tab bar. A wide layout serves desktop and tablet windows with the list and detail areas visible together.
The layout is derived from the media query, not stored in a preference. Both shells read the same app, vault, and UI state, so a resize changes the presentation without changing the selected entry, view, or open work. In compact mode, the current store state determines whether the user sees a draft, detail, Settings, a generator, or a tab root.
The following flow shows how the same state model selects a presentation without adding a second navigation model.
flowchart TD
State[App, vault, and UI state] --> Width{Window width}
Width -->|767px or less| Compact[Compact shell]
Width -->|768px or more| Wide[Wide shell]
Compact --> Roots[Tab roots and pushed screens]
Wide --> Panes[Rail, list, and detail panes]
State --> Overlays[Shared overlays and feedback]
References: src/…/useLayout.ts• L1-30 src/…/index.tsx• L18-51 src/…/index.tsx• L11-27 src/…/index.tsx• L28-73 src/…/Wide.tsx• L6-21
Persisted preferences
Preferences control behavior that should remain after a lock and restart. Rust owns the durable settings file, while the frontend keeps a reactive copy so Settings and the rest of the app use the same current values.
Preference categories
The preference set covers user-facing behavior rather than vault content:
| Category | User effect |
|---|---|
| Appearance | Light, dark, or system theme and the accent color. |
| List and region | Entry sort order, date format, and language choice. |
| Security behavior | Auto-lock delay, clipboard clearing delay, and optional breach checks. |
| Password generation | Default length, character groups, and excluded characters for new passwords. |
The backend also enforces safe bounds for time values. A zero clipboard delay means that the copied value is not cleared by a timer, while an invalid or excessive auto-lock value is normalized before it is used.
References: src/…/app.ts• L7-40 src/…/prefs.ts• L32-55 src-tauri/…/settings.rs• L20-30 src-tauri/…/settings.rs• L63-100 src-tauri/…/settings.rs• L104-119
Preference lifecycle
At startup, the frontend receives the settings needed for the first frame and then adopts the full application status. The preference store validates the values before exposing them. A user change updates the store at once, sends a partial change to the backend, and accepts the backend's merged result when it returns.
The update path protects against two common races: an older write must not undo a newer one, and a status probe that started before a preference change must not restore stale settings. A failed write leaves the new value active for the current session, but it cannot promise that the value will survive a restart.
Older local storage keys are imported once into the durable settings file. Valid values are carried forward, then the old keys are removed only after the write succeeds.
References: src/boot.ts• L44-70 src/boot.ts• L73-114 src/…/prefs.ts• L63-90 src/…/prefs.ts• L95-172 src/…/legacyPrefs.ts• L5-20 src/…/legacyPrefs.ts• L163-179
Theme and language behavior
Theme and accent preferences have immediate effects outside React state. The frontend applies them to document root attributes, which lets the visual shell update without waiting for a remount. A system theme follows later operating system appearance changes when the platform exposes that signal.
Language changes go through the translation system and become stored preferences after initialization. A null language preference means the app follows the operating system. Date formatting remains a separate preference, so language choice and the shape of dates can change independently.
References: src/…/index.ts• L3-5 src/…/index.ts• L29-46 src/…/prefs.ts• L181-210 src/…/ThemePicker.tsx• L17-25 src/…/Language.tsx• L62-95
State transition boundaries
The important state changes are lock, unlock, workspace replacement, and backend merge. Each transition has a clear owner so that the visible shell does not show data from the wrong vault or claim that a request finished before the backend confirms it.
Lock and unlock flow
Locking removes unlocked session data and returns the shell to authentication. Unlocking supplies the entry list and sync configuration, then opens the main flow and starts the first sync when the vault is configured for sync. Preferences and process-level status remain available across this boundary.
sequenceDiagram
participant User
participant Backend
participant Events
participant Stores
participant Shell
User->>Backend: Lock or inactivity timeout
Backend-->>Events: vault:locked
Events->>Stores: Clear vault and UI session
Events->>Shell: Show authentication flow
User->>Backend: Unlock active workspace
Backend-->>Stores: Entries and sync configuration
Stores->>Shell: Show main workspace
Stores->>Backend: Start sync when configured
References: src/…/events.ts• L59-65 src/…/app.ts• L239-279 src/…/vault.ts• L210
Workspace replacement flow
Workspace switching changes the database behind the main shell. The transition blocks a second switch, clears the old session when the new vault opens, and refreshes the shared application probe. If the target needs its password, the same lock event path protects the old data and presents the target's lock screen.
Workspace creation and restoration also clear the old session before entering the new workspace. This makes all ways to replace the active vault obey the same data boundary.
References: src/…/workspace.ts• L38-71 src/…/workspace.ts• L96-110 src/…/workspace.ts• L113-166 src/…/app.ts• L264-279
Sync and merge flow
Sync state is held in the app store and entry merges are applied to the vault store. A sync status event updates the status used by all flows. A merge replaces the visible entry metadata, reruns the audit, and refreshes an open Archive so that rows removed or restored on another device do not remain hidden from the current view.
Local writes use a short delay before requesting a whole-vault sync. This groups a burst of completed edits and avoids publishing the same snapshot for each individual save. A lock cancels a pending request because the next unlock performs a fresh sync.
References: src/…/events.ts• L18-24 src/…/events.ts• L39-47 src/…/app.ts• L301-337 src/…/vault.ts• L143-155
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the lifetime boundary aligned across
clearSession,resetVault, andresetUi. A new secret-bearing or vault-specific field must not survive a lock by accident. - When a backend event payload changes, update the event bridge and the store action together. Merge events must continue to refresh both the visible list and any open Archive or audit state.
- When application status changes, preserve one source for workspace identity, biometric availability, scan support, sync state, and preferences. Do not add a second snapshot that can become stale.
- When adding a preference, update the backend settings shape, frontend validation, default values, boot hydration, and the optimistic-write race rules as one contract.
- When changing navigation or adding an overlay, check both compact and wide shells. Compact navigation is derived from store state and modal presence also controls keyboard and drop behavior.
- When changing workspace replacement, preserve the order of clearing the old session, entering the new vault, and probing the new status.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A lock path clears only the vault rows but not UI state | The next user can see a prior selection, filter, dialog, or live share surface. | The single vault:locked reaction calls clearSession, which resets both vault and UI state. |
| A delayed application probe returns after a newer preference write | Old theme, accent, or other settings can replace the user's current choice. | Preference writes are stamped and probe hydration is accepted only when no newer write is pending. |
| A workspace switch races with another switch or an unlock attempt | The shell can show one workspace while the backend opens another. | switching blocks a second switch and the lock screen waits for the replacement probe. |
| A vault merge changes the currently displayed Archive | The Archive can show stale tombstones until the user leaves and returns. | An open Archive is refreshed when a merge event arrives. |
| A save removes the current entry from its active filter | The saved entry can disappear while the detail pane still shows it. | Save actions drop the incompatible filter or return to All Items and update selection together. |
| A settings file contains an invalid timeout or enum value | The UI and backend timer can disagree, or a timer can receive an unsafe value. | Rust normalizes time values and the frontend sanitizes enum and preference fields on hydration. |
Key files
| File | Why to read it |
|---|---|
| src/…/index.ts | Defines the lifetime-based store model and the public state surface. |
| src/…/app.ts | Shows flow selection, lock boundaries, sync status, Drive setup, and update state. |
| src/…/vault.ts | Shows how unlocked entries, selection, edits, archive data, audits, and sync-triggering writes relate. |
| src/…/ui.ts | Shows the list views, filters, overlays, Settings navigation, and transient feedback model. |
| src/…/workspace.ts | Shows how workspace switching, creation, deletion, and restore replace the active session. |
| src/…/events.ts | Shows how backend events enter the frontend state model. |
| src/…/prefs.ts | Shows preference defaults, validation, persistence, and race-safe hydration. |
| src/…/useLayout.ts | Defines the compact and wide layout boundary. |
| src/…/index.tsx | Shows how shared overlays use the selected layout and shared state. |
| src/…/index.tsx | Shows compact navigation as a projection of store state. |
| src/boot.ts | Shows first-frame preference hydration, application probing, and legacy preference adoption. |
| src-tauri/…/settings.rs | Defines durable preference ownership, defaults, normalization, and persistence. |
| src/…/app.ts | Defines the application status and preference contract exchanged with the backend. |