Scrum4Me/components/sprint/sprint-board-client.tsx
Janpeter Visser d292e445d9
Sprint: Verbeteren debug mode (#179)
* feat(PBI-49): add debugProps helper + Vitest test

Adds lib/debug.ts with debugProps(id, component, file) that returns
data-debug-id and data-debug-label attrs in dev mode, empty object in
production. Adds __tests__/lib/debug.test.ts covering both modes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(PBI-49): add debug-id pattern doc + CLAUDE.md reference

Adds docs/patterns/debug-id.md documenting the named-component boundary
rule (6 punten), helper-voorbeeld, skip-criteria en motivatie voor
handmatige pad-argumenten. Voegt verwijzing toe aan CLAUDE.md
patterns-tabel.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(PBI-49): migrate 17 shared/ components to debugProps helper

Replace hardcoded data-debug-id + data-debug-label attribute pairs with
{...debugProps(id, component, file)} spread in all 17 components/shared/
files. Existing debug-ids preserved unchanged.

* feat(PBI-49): add debugProps to backlog/, sprint/, solo/ components

* feat(PBI-49): add debugProps to jobs/ + ideas/ components

* feat(PBI-49): add debugProps to products/ + settings/ + notifications/ components

* feat(PBI-49): add debugProps to admin/ + dashboard/ + dialogs/ + mobile/ + split-pane/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(PBI-49): use attr(data-debug-id) for debug tooltip in globals.css

* refactor(PBI-49): remove data-debug-label from debugProps helper + test

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(PBI-49): strip unused component/file args from debugProps in shared/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to StatusBar, NavBar, PanelNavBar

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to components/sprint/*

- new-sprint-dialog: __submit on submit button
- sprint-backlog: __list on SprintBacklogLeft + SprintBacklogRight scroll areas
- sprint-board-client: root wrapper div (display:contents) + __drag-overlay
- sprint-header: __title on goal button, __dates on dates button, __actions on action cluster
- sprint-run-controls: root on controls div, __start/__cancel on action buttons; __blockers-dialog on dialog content
- start-sprint-button: root on trigger button, __dialog on dialog content, __submit on submit button
- sync-active-sprint-cookie: no debug-id (returns null, side-effect only), comment added

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to components/backlog/*

* feat(PBI-49): add BEM sub-element data-debug-id to components/ideas/*

* feat(PBI-49): add BEM sub-element data-debug-id to components/dashboard/* + components/markdown.tsx

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to new-product-button

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to components/solo/*

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-elements to nav-status-indicators

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to components/jobs/*

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to components/products/*

* feat(PBI-49): add BEM sub-element data-debug-id to components/notifications/*

- answer-modal: __content (scroll area), __submit (footer)
- notifications-bridge: skip comment (bridge, non-rendering wrapper)
- notifications-realtime-mount: skip comment (returns null)
- notifications-sheet: __header, __items (questions list)
- push-toggle: __switch (button), __label (button text) on subscribed/unsubscribed states

* feat(PBI-49): add BEM sub-element data-debug-id to components/settings/*

- leave-product-button: root only (single-button component)
- min-quota-editor: __input (number input), __save (save button)
- profile-editor: __username (bio/short-description input), __save (submit)
- role-manager: __roles (checkbox list), __add (save button)
- token-manager: __tokens (active tokens list), __generate (create button)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(PBI-49): add BEM sub-element data-debug-id to admin, auth, dialogs, entity-dialog, mobile, split-pane

* docs(PBI-49): add debug-labels BEM pattern doc + CLAUDE.md entry

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-09 22:46:29 +02:00

258 lines
9.2 KiB
TypeScript

'use client'
import { useState, useEffect, useTransition } from 'react'
import {
DndContext, DragEndEvent, DragStartEvent, DragOverlay,
KeyboardSensor, PointerSensor, useSensor, useSensors, closestCenter,
} from '@dnd-kit/core'
import { sortableKeyboardCoordinates, arrayMove } from '@dnd-kit/sortable'
import { toast } from 'sonner'
import { SplitPane } from '@/components/split-pane/split-pane'
import { SprintBacklogLeft, SprintBacklogRight } from './sprint-backlog'
import type { SprintStory, PbiWithStories, ProductMember } from './sprint-backlog'
import { TaskList } from './task-list'
import type { Task } from './task-list'
import { useSprintStore } from '@/stores/sprint-store'
import {
addStoryToSprintAction,
removeStoryFromSprintAction,
reorderSprintStoriesAction,
} from '@/actions/sprints'
import { debugProps } from '@/lib/debug'
interface SprintBoardClientProps {
productId: string
sprintId: string
stories: SprintStory[]
pbisWithStories: PbiWithStories[]
sprintStoryIdList: string[]
tasksByStory: Record<string, Task[]>
isDemo: boolean
currentUserId: string
members: ProductMember[]
}
export function SprintBoardClient({
productId,
sprintId,
stories,
pbisWithStories,
sprintStoryIdList,
tasksByStory,
isDemo,
currentUserId,
members,
}: SprintBoardClientProps) {
const [sprintStories, setSprintStories] = useState<SprintStory[]>(stories)
const [sprintStoryIds, setSprintStoryIds] = useState<Set<string>>(() => new Set(sprintStoryIdList))
const [selectedStoryId, setSelectedStoryId] = useState<string | null>(null)
const {
sprintStoryOrder,
initSprint,
addStoryToSprint,
removeStoryFromSprint,
reorderSprintStories,
rollbackSprint,
} = useSprintStore()
const [activeDragStory, setActiveDragStory] = useState<SprintStory | null>(null)
const [, startTransition] = useTransition()
useEffect(() => {
initSprint(sprintId, stories.map(s => s.id))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sprintId])
const sensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 5 } }),
useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
)
function handleDragStart(event: DragStartEvent) {
const id = event.active.id.toString()
if (id.startsWith('pb:')) {
const story = pbisWithStories.flatMap(p => p.stories).find(s => s.id === id.slice(3))
setActiveDragStory(story ?? null)
}
}
function handleDragEnd(event: DragEndEvent) {
setActiveDragStory(null)
const { active, over } = event
if (!over) return
const activeId = active.id.toString()
const overId = over.id.toString()
const order = sprintStoryOrder[sprintId] ?? sprintStories.map(s => s.id)
// Drag from left (product backlog) → add to sprint (middle)
if (activeId.startsWith('pb:')) {
const storyId = activeId.slice(3)
const droppingOnSprint =
overId === 'sprint-zone' ||
(!overId.startsWith('pb:') && overId !== 'backlog-zone')
if (droppingOnSprint && !sprintStoryIds.has(storyId)) {
const storyData = pbisWithStories.flatMap(p => p.stories).find(s => s.id === storyId)
if (!storyData) return
setSprintStoryIds(prev => new Set([...prev, storyId]))
setSprintStories(prev => [...prev, storyData])
addStoryToSprint(sprintId, storyId)
startTransition(async () => {
const result = await addStoryToSprintAction(sprintId, storyId)
if (!result.success) {
setSprintStoryIds(prev => { const n = new Set(prev); n.delete(storyId); return n })
setSprintStories(prev => prev.filter(s => s.id !== storyId))
removeStoryFromSprint(sprintId, storyId)
toast.error(result.error ?? 'Toevoegen mislukt')
}
})
}
return
}
// Drag from middle (sprint backlog) → left (product backlog) → remove
if (overId === 'backlog-zone') {
const storyData = sprintStories.find(s => s.id === activeId)
setSprintStoryIds(prev => { const n = new Set(prev); n.delete(activeId); return n })
setSprintStories(prev => prev.filter(s => s.id !== activeId))
removeStoryFromSprint(sprintId, activeId)
if (selectedStoryId === activeId) setSelectedStoryId(null)
startTransition(async () => {
const result = await removeStoryFromSprintAction(activeId)
if (!result.success) {
if (storyData) {
setSprintStoryIds(prev => new Set([...prev, activeId]))
setSprintStories(prev => [...prev, storyData])
}
addStoryToSprint(sprintId, activeId)
toast.error('Verwijderen mislukt')
}
})
return
}
// Reorder within sprint (middle panel)
if (activeId !== overId && !activeId.startsWith('pb:')) {
const prevOrder = [...order]
const newOrder = order.includes(overId)
? arrayMove([...order], order.indexOf(activeId), order.indexOf(overId))
: [...order.filter(id => id !== activeId), activeId]
reorderSprintStories(sprintId, newOrder)
startTransition(async () => {
const result = await reorderSprintStoriesAction(sprintId, newOrder)
if (!result.success) {
rollbackSprint(sprintId, prevOrder)
toast.error('Volgorde opslaan mislukt')
}
})
}
}
function handleAdd(storyId: string) {
if (sprintStoryIds.has(storyId)) return
const storyData = pbisWithStories.flatMap(p => p.stories).find(s => s.id === storyId)
if (!storyData) return
setSprintStoryIds(prev => new Set([...prev, storyId]))
setSprintStories(prev => [...prev, storyData])
addStoryToSprint(sprintId, storyId)
startTransition(async () => {
const result = await addStoryToSprintAction(sprintId, storyId)
if (!result.success) {
setSprintStoryIds(prev => { const n = new Set(prev); n.delete(storyId); return n })
setSprintStories(prev => prev.filter(s => s.id !== storyId))
removeStoryFromSprint(sprintId, storyId)
toast.error(result.error ?? 'Toevoegen mislukt')
}
})
}
function handleAssigneeChange(storyId: string, assigneeId: string | null, assigneeUsername: string | null) {
setSprintStories(prev =>
prev.map(s => s.id === storyId ? { ...s, assignee_id: assigneeId, assignee_username: assigneeUsername } : s)
)
}
function handleRemove(storyId: string) {
const storyData = sprintStories.find(s => s.id === storyId)
setSprintStoryIds(prev => { const n = new Set(prev); n.delete(storyId); return n })
setSprintStories(prev => prev.filter(s => s.id !== storyId))
removeStoryFromSprint(sprintId, storyId)
if (selectedStoryId === storyId) setSelectedStoryId(null)
startTransition(async () => {
const result = await removeStoryFromSprintAction(storyId)
if (!result.success) {
if (storyData) {
setSprintStoryIds(prev => new Set([...prev, storyId]))
setSprintStories(prev => [...prev, storyData])
}
addStoryToSprint(sprintId, storyId)
toast.error('Verwijderen mislukt')
}
})
}
const selectedTasks = selectedStoryId ? (tasksByStory[selectedStoryId] ?? []) : []
return (
<div {...debugProps('sprint-board-client')} className="contents">
<DndContext
id="sprint-board"
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
>
<SplitPane
cookieKey={`sprint-${productId}`}
defaultSplit={[28, 35, 37]}
tabLabels={['Backlog', 'Sprint', 'Taken']}
panes={[
<SprintBacklogRight
key="backlog"
pbisWithStories={pbisWithStories}
sprintStoryIds={sprintStoryIds}
isDemo={isDemo}
productId={productId}
onAdd={handleAdd}
/>,
<SprintBacklogLeft
key="sprint"
sprintId={sprintId}
stories={sprintStories}
isDemo={isDemo}
onRemove={handleRemove}
onSelect={setSelectedStoryId}
selectedStoryId={selectedStoryId}
currentUserId={currentUserId}
productId={productId}
members={members}
onAssigneeChange={handleAssigneeChange}
/>,
selectedStoryId ? (
<TaskList
key="tasks"
storyId={selectedStoryId}
sprintId={sprintId}
productId={productId}
tasks={selectedTasks}
isDemo={isDemo}
/>
) : (
<div key="tasks-empty" className="flex items-center justify-center h-full">
<p className="text-sm text-muted-foreground">Selecteer een story om de taken te bekijken.</p>
</div>
),
]}
/>
<DragOverlay>
{activeDragStory && (
<div className="flex items-center gap-3 px-6 py-2 bg-popover border border-primary rounded shadow-lg text-sm opacity-95 w-72" data-debug-id="sprint-board-client__drag-overlay">
<span className="text-muted-foreground select-none"></span>
<span className="truncate flex-1">{activeDragStory.title}</span>
</div>
)}
</DragOverlay>
</DndContext>
</div>
)
}