Scrum4Me/components/dialogs/product-dialog.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

311 lines
12 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { useForm, useWatch } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { toast } from 'sonner'
import { cn } from '@/lib/utils'
import {
Dialog,
DialogContent,
DialogTitle,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
import { DemoTooltip } from '@/components/shared/demo-tooltip'
import {
useDirtyCloseGuard,
DirtyCloseGuardDialog,
} from '@/components/shared/use-dirty-close-guard'
import { useDialogSubmitShortcut } from '@/components/shared/use-dialog-submit-shortcut'
import {
entityDialogBodyClasses,
entityDialogContentClasses,
entityDialogFooterClasses,
entityDialogHeaderClasses,
} from '@/components/shared/entity-dialog-layout'
import { productSchema, type ProductInput } from '@/lib/schemas/product'
import { createProductAction, updateProductAction } from '@/actions/products'
import { useProductsStore } from '@/stores/products-store'
import { debugProps } from '@/lib/debug'
export interface ProductDialogProduct {
id: string
name: string
code?: string | null
description?: string | null
repo_url?: string | null
definition_of_done?: string | null
auto_pr?: boolean
}
type Props =
| { mode: 'create'; open: boolean; onOpenChange: (v: boolean) => void; onSaved?: (id: string) => void; isDemo?: boolean }
| { mode: 'edit'; open: boolean; onOpenChange: (v: boolean) => void; product: ProductDialogProduct; onSaved?: (id: string) => void; isDemo?: boolean }
export function ProductDialog(props: Props) {
const { mode, open, onOpenChange, isDemo = false } = props
const product = mode === 'edit' ? props.product : null
const addProduct = useProductsStore((s) => s.addProduct)
const updateProduct = useProductsStore((s) => s.updateProduct)
const [isPending, setIsPending] = useState(false)
const form = useForm<ProductInput>({
resolver: zodResolver(productSchema),
mode: 'onTouched',
defaultValues: {
name: product?.name ?? '',
code: product?.code ?? '',
description: product?.description ?? '',
repo_url: product?.repo_url ?? '',
definition_of_done: product?.definition_of_done ?? '',
auto_pr: product?.auto_pr ?? false,
},
})
// Reset when opening or switching product
useEffect(() => {
if (open) {
form.reset({
name: product?.name ?? '',
code: product?.code ?? '',
description: product?.description ?? '',
repo_url: product?.repo_url ?? '',
definition_of_done: product?.definition_of_done ?? '',
auto_pr: product?.auto_pr ?? false,
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, product?.id])
const closeGuard = useDirtyCloseGuard(form.formState.isDirty, () => onOpenChange(false))
const handleKeyDown = useDialogSubmitShortcut(() => form.handleSubmit(onSubmit)())
async function onSubmit(values: ProductInput) {
setIsPending(true)
try {
const payload: ProductInput = {
name: values.name,
code: values.code || undefined,
description: values.description || undefined,
repo_url: values.repo_url || null,
definition_of_done: values.definition_of_done || undefined,
auto_pr: values.auto_pr,
}
function applyError(result: { error: string; code?: number; fieldErrors?: Partial<Record<keyof ProductInput, string[]>> }) {
if (result.code === 422 && result.fieldErrors) {
for (const [field, errors] of Object.entries(result.fieldErrors)) {
if (errors && errors.length > 0) {
form.setError(field as keyof ProductInput, { message: errors[0] })
}
}
const firstError = Object.keys(result.fieldErrors)[0] as keyof ProductInput | undefined
if (firstError) form.setFocus(firstError)
return
}
toast.error(result.error)
}
if (mode === 'create') {
const result = await createProductAction(payload)
if ('error' in result) {
applyError(result)
return
}
const productId = result.productId
addProduct({
id: productId,
name: values.name,
code: values.code ?? null,
description: values.description ?? null,
repo_url: values.repo_url ?? null,
definition_of_done: values.definition_of_done ?? '',
auto_pr: values.auto_pr,
})
toast.success('Product aangemaakt')
onOpenChange(false)
props.onSaved?.(productId)
} else {
const result = await updateProductAction(product!.id, payload)
if ('error' in result) {
applyError(result)
return
}
updateProduct(product!.id, {
name: values.name,
code: values.code ?? null,
description: values.description ?? null,
repo_url: values.repo_url ?? null,
definition_of_done: values.definition_of_done ?? '',
auto_pr: values.auto_pr,
})
toast.success('Product opgeslagen')
onOpenChange(false)
props.onSaved?.(product!.id)
}
} finally {
setIsPending(false)
}
}
const autoPr = useWatch({ control: form.control, name: 'auto_pr' })
return (
<>
<Dialog open={open} onOpenChange={(v) => { if (!v) closeGuard.attemptClose(); else onOpenChange(v) }}>
<DialogContent
showCloseButton={false}
onKeyDown={handleKeyDown}
className={entityDialogContentClasses}
{...debugProps('product-dialog', 'ProductDialog', 'components/dialogs/product-dialog.tsx')}
>
<div className={entityDialogHeaderClasses}>
<DialogTitle className="text-xl font-semibold">
{mode === 'edit' ? 'Product bewerken' : 'Nieuw product'}
</DialogTitle>
</div>
<form
id="product-form"
onSubmit={form.handleSubmit(onSubmit)}
className={entityDialogBodyClasses}
data-debug-id="product-dialog__content"
>
<div className="grid gap-1.5">
<label htmlFor="product-name" className="text-sm font-medium">
Naam <span className="text-error">*</span>
</label>
<Input
id="product-name"
autoFocus={mode === 'create'}
disabled={isDemo}
maxLength={200}
aria-invalid={!!form.formState.errors.name}
{...form.register('name')}
className={form.formState.errors.name ? 'border-error' : ''}
/>
{form.formState.errors.name && (
<p className="text-xs text-error">{form.formState.errors.name.message}</p>
)}
</div>
<div className="grid gap-1.5">
<label htmlFor="product-code" className="text-sm font-medium">
Code <span className="text-muted-foreground font-normal">(optioneel)</span>
</label>
<Input
id="product-code"
disabled={isDemo}
maxLength={20}
placeholder="korte slug, bv. SCRUM4ME"
aria-invalid={!!form.formState.errors.code}
className={cn('font-mono text-sm', form.formState.errors.code && 'border-error')}
{...form.register('code')}
/>
{form.formState.errors.code && (
<p className="text-xs text-error">{form.formState.errors.code.message}</p>
)}
</div>
<div className="grid gap-1.5">
<label htmlFor="product-description" className="text-sm font-medium">
Beschrijving <span className="text-muted-foreground font-normal">(optioneel)</span>
</label>
<Textarea
id="product-description"
disabled={isDemo}
rows={3}
maxLength={4000}
className="resize-none"
{...form.register('description')}
/>
</div>
<div className="grid gap-1.5">
<label htmlFor="product-repo-url" className="text-sm font-medium">
Repository URL <span className="text-muted-foreground font-normal">(optioneel)</span>
</label>
<Input
id="product-repo-url"
disabled={isDemo}
placeholder="https://github.com/owner/repo"
aria-invalid={!!form.formState.errors.repo_url}
{...form.register('repo_url')}
className={form.formState.errors.repo_url ? 'border-error' : ''}
/>
{form.formState.errors.repo_url && (
<p className="text-xs text-error">{form.formState.errors.repo_url.message}</p>
)}
</div>
<div className="grid gap-1.5">
<label htmlFor="product-dod" className="text-sm font-medium">
Definition of Done <span className="text-muted-foreground font-normal">(optioneel)</span>
</label>
<Textarea
id="product-dod"
disabled={isDemo}
rows={4}
maxLength={4000}
className="resize-none"
{...form.register('definition_of_done')}
/>
</div>
<div className="flex items-start gap-3">
<button
type="button"
role="switch"
aria-checked={autoPr}
disabled={isDemo}
onClick={() => form.setValue('auto_pr', !autoPr, { shouldDirty: true })}
className={cn(
'relative mt-0.5 inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full border-2 border-transparent',
'transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
'disabled:cursor-not-allowed disabled:opacity-50',
autoPr ? 'bg-primary' : 'bg-input',
)}
>
<span
className={cn(
'pointer-events-none inline-block h-4 w-4 rounded-full bg-background shadow-sm',
'transition-transform duration-200',
autoPr ? 'translate-x-4' : 'translate-x-0',
)}
/>
</button>
<div className="grid gap-0.5">
<span className="text-sm font-medium">Automatisch PR aanmaken na voltooide story</span>
<span className="text-xs text-muted-foreground">
Bij elke voltooide story automatisch een PR aanmaken in <code>repo_url</code>
</span>
</div>
</div>
</form>
<div className={entityDialogFooterClasses}>
<div className="flex items-center justify-end gap-2">
<Button
type="button"
variant="ghost"
onClick={closeGuard.attemptClose}
disabled={isPending}
>
Annuleren
</Button>
<DemoTooltip show={isDemo}>
<Button type="submit" form="product-form" disabled={isPending || isDemo} data-debug-id="product-dialog__submit">
{isPending ? '…' : mode === 'edit' ? 'Opslaan' : 'Aanmaken'}
</Button>
</DemoTooltip>
</div>
</div>
</DialogContent>
</Dialog>
<DirtyCloseGuardDialog guard={closeGuard} />
</>
)
}