feat(PBI-74): sprint hydratie + realtime SSE (Story 9 / T-880)

- app/api/realtime/sprint/route.ts: SSE-stream LISTEN/NOTIFY op
  scrum4me_changes, filter entity ∈ {sprint, story, task} per product_id;
  ready-event, heartbeat 25s, hard-close 240s
- lib/realtime/use-sprint-realtime.ts: client-hook met backoff-reconnect;
  ready-cycle telt; geen close op hidden; setRealtimeStatus
- lib/realtime/use-sprint-workspace-resync.ts: visibility + online triggers
  resyncActiveScopes('visible' | 'reconnect')
- components/sprint/sprint-hydration-wrapper.tsx: hydrateSnapshot via
  useEffect met fingerprint-check; mount realtime + resync
- app/(app)/products/[id]/sprint/[sprintId]/page.tsx: wrap SprintBoardClient
  in SprintHydrationWrapper; bouw SprintWorkspaceTask-shape voor
  tasksByStoryWorkspace en SprintHydrationData voor de wrapper

Schaduw-fase: useSprintStore blijft parallel werken in board components
totdat T-881 die migreert en T-883 de oude store opruimt.
This commit is contained in:
Janpeter Visser 2026-05-10 06:37:59 +02:00
parent fdd83005a8
commit 307b998871
5 changed files with 410 additions and 11 deletions

View file

@ -0,0 +1,86 @@
'use client'
// PBI-74 / Story 9 / T-880: Sprint workspace hydration wrapper.
//
// Server-component (sprint page) fetcht initial sprint snapshot; deze wrapper
// hydreert useSprintWorkspaceStore op client-mount, mount de SSE-hook en de
// resync-laag. Tijdens T-880 (schaduw-fase) blijft useSprintStore parallel
// werken in sprint-board components — beide stores naast elkaar tot T-881
// componenten omzet en T-883 de oude store opruimt.
import { useEffect, useRef } from 'react'
import { useSprintRealtime } from '@/lib/realtime/use-sprint-realtime'
import { useSprintWorkspaceResync } from '@/lib/realtime/use-sprint-workspace-resync'
import { useSprintWorkspaceStore } from '@/stores/sprint-workspace/store'
import type {
SprintWorkspaceSnapshot,
SprintWorkspaceSprint,
SprintWorkspaceStory,
SprintWorkspaceTask,
} from '@/stores/sprint-workspace/types'
export interface SprintHydrationData {
sprint: SprintWorkspaceSprint
stories: SprintWorkspaceStory[]
tasksByStory: Record<string, SprintWorkspaceTask[]>
}
interface SprintHydrationWrapperProps {
initialData: SprintHydrationData
productId: string
productName?: string
children: React.ReactNode
}
function fingerprint(data: SprintHydrationData): string {
const sprintPart = `${data.sprint.id}:${data.sprint.status}`
const storyPart = data.stories
.map((s) => `${s.id}:${s.status}:${s.sprint_id ?? 'null'}:${s.sort_order}`)
.join(',')
const taskPart = Object.entries(data.tasksByStory)
.flatMap(([, list]) => list.map((t) => `${t.id}:${t.status}:${t.sort_order}`))
.join(',')
return `${sprintPart}|${storyPart}|${taskPart}`
}
function toWorkspaceSnapshot(
data: SprintHydrationData,
productId: string,
productName: string | undefined,
): SprintWorkspaceSnapshot {
return {
product: { id: productId, name: productName ?? '' },
sprint: data.sprint,
stories: data.stories,
tasksByStory: data.tasksByStory,
}
}
export function SprintHydrationWrapper({
initialData,
productId,
productName,
children,
}: SprintHydrationWrapperProps) {
const lastFingerprint = useRef<string>('')
useEffect(() => {
const fp = fingerprint(initialData)
if (fp !== lastFingerprint.current) {
lastFingerprint.current = fp
useSprintWorkspaceStore
.getState()
.hydrateSnapshot(toWorkspaceSnapshot(initialData, productId, productName))
// T-880 schaduw-fase: zet activeSprintId zodat selectors meteen werken
useSprintWorkspaceStore.setState((s) => {
s.context.activeSprintId = initialData.sprint.id
s.context.activeProduct = { id: productId, name: productName ?? '' }
})
}
}, [initialData, productId, productName])
useSprintRealtime(productId)
useSprintWorkspaceResync()
return <>{children}</>
}