Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web_app/src/components/stance/SentenceHeading.tsx from CommunityOne/open-navigator: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/components/stance/SentenceHeading.tsx
- Command line
-
hf download hf://spaces/CommunityOne/open-navigator/web_app/src/components/stance/SentenceHeading.tsx
-
curl -L -o SentenceHeading.tsx https://huggingface.co/spaces/CommunityOne/open-navigator/resolve/main/web_app/src/components/stance/SentenceHeading.tsx
3.74 kB
| 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 ( | |
| <span className="stance-blank-wrap"> | |
| <select className="stance-blank" aria-label={b.label} value={b.value} onChange={(e) => b.onChange(e.target.value)}> | |
| {groups.map((g) => { | |
| const opts = b.options | |
| .filter((o) => (o.group ?? '') === g) | |
| .map((o) => ( | |
| <option key={o.value} value={o.value} title={o.hint}> | |
| {o.label} | |
| </option> | |
| )) | |
| return g ? ( | |
| <optgroup key={g} label={g}> | |
| {opts} | |
| </optgroup> | |
| ) : ( | |
| opts | |
| ) | |
| })} | |
| </select> | |
| <span aria-hidden="true" className="stance-blank-caret"> | |
| ▾ | |
| </span> | |
| </span> | |
| ) | |
| } | |
| export default function SentenceHeading({ template, blanks, as: Tag = 'h1', className, suggestions }: Props) { | |
| return ( | |
| <> | |
| <Tag className={`stance-sentence${className ? ` ${className}` : ''}`}> | |
| {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 <span key={i}>{part}</span> | |
| } | |
| 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 ( | |
| <span key={i} className={mark ? 'stance-nowrap' : undefined}> | |
| {b.control ?? <BlankSelect b={b} />} | |
| {mark} | |
| </span> | |
| ) | |
| })} | |
| </Tag> | |
| {suggestions && suggestions.length > 0 && ( | |
| <p className="stance-try"> | |
| Try:{' '} | |
| {suggestions.map((s) => ( | |
| <button key={s.label} type="button" className="folio-link-btn" onClick={s.onPick}> | |
| {s.label} | |
| </button> | |
| ))} | |
| </p> | |
| )} | |
| </> | |
| ) | |
| } | |