import type { ReactNode } from 'react' /** * A heading written as a sentence with blanks that are the controls: "What did [Alabama] say about [data centers]?". * The props mirror the shared component another branch is building (`components/story/SentenceHeading`), so the two can be * merged into one afterwards: a template of text parts and blanks, each blank `{ id, label, value, display, options, onChange, * searchable? }`, and an optional "Try" row. Local addition: a blank may bring its own `control` (the place blank uses the * place popover, which also takes a community), shown instead of the select built from `options`. */ export interface BlankOption { value: string label: string group?: string hint?: string } export interface Blank { id: string /** The accessible name ("Place", "Topic"). */ label: string value: string /** The text the blank shows for the current value. */ display: string options: BlankOption[] onChange: (value: string) => void searchable?: boolean /** Replaces the select built from `options`. */ control?: ReactNode } export type TemplatePart = string | { blank: string } export interface Suggestion { label: string onPick: () => void } interface Props { template: TemplatePart[] blanks: Blank[] as?: 'h1' | 'h2' className?: string suggestions?: Suggestion[] } /** A template part that is only closing punctuation ("?", "."), which must stay on the line of the word before it. */ export function isClosingMark(part: string): boolean { return /^\s*[?!.,;:]+\s*$/.test(part) } function BlankSelect({ b }: { b: Blank }) { const groups = [...new Set(b.options.map((o) => o.group ?? ''))] return ( ) } export default function SentenceHeading({ template, blanks, as: Tag = 'h1', className, suggestions }: Props) { return ( <> {template.map((part, i) => { if (typeof part === 'string') { // Closing punctuation is drawn with the blank before it (below), so "?" never wraps onto a line alone. if (isClosingMark(part) && i > 0 && typeof template[i - 1] !== 'string') return null return {part} } const b = blanks.find((x) => x.id === part.blank) if (!b) return null const next = template[i + 1] const mark = typeof next === 'string' && isClosingMark(next) ? next.trim() : '' return ( {b.control ?? } {mark} ) })} {suggestions && suggestions.length > 0 && (

Try:{' '} {suggestions.map((s) => ( ))}

)} ) }