import { useState } from "react" import { ChevronDown, Plus, Trash2 } from "lucide-react" import { Button } from "../../../../components/ui/button" import { Card } from "../../../../components/ui/card" import { Input } from "../../../../components/ui/input" import { DynamicSchemaSlotField } from "../../../../components/content-management/DynamicSchemaSlotField" import { cn } from "../../../../lib/utils" import { countQuestionsReferencingBlock, createEmptyBlockElement, createEmptyStimulusBlock, STIMULUS_BLOCK_KINDS, stimulusBlockKindLabel, type PracticeFormStimulusBlock, } from "../../../../lib/practiceStimulusBlocks" import { getStimulusKindPresentation } from "../question-type-steps/componentKindUi" interface StimulusBlocksPanelProps { blocks: PracticeFormStimulusBlock[] questions: { stimulusBlockKey?: string | null }[] onChange: (blocks: PracticeFormStimulusBlock[]) => void onClearQuestionBlockKeys?: (blockKey: string) => void } export function StimulusBlocksPanel({ blocks, questions, onChange, onClearQuestionBlockKeys, }: StimulusBlocksPanelProps) { const [expandedBlockKeys, setExpandedBlockKeys] = useState>( () => new Set(blocks[0]?.blockKey ? [blocks[0].blockKey] : []), ) const toggleBlock = (blockKey: string) => { setExpandedBlockKeys((prev) => { const next = new Set(prev) if (next.has(blockKey)) next.delete(blockKey) else next.add(blockKey) return next }) } const updateBlock = (index: number, patch: Partial) => { const next = [...blocks] next[index] = { ...next[index], ...patch } onChange(next) } const removeBlock = (index: number) => { const block = blocks[index] const refCount = countQuestionsReferencingBlock(block.blockKey, questions) if (refCount > 0) { const proceed = window.confirm( `${refCount} question(s) reference block "${block.blockKey}". Remove the block and clear those links?`, ) if (!proceed) return onClearQuestionBlockKeys?.(block.blockKey) } const next = blocks.filter((_, i) => i !== index) onChange( next.map((b, i) => ({ ...b, displayOrder: i + 1, })), ) } const addBlock = () => { const block = createEmptyStimulusBlock(blocks) onChange([...blocks, block]) setExpandedBlockKeys(new Set([block.blockKey])) } return (

Shared stimulus blocks

Define audio, passages, or instructions once per section. Link questions to a block so they share that content.

{blocks.length === 0 ? (
No stimulus blocks yet. Add a block for each listening or reading section.
) : null}
{blocks.map((block, blockIndex) => { const expanded = expandedBlockKeys.has(block.blockKey) const refCount = countQuestionsReferencingBlock(block.blockKey, questions) return (
{expanded ? (
updateBlock(blockIndex, { blockKey: e.target.value }) } className="font-mono text-sm" placeholder="section-1" />

Shared stimulus

{block.elements.map((element, elementIndex) => { const presentation = getStimulusKindPresentation(element.kind) const fieldValues: Record = { [`stimulus:${element.id}`]: element.fieldValue, } return (
{presentation.label} {element.id}
{ const elements = [...block.elements] elements[elementIndex] = { ...elements[elementIndex], fieldValue: next, } updateBlock(blockIndex, { elements }) }} allFieldValues={fieldValues} stimulusSchema={block.elements.map((el) => ({ id: el.id, kind: el.kind, }))} responseSchema={[]} />
) })}
) : null}
) })}
) }