|
1 | 1 | import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts"; |
2 | 2 | import { memo, useCallback, useEffect, useId, useRef, useState } from "react"; |
3 | | -import ReactMarkdown from "react-markdown"; |
4 | | -import remarkGfm from "remark-gfm"; |
5 | | -import type { Root, RootContent } from "mdast"; |
6 | 3 | import ChatMarkdown from "../ChatMarkdown"; |
7 | 4 | import { type PendingUserInput } from "../../session-logic"; |
8 | 5 | import { |
@@ -88,6 +85,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard( |
88 | 85 | const responseDisabled = disabled || isResponding || !canRespond; |
89 | 86 | const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex); |
90 | 87 | const optionIdPrefix = useId(); |
| 88 | + const headerId = `${optionIdPrefix}-header`; |
| 89 | + const previewId = `${optionIdPrefix}-preview`; |
91 | 90 | const activeQuestion = progress.activeQuestion; |
92 | 91 | const autoAdvanceTimerRef = useRef<number | null>(null); |
93 | 92 | const onAdvanceRef = useRef(onAdvance); |
@@ -205,57 +204,57 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard( |
205 | 204 | setCollapsedQuestionId(open ? null : activeQuestion.id); |
206 | 205 | }} |
207 | 206 | > |
208 | | - <CollapsibleTrigger |
209 | | - render={<ComposerBanner.Row render={<button type="button" />} />} |
210 | | - title={ |
211 | | - isCollapsed ? "Show the question and its options" : "Hide the question and its options" |
212 | | - } |
213 | | - data-pending-user-input-toggle={isCollapsed ? "collapsed" : "expanded"} |
214 | | - > |
215 | | - <ComposerBanner.Icon /> |
216 | | - <ComposerBanner.Content> |
217 | | - <span className="shrink-0 font-medium text-muted-foreground"> |
| 207 | + <ComposerBanner.Row className="relative"> |
| 208 | + <CollapsibleTrigger |
| 209 | + render={<button type="button" />} |
| 210 | + className="absolute inset-0 cursor-pointer rounded-md focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring" |
| 211 | + aria-labelledby={headerId} |
| 212 | + aria-describedby={isCollapsed ? previewId : undefined} |
| 213 | + title={ |
| 214 | + isCollapsed ? "Show the question and its options" : "Hide the question and its options" |
| 215 | + } |
| 216 | + data-pending-user-input-toggle={isCollapsed ? "collapsed" : "expanded"} |
| 217 | + /> |
| 218 | + <ComposerBanner.Icon className="pointer-events-none relative" /> |
| 219 | + <ComposerBanner.Content className="pointer-events-none relative"> |
| 220 | + <span id={headerId} className="shrink-0 font-medium text-muted-foreground"> |
218 | 221 | {activeQuestion.header} |
219 | 222 | </span> |
220 | 223 | {isCollapsed ? ( |
221 | | - <span className="min-w-0 flex-1 truncate text-secondary-label"> |
222 | | - <ReactMarkdown remarkPlugins={[remarkGfm, remarkQuestionPreview]}> |
223 | | - {activeQuestion.question} |
224 | | - </ReactMarkdown> |
| 224 | + <span |
| 225 | + id={previewId} |
| 226 | + inert |
| 227 | + className="min-w-0 flex-1 text-secondary-label" |
| 228 | + data-pending-user-input-preview |
| 229 | + > |
| 230 | + <ChatMarkdown |
| 231 | + text={activeQuestion.question} |
| 232 | + cwd={cwd} |
| 233 | + threadRef={threadRef} |
| 234 | + className="line-clamp-1" |
| 235 | + /> |
225 | 236 | </span> |
226 | 237 | ) : null} |
227 | 238 | </ComposerBanner.Content> |
228 | | - <ComposerBanner.Actions> |
| 239 | + <ComposerBanner.Actions className="pointer-events-none relative"> |
229 | 240 | {prompt.questions.length > 1 ? ( |
230 | 241 | <span className="text-3xs font-medium text-muted-foreground tabular-nums"> |
231 | 242 | {questionIndex + 1}/{prompt.questions.length} |
232 | 243 | </span> |
233 | 244 | ) : null} |
234 | 245 | <ComposerBanner.ToggleIcon expanded={!isCollapsed} /> |
235 | 246 | {prompt.dismissible ? ( |
236 | | - // Sits inside the trigger button, so stop the click from toggling |
237 | | - // the disclosure. Dismiss closes the question without a reply. |
238 | 247 | <ComposerBanner.Dismiss |
239 | | - render={<span role="button" tabIndex={0} />} |
| 248 | + className="pointer-events-auto" |
240 | 249 | aria-label="Dismiss question without answering" |
241 | 250 | title="Dismiss question without answering" |
242 | 251 | disabled={isResponding} |
243 | 252 | data-pending-user-input-dismiss |
244 | | - onClick={(event) => { |
245 | | - event.preventDefault(); |
246 | | - event.stopPropagation(); |
247 | | - onDismiss(prompt.requestId); |
248 | | - }} |
249 | | - onKeyDown={(event) => { |
250 | | - if (event.key !== "Enter" && event.key !== " ") return; |
251 | | - event.preventDefault(); |
252 | | - event.stopPropagation(); |
253 | | - onDismiss(prompt.requestId); |
254 | | - }} |
| 253 | + onClick={() => onDismiss(prompt.requestId)} |
255 | 254 | /> |
256 | 255 | ) : null} |
257 | 256 | </ComposerBanner.Actions> |
258 | | - </CollapsibleTrigger> |
| 257 | + </ComposerBanner.Row> |
259 | 258 | <CollapsiblePanel> |
260 | 259 | <ComposerBanner.Scroll> |
261 | 260 | <ComposerBanner.Body className="pe-1 pb-1 wrap-anywhere"> |
@@ -345,23 +344,3 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard( |
345 | 344 | </Collapsible> |
346 | 345 | ); |
347 | 346 | }); |
348 | | - |
349 | | -function remarkQuestionPreview() { |
350 | | - return (tree: Root) => { |
351 | | - const text = (node: Root | RootContent): string => { |
352 | | - if (node.type === "html" || node.type === "definition") return ""; |
353 | | - if ("alt" in node) return node.alt ?? ""; |
354 | | - if ("value" in node) return node.value; |
355 | | - if ("children" in node) { |
356 | | - const separator = ["root", "blockquote", "list", "listItem", "table", "tableRow"].includes( |
357 | | - node.type, |
358 | | - ) |
359 | | - ? " " |
360 | | - : ""; |
361 | | - return node.children.map(text).join(separator); |
362 | | - } |
363 | | - return node.type === "break" ? " " : ""; |
364 | | - }; |
365 | | - tree.children = [{ type: "text", value: text(tree).replace(/\s+/g, " ").trim() }]; |
366 | | - }; |
367 | | -} |
0 commit comments