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 (
<>
Try:{' '} {suggestions.map((s) => ( ))}
)} > ) }