Scrum4Me/stores/sprint-workspace/selectors.ts
Madhura68 fdd83005a8 feat(PBI-74): sprint-workspace-store skelet (Story 9 / T-879)
- stores/sprint-workspace/{types,store,selectors,restore}.ts conform
  product-workspace blueprint
- ContextSlice: activeProduct, activeSprintId, activeStoryId, activeTaskId
- EntitiesSlice: sprintsById, storiesById, tasksById
- RelationsSlice: sprintIdsByProduct, storyIdsBySprint, taskIdsByStory
- LoadingSlice met activeRequestId voor race-safe ensure*Loaded
- SyncSlice: realtimeStatus, lastResyncAt, resyncReason
- Realtime applyRealtimeEvent voor sprint/story/task entities + unknown-event
  fallback, parent-move handling, child-cleanup bij D op sprint/story
- Optimistic mutations: sprint-story-order, sprint-task-order, entity-patch
- LocalStorage hints (storage key sprint-workspace-hints) per product/sprint
- 45 unit-tests groen — verplicht 13 cases uit workspace-store.md §Tests
2026-05-10 06:34:05 +02:00

115 lines
3.5 KiB
TypeScript

import type { SprintWorkspaceStore } from './store'
import type {
SprintWorkspaceSprint,
SprintWorkspaceStory,
SprintWorkspaceTask,
SprintWorkspaceTaskDetail,
} from './types'
// G1: stable EMPTY-references zodat selectors geen nieuwe array per call retourneren.
const EMPTY_SPRINTS: SprintWorkspaceSprint[] = []
const EMPTY_STORIES: SprintWorkspaceStory[] = []
const EMPTY_TASKS: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []
/**
* Lijst-selector. Vereist `useShallow` in componenten (G2).
*/
export function selectVisibleSprints(s: SprintWorkspaceStore): SprintWorkspaceSprint[] {
const productId = s.context.activeProduct?.id
if (!productId) return EMPTY_SPRINTS
const ids = s.relations.sprintIdsByProduct[productId]
if (!ids || ids.length === 0) return EMPTY_SPRINTS
const out: SprintWorkspaceSprint[] = []
for (const id of ids) {
const sprint = s.entities.sprintsById[id]
if (sprint) out.push(sprint)
}
return out.length === 0 ? EMPTY_SPRINTS : out
}
/**
* Lijst-selector. Vereist `useShallow` in componenten (G2).
*/
export function selectStoriesForActiveSprint(
s: SprintWorkspaceStore,
): SprintWorkspaceStory[] {
const sprintId = s.context.activeSprintId
if (!sprintId) return EMPTY_STORIES
const ids = s.relations.storyIdsBySprint[sprintId]
if (!ids || ids.length === 0) return EMPTY_STORIES
const out: SprintWorkspaceStory[] = []
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: SprintWorkspaceStore,
): (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] {
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: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []
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.
*/
export function selectActiveSprint(
s: SprintWorkspaceStore,
): SprintWorkspaceSprint | null {
const id = s.context.activeSprintId
if (!id) return null
return s.entities.sprintsById[id] ?? null
}
/**
* Single-value selector. `useShallow` niet vereist.
*/
export function selectActiveStory(
s: SprintWorkspaceStore,
): SprintWorkspaceStory | 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: SprintWorkspaceStore,
): SprintWorkspaceTask | SprintWorkspaceTaskDetail | null {
const id = s.context.activeTaskId
if (!id) return null
return s.entities.tasksById[id] ?? null
}
/**
* Lijst-selector voor tasks binnen een specifieke story (niet per se actief).
* Vereist `useShallow` in componenten (G2).
*/
export function selectTasksForStory(
s: SprintWorkspaceStore,
storyId: string,
): (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] {
const ids = s.relations.taskIdsByStory[storyId]
if (!ids || ids.length === 0) return EMPTY_TASKS
const out: (SprintWorkspaceTask | SprintWorkspaceTaskDetail)[] = []
for (const id of ids) {
const task = s.entities.tasksById[id]
if (task) out.push(task)
}
return out.length === 0 ? EMPTY_TASKS : out
}