Import src/client/store-core.ts

This commit is contained in:
QQ 2026-11-11 22:32:41 +08:00
parent 648b47b5b6
commit 5c7a95689f
1 changed files with 44 additions and 0 deletions

44
src/client/store-core.ts Normal file
View File

@ -0,0 +1,44 @@
/**
* Pure expansion-state transforms for the workspace-groups store. Kept free
* of any runtime import so both the store (which binds them via defineStore)
* and unit tests (which exercise them directly) share one implementation.
*
* Expansion semantics follow the official ui-workspace store: collapse
* WRITES `false` (never deletes the key). The browser's auto-expand guard
* uses `Object.hasOwn` — a present `false` means "user deliberately
* collapsed this" and must NOT be re-expanded; only a fully absent key means
* "never touched" and may auto-expand. (Earlier versions deleted the key on
* collapse, which made the current category/workspace impossible to fold.)
*/
/** Workspace-groups browser viewing state persisted across surface remounts and reloads. */
export interface GroupsViewState {
/** Category folder expansion keyed by category label (absent = never touched). */
categoryExpansion: Record<string, boolean>
/** Workspace folder expansion keyed by workspace id (absent = never touched). */
workspaceExpansion: Record<string, boolean>
}
/** Collapse writes `false` (key retained); expand writes `true`. */
export function setCategoryExpandedImpl(state: GroupsViewState, key: string, expanded: boolean): void {
state.categoryExpansion[key] = expanded
}
/** Collapse writes `false` (key retained); expand writes `true`. */
export function setWorkspaceExpandedImpl(state: GroupsViewState, key: string, expanded: boolean): void {
state.workspaceExpansion[key] = expanded
}
/** Drop expansion keys that no longer exist (renames/deletes/config edits). */
export function retainKeysImpl(
state: GroupsViewState,
categoryKeys: readonly string[],
workspaceKeys: readonly string[],
): void {
state.categoryExpansion = Object.fromEntries(
Object.entries(state.categoryExpansion).filter(([key]) => categoryKeys.includes(key)),
)
state.workspaceExpansion = Object.fromEntries(
Object.entries(state.workspaceExpansion).filter(([key]) => workspaceKeys.includes(key)),
)
}