Skip to content

Commit f9dea9f

Browse files
committed
fix(web): reuse question renderer for collapsed previews
1 parent 284dfd7 commit f9dea9f

2 files changed

Lines changed: 64 additions & 62 deletions

File tree

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.markdown.test.tsx‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,12 @@ describe("question Markdown", () => {
136136
await renderPanel();
137137
const toggle = element("[data-pending-user-input-toggle]", HTMLButtonElement);
138138
await act(async () => toggle.click());
139-
expect(toggle.textContent).toContain("Open Linear login link and confirm when ready.");
140-
expect(toggle.textContent).not.toContain("https://");
141-
expect(toggle.textContent).not.toContain("**");
142-
expect(toggle.querySelector("a")).toBeNull();
139+
const preview = element("[data-pending-user-input-preview]", HTMLSpanElement);
140+
expect(preview.textContent).toContain("Linear login link");
141+
expect(preview.textContent).not.toContain("https://");
142+
expect(preview.textContent).not.toContain("**");
143+
expect(preview.querySelector("a")?.closest("button")).toBeNull();
144+
expect(preview.hasAttribute("inert")).toBe(true);
143145
});
144146

145147
it("renders block Markdown in the expanded question", async () => {
@@ -174,8 +176,29 @@ describe("question Markdown", () => {
174176
]);
175177
const toggle = element("[data-pending-user-input-toggle]", HTMLButtonElement);
176178
await act(async () => toggle.click());
177-
expect(toggle.textContent).toContain("Open help and keep *literal*.");
178-
expect(toggle.textContent).not.toContain("long-destination");
179+
const preview = element("[data-pending-user-input-preview]", HTMLSpanElement);
180+
expect(preview.textContent).toContain("Open help and keep *literal*.");
181+
expect(preview.textContent).not.toContain("long-destination");
182+
});
183+
184+
it.each([
185+
"First<br>Second",
186+
"First<br />Second",
187+
"First<BR>Second",
188+
"<p>First</p><p>Second</p>",
189+
"First<div>Second</div>Third",
190+
"<em>First</em>Second",
191+
"<p>First &amp; second</p>",
192+
"<p>First</p><script>hidden()</script><p>Second</p>",
193+
])("uses the expanded renderer unchanged in the collapsed preview: %s", async (markdown) => {
194+
await renderPanel([{ ...question, question: markdown }]);
195+
const expanded = element(".chat-markdown", HTMLDivElement);
196+
const rendered = expanded.innerHTML;
197+
const toggle = element("[data-pending-user-input-toggle]", HTMLButtonElement);
198+
await act(async () => toggle.click());
199+
const preview = element("[data-pending-user-input-preview] .chat-markdown", HTMLDivElement);
200+
expect(preview.innerHTML).toBe(rendered);
201+
expect(preview.textContent).not.toContain("hidden()");
179202
});
180203

181204
it("renders option formatting without nesting links inside selection buttons", async () => {
@@ -206,7 +229,7 @@ describe("question Markdown", () => {
206229
it("submits the original option value after a formatted option is selected", async () => {
207230
await renderPanel();
208231
const button = [...container.querySelectorAll("button")].find(
209-
(candidate) => candidate.getAttribute("aria-labelledby") !== null,
232+
(candidate) => candidate.getAttribute("aria-pressed") !== null,
210233
);
211234
if (!button) throw new Error("Missing option selection button");
212235
await act(async () => button.click());
@@ -234,7 +257,7 @@ describe("question Markdown", () => {
234257

235258
it("toggles multi-select answers without advancing", async () => {
236259
await renderPanel([{ ...question, multiSelect: true }]);
237-
const button = element("button[aria-labelledby]", HTMLButtonElement);
260+
const button = element("button[aria-pressed]", HTMLButtonElement);
238261
await act(async () => button.click());
239262
expect(onToggleOption).toHaveBeenCalledWith("login", "**Done**");
240263
await act(async () => vi.advanceTimersByTime(200));
@@ -268,7 +291,7 @@ describe("question Markdown", () => {
268291

269292
it("does not select a formatted answer when responses are disabled", async () => {
270293
await renderPanel([question], true);
271-
const button = element("button[aria-labelledby]", HTMLButtonElement);
294+
const button = element("button[aria-pressed]", HTMLButtonElement);
272295
expect(button.disabled).toBe(true);
273296
await act(async () => button.click());
274297
expect(onToggleOption).not.toHaveBeenCalled();

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx‎

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts";
22
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";
63
import ChatMarkdown from "../ChatMarkdown";
74
import { type PendingUserInput } from "../../session-logic";
85
import {
@@ -88,6 +85,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
8885
const responseDisabled = disabled || isResponding || !canRespond;
8986
const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);
9087
const optionIdPrefix = useId();
88+
const headerId = `${optionIdPrefix}-header`;
89+
const previewId = `${optionIdPrefix}-preview`;
9190
const activeQuestion = progress.activeQuestion;
9291
const autoAdvanceTimerRef = useRef<number | null>(null);
9392
const onAdvanceRef = useRef(onAdvance);
@@ -205,57 +204,57 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
205204
setCollapsedQuestionId(open ? null : activeQuestion.id);
206205
}}
207206
>
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">
218221
{activeQuestion.header}
219222
</span>
220223
{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+
/>
225236
</span>
226237
) : null}
227238
</ComposerBanner.Content>
228-
<ComposerBanner.Actions>
239+
<ComposerBanner.Actions className="pointer-events-none relative">
229240
{prompt.questions.length > 1 ? (
230241
<span className="text-3xs font-medium text-muted-foreground tabular-nums">
231242
{questionIndex + 1}/{prompt.questions.length}
232243
</span>
233244
) : null}
234245
<ComposerBanner.ToggleIcon expanded={!isCollapsed} />
235246
{prompt.dismissible ? (
236-
// Sits inside the trigger button, so stop the click from toggling
237-
// the disclosure. Dismiss closes the question without a reply.
238247
<ComposerBanner.Dismiss
239-
render={<span role="button" tabIndex={0} />}
248+
className="pointer-events-auto"
240249
aria-label="Dismiss question without answering"
241250
title="Dismiss question without answering"
242251
disabled={isResponding}
243252
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)}
255254
/>
256255
) : null}
257256
</ComposerBanner.Actions>
258-
</CollapsibleTrigger>
257+
</ComposerBanner.Row>
259258
<CollapsiblePanel>
260259
<ComposerBanner.Scroll>
261260
<ComposerBanner.Body className="pe-1 pb-1 wrap-anywhere">
@@ -345,23 +344,3 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
345344
</Collapsible>
346345
);
347346
});
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

Comments
 (0)