feat(fp): brief v3 — besluit-driven guided drafting

Compose the herregistratie letter from the besluit instead of a library hunt:
the behandelaar picks positief/negatief (+ reden-checkboxes for a negatief) and
the kern's standaardteksten follow the selection live.

Front-end (this increment):
- Kern is recomposed reactively from the besluit selection (new BesluitSelected
  machine msg + composeKern); the "Genereer conceptbrief" button is gone. The
  drafter's free text is preserved across a selection change.
- The editor shows only the editable sections; the locked aanhef/slot render in
  the preview, not the authoring surface. Slot is a case-type template section
  (per templateId), documented as such.
- The panel re-seeds from the letter via inferSelection() — the besluit + redenen
  are read back off the kern's passage blocks, so the selection survives reload
  with no new wire fields (derive, don't store).
- letter-section drops the now-redundant per-section passage picker (besluit owns
  standaardteksten); keeps free-text + block edit/move/remove.

Fix: app-checkbox now falls back to a unique per-instance id when checkboxId is
omitted. The CIBG styled checkbox routes clicks through the label, so the shared
id="undefined" made every reason label toggle the first input — the second
checkbox could never be checked. Verified live (Playwright): each reason toggles
independently.

Backend/seam (brief v3 WIP): besluit/reason passage tags on the wire + seed,
carried through the adapter parse boundary.

Specs updated (besluit, brief.machine) and the affected stories re-pointed at the
new API. FE lint + build + 253 vitest specs green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
eho
2026-07-20 18:53:53 +02:00
co-authored by Claude Opus 4.8
parent 62ba0b98c4
commit ba32e3dd9f
31 changed files with 4900 additions and 2878 deletions
+72
View File
@@ -0,0 +1,72 @@
import { Besluit, LetterBlock, LibraryPassage } from './brief';
/**
* Guided drafting: given the behandelaar's besluit + chosen redenen, which library
* passages belong in the kern. This is the "don't make them a detective" logic —
* pure, so it's unit-tested directly and the UI just renders the result.
*
* A passage is offered when:
* - it has no besluit tag (a shared intro/toelichting, relevant to any besluit), OR
* - its besluit matches AND either it isn't reason-specific, or its reason is ticked.
*
* Kept in library order (server order = reading order), so an inserted set already
* flows as a letter.
*/
export function passagesForBesluit(
passages: readonly LibraryPassage[],
besluit: Besluit,
reasons: readonly string[],
): LibraryPassage[] {
return passages.filter((p) => {
if (p.sectionKey !== 'kern') return false;
if (p.besluit === undefined) return true; // shared, any besluit
if (p.besluit !== besluit) return false;
if (p.reason === undefined) return true; // besluit-level, not reason-specific
return reasons.includes(p.reason);
});
}
/** A selectable reden for a besluit, derived from the reason-specific passages — no
separate catalog. `code` drives `passagesForBesluit`; `label` is the checkbox text.
ponytail: assumes one passage per reason (true for the seed); dedupes on code if not. */
export interface Reden {
readonly code: string;
readonly label: string;
}
export function redenenFor(passages: readonly LibraryPassage[], besluit: Besluit): Reden[] {
const seen = new Set<string>();
const out: Reden[] = [];
for (const p of passages) {
if (p.sectionKey !== 'kern' || p.besluit !== besluit || p.reason === undefined) continue;
if (seen.has(p.reason)) continue;
seen.add(p.reason);
out.push({ code: p.reason, label: p.label });
}
return out;
}
/**
* The inverse of `passagesForBesluit`: read the current besluit + redenen back off the
* kern's passage blocks (each carries its `sourcePassageId`), so the panel can re-seed
* itself on reload/undo without persisting the selection separately. Kern passage blocks
* are besluit-derived by construction (the only way passages enter the kern), so this
* round-trips: `inferSelection(kern(passagesForBesluit(lib, b, r)), lib) === { b, r }`.
* Free-text blocks carry no provenance and are ignored.
*/
export function inferSelection(
kernBlocks: readonly LetterBlock[],
passages: readonly LibraryPassage[],
): { besluit: Besluit | null; reasons: string[] } {
const byId = new Map(passages.map((p) => [p.passageId, p]));
let besluit: Besluit | null = null;
const reasons: string[] = [];
for (const b of kernBlocks) {
if (b.type !== 'passage') continue;
const source = byId.get(b.sourcePassageId);
if (!source) continue;
if (source.besluit !== undefined) besluit = source.besluit;
if (source.reason !== undefined && !reasons.includes(source.reason)) reasons.push(source.reason);
}
return { besluit, reasons };
}