Scrum4Me/stores/product-workspace/selectors.ts
Madhura68 48d1e11a2a feat(PBI-74): product-workspace store skelet + test-infra (Story 1)
Skelet voor de nieuwe `product-workspace-store` die op termijn de gefragmenteerde
`backlog-store`/`planner-store`/`selection-store`/`product-store` vervangt. Deze
PR levert alleen het skelet + tests; UI-consumers worden in latere stories
omgezet.

- vitest naar jsdom + tests/setup.ts (MemoryStorage, default fetch-stub) — G6/G8
- stores/product-workspace/{types,store,selectors,restore}.ts — immer-middleware,
  alle slices en acties (hydrate, setActive*, ensure*Loaded met activeRequestId-
  guard, applyRealtimeEvent, resyncActiveScopes/loadedScopes, optimistic
  mutations). Restore-wiring in setters volgt in Story 4 (T-857/T-858).
- selectors gebruiken module-level EMPTY refs (G1) en documenteren useShallow-
  vereiste (G2)
- 34 nieuwe unit-tests dekken §Testing setup-checklist uit het ontwerp:
  hydrateSnapshot, selection-cascade, applyRealtimeEvent (I/U/D + parent-move +
  ander-product + unknown-entity → resync), delete-cleanup, race-safe loaders,
  ensureTaskLoaded _detail-flag, resyncActiveScopes ensure-keten, restore-hints
  read/write/clear, optimistic mutation rollback/settle/SSE-echo idempotent
- docs/api/rest-contract.md: audit-sectie met de vier ontbrekende
  ensure*Loaded-endpoints (worden toegevoegd in Story 7 / T-870)

Refs: PBI-74, ST-1318, T-837..T-843
Bron-ontwerp: docs/plans/zustand-store-rearchitecture.md
Implementatieplan: docs/plans/zustand-workspace-store-implementation.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 00:55:22 +02:00

102 lines
3.1 KiB
TypeScript

import type { ProductWorkspaceStore } from './store'
import type { BacklogPbi, BacklogStory, BacklogTask, TaskDetail } from './types'
// G1: stable EMPTY-references zodat selectors geen nieuwe array per call retourneren.
const EMPTY_PBIS: BacklogPbi[] = []
const EMPTY_STORIES: BacklogStory[] = []
const EMPTY_TASKS: (BacklogTask | TaskDetail)[] = []
/**
* Lijst-selector. Vereist `useShallow` in componenten (G2) — anders re-rendert
* elke ongerelateerde store-mutatie het component.
*/
export function selectVisiblePbis(s: ProductWorkspaceStore): BacklogPbi[] {
if (s.relations.pbiIds.length === 0) return EMPTY_PBIS
const out: BacklogPbi[] = []
for (const id of s.relations.pbiIds) {
const pbi = s.entities.pbisById[id]
if (pbi) out.push(pbi)
}
return out.length === 0 ? EMPTY_PBIS : out
}
/**
* Lijst-selector. Vereist `useShallow` in componenten (G2).
*/
export function selectStoriesForActivePbi(s: ProductWorkspaceStore): BacklogStory[] {
const pbiId = s.context.activePbiId
if (!pbiId) return EMPTY_STORIES
const ids = s.relations.storyIdsByPbi[pbiId]
if (!ids || ids.length === 0) return EMPTY_STORIES
const out: BacklogStory[] = []
for (const id of ids) {
const story = s.entities.storiesById[id]
if (story) out.push(story)
}
return out.length === 0 ? EMPTY_STORIES : out
}
/**
* Lijst-selector. Vereist `useShallow` in componenten (G2).
*/
export function selectTasksForActiveStory(
s: ProductWorkspaceStore,
): (BacklogTask | TaskDetail)[] {
const storyId = s.context.activeStoryId
if (!storyId) return EMPTY_TASKS
const ids = s.relations.taskIdsByStory[storyId]
if (!ids || ids.length === 0) return EMPTY_TASKS
const out: (BacklogTask | TaskDetail)[] = []
for (const id of ids) {
const task = s.entities.tasksById[id]
if (task) out.push(task)
}
return out.length === 0 ? EMPTY_TASKS : out
}
/**
* Single-value selector. `useShallow` niet vereist — retourneert stable
* entity-reference (zelfde object zolang entity ongewijzigd).
*/
export function selectActivePbi(s: ProductWorkspaceStore): BacklogPbi | null {
const id = s.context.activePbiId
if (!id) return null
return s.entities.pbisById[id] ?? null
}
/**
* Single-value selector. `useShallow` niet vereist.
*/
export function selectActiveStory(s: ProductWorkspaceStore): BacklogStory | null {
const id = s.context.activeStoryId
if (!id) return null
return s.entities.storiesById[id] ?? null
}
/**
* Single-value selector. `useShallow` niet vereist.
*/
export function selectActiveTask(
s: ProductWorkspaceStore,
): BacklogTask | TaskDetail | null {
const id = s.context.activeTaskId
if (!id) return null
return s.entities.tasksById[id] ?? null
}
/**
* Single-value selector voor stories binnen een specifiek PBI (niet per se actief).
*/
export function selectStoriesForPbi(
s: ProductWorkspaceStore,
pbiId: string,
): BacklogStory[] {
const ids = s.relations.storyIdsByPbi[pbiId]
if (!ids || ids.length === 0) return EMPTY_STORIES
const out: BacklogStory[] = []
for (const id of ids) {
const story = s.entities.storiesById[id]
if (story) out.push(story)
}
return out.length === 0 ? EMPTY_STORIES : out
}