Skip to content

Commit 375d24c

Browse files
Umais-Adeedclaude
andcommitted
fix(mobile): chat folds stay closed until tapped, as on web and desktop
Chat messages shared the Markdown conversion that flattens <details> for pull request descriptions, so every fold in an agent reply rendered open. Split top-level folds out of chat Markdown and render each as a tappable summary over its body. Pull request descriptions keep the flattening. Fixes #28059 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ffaa1db commit 375d24c

4 files changed

Lines changed: 207 additions & 7 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ import { FONT_FAMILIES } from "../../lib/fontFamilies";
148148
import { scopedThreadKey } from "../../lib/scopedEntities";
149149
import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic";
150150
import { downloadAndShareAttachment } from "../../lib/attachmentDownload";
151+
import { splitMarkdownDetails } from "../../lib/markdownDetails";
151152
import { hasWideMarkdownBlock } from "../../lib/wideMarkdownBlocks";
152153
import {
153154
SelectableMarkdownText,
@@ -1204,21 +1205,87 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
12041205
/>
12051206
);
12061207
}
1207-
if (segment.markdown.trim().length === 0) return null;
1208+
return (
1209+
<AssistantMarkdownBlocks
1210+
key={`markdown:${segment.sourceOffset}`}
1211+
markdown={segment.markdown}
1212+
markdownStyles={props.markdownStyles}
1213+
linkHandlers={linkHandlers}
1214+
renderImage={props.renderImage}
1215+
skills={props.skills}
1216+
/>
1217+
);
1218+
});
1219+
});
12081220

1209-
const markdown = renderCodexInlineDirectivesAsMarkdown(segment.markdown);
1221+
function AssistantMarkdownBlocks(props: {
1222+
readonly markdown: string;
1223+
readonly markdownStyles: MarkdownStyleSet;
1224+
readonly linkHandlers: MarkdownLinkHandlers;
1225+
readonly renderImage: MarkdownImageRenderer;
1226+
readonly skills?: ReadonlyArray<SelectableMarkdownSkill> | undefined;
1227+
}) {
1228+
const blocks = useMemo(() => splitMarkdownDetails(props.markdown), [props.markdown]);
1229+
return blocks.map((block) => {
1230+
if (block.kind === "details") {
1231+
return (
1232+
<MarkdownDetailsFold
1233+
key={`details:${block.sourceOffset}`}
1234+
summary={block.summary}
1235+
initiallyOpen={block.open}
1236+
>
1237+
<AssistantMarkdownBlocks {...props} markdown={block.body} />
1238+
</MarkdownDetailsFold>
1239+
);
1240+
}
1241+
if (block.markdown.trim().length === 0) return null;
12101242
return (
12111243
<SelectableMarkdownText
1212-
key={`markdown:${segment.sourceOffset}`}
1213-
markdown={markdown}
1244+
key={`markdown:${block.sourceOffset}`}
1245+
markdown={renderCodexInlineDirectivesAsMarkdown(block.markdown)}
12141246
skills={props.skills}
12151247
textStyle={props.markdownStyles.nativeTextStyle}
1216-
{...linkHandlers}
1248+
{...props.linkHandlers}
12171249
renderImage={props.renderImage}
12181250
/>
12191251
);
12201252
});
1221-
});
1253+
}
1254+
1255+
function MarkdownDetailsFold(props: {
1256+
readonly summary: string;
1257+
readonly initiallyOpen: boolean;
1258+
readonly children: ReactNode;
1259+
}) {
1260+
const [expanded, setExpanded] = useState(props.initiallyOpen);
1261+
const iconSubtleColor = useUniwindTheme()["--color-icon-subtle"];
1262+
return (
1263+
<View className="my-2 border-y border-border">
1264+
<Pressable
1265+
accessibilityRole="button"
1266+
accessibilityLabel={props.summary}
1267+
accessibilityState={{ expanded }}
1268+
hitSlop={4}
1269+
className="min-h-10 flex-row items-center gap-2 py-2"
1270+
onPress={() => {
1271+
void Haptics.selectionAsync();
1272+
setExpanded((value) => !value);
1273+
}}
1274+
>
1275+
<ThreadDisclosureChevron
1276+
expanded={expanded}
1277+
collapsedDirection="right"
1278+
size={12}
1279+
tintColor={iconSubtleColor}
1280+
/>
1281+
<Text className="min-w-0 shrink font-t3-medium text-sm text-foreground">
1282+
{props.summary}
1283+
</Text>
1284+
</Pressable>
1285+
{expanded ? <View className="pb-3 pl-5">{props.children}</View> : null}
1286+
</View>
1287+
);
1288+
}
12221289

12231290
function useMarkdownStyles(): MarkdownStyleSets {
12241291
const { appearance } = useAppearancePreferences();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { splitMarkdownDetails } from "./markdownDetails";
4+
5+
describe("splitMarkdownDetails", () => {
6+
// regression: #28059 mobile chat showed every <details> fold open
7+
it("splits a top-level fold into its summary and hidden body", () => {
8+
const markdown =
9+
"Short answer.\n\n<details>\n<summary>Long details</summary>\n\nA long paragraph.\n\n</details>\n\nAfter.";
10+
expect(splitMarkdownDetails(markdown)).toEqual([
11+
{ kind: "markdown", markdown: "Short answer.\n\n", sourceOffset: 0 },
12+
{
13+
kind: "details",
14+
summary: "Long details",
15+
body: "\n\nA long paragraph.\n\n",
16+
open: false,
17+
sourceOffset: 15,
18+
},
19+
{ kind: "markdown", markdown: "\n\nAfter.", sourceOffset: 87 },
20+
]);
21+
});
22+
23+
it("keeps nested folds in the body and honours the open attribute", () => {
24+
const markdown =
25+
"<details open>\n<summary>Outer &amp; <code>more</code></summary>\n\n<details>\n<summary>Inner</summary>\n\nx\n\n</details>\n\n</details>";
26+
const [fold] = splitMarkdownDetails(markdown);
27+
expect(fold).toMatchObject({ kind: "details", summary: "Outer & more", open: true });
28+
expect(fold?.kind === "details" && splitMarkdownDetails(fold.body)).toMatchObject([
29+
{ kind: "markdown" },
30+
{ kind: "details", summary: "Inner", body: "\n\nx\n\n" },
31+
{ kind: "markdown" },
32+
]);
33+
});
34+
35+
it("leaves folds in code and inline tags as markdown", () => {
36+
for (const markdown of [
37+
"```html\n<details>\n<summary>Code</summary>\n</details>\n```",
38+
"Use `<details>` for folds.",
39+
"Text <details><summary>inline</summary>body</details> more",
40+
]) {
41+
expect(splitMarkdownDetails(markdown)).toEqual([
42+
{ kind: "markdown", markdown, sourceOffset: 0 },
43+
]);
44+
}
45+
});
46+
47+
it("folds the rest of a reply whose closing tag has not streamed yet", () => {
48+
expect(splitMarkdownDetails("<details>\n<summary>Plan</summary>\n\nStep one")).toEqual([
49+
{ kind: "details", summary: "Plan", body: "\n\nStep one", open: false, sourceOffset: 0 },
50+
]);
51+
});
52+
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { unified } from "unified";
2+
import remarkParse from "remark-parse";
3+
4+
import { decodeHtmlEntities } from "./markdownDocument";
5+
6+
export type MarkdownDetailsSegment =
7+
| { readonly kind: "markdown"; readonly markdown: string; readonly sourceOffset: number }
8+
| {
9+
readonly kind: "details";
10+
readonly summary: string;
11+
readonly body: string;
12+
readonly open: boolean;
13+
readonly sourceOffset: number;
14+
};
15+
16+
const parser = unified().use(remarkParse);
17+
const DETAILS_TAG = /<(\/?)details\b[^>]*>/gi;
18+
const SUMMARY = /^\s*<summary\b[^>]*>([\s\S]*?)<\/summary\s*>/i;
19+
20+
/**
21+
* Splits top-level `<details>` blocks out of chat Markdown so they can render as folds.
22+
* Tags inside code or inline in a paragraph stay Markdown. A fold whose closing tag has
23+
* not streamed yet runs to the end.
24+
*/
25+
export function splitMarkdownDetails(markdown: string): ReadonlyArray<MarkdownDetailsSegment> {
26+
if (!/<details\b/i.test(markdown)) return [{ kind: "markdown", markdown, sourceOffset: 0 }];
27+
28+
const folds: Array<{ start: number; openTagEnd: number; end: number; open: boolean }> = [];
29+
let depth = 0;
30+
let current: { start: number; openTagEnd: number; open: boolean } | null = null;
31+
for (const node of parser.parse(markdown).children) {
32+
if (node.type !== "html" || node.position?.start.offset === undefined) continue;
33+
const nodeStart = node.position.start.offset;
34+
for (const tag of node.value.matchAll(DETAILS_TAG)) {
35+
const start = nodeStart + tag.index;
36+
if (tag[1] !== "/") {
37+
if (depth === 0) {
38+
current = { start, openTagEnd: start + tag[0].length, open: /\sopen\b/i.test(tag[0]) };
39+
}
40+
depth += 1;
41+
} else if (depth > 0) {
42+
depth -= 1;
43+
if (depth === 0 && current) {
44+
folds.push({ ...current, end: start + tag[0].length });
45+
current = null;
46+
}
47+
}
48+
}
49+
}
50+
if (current) folds.push({ ...current, end: markdown.length });
51+
if (folds.length === 0) return [{ kind: "markdown", markdown, sourceOffset: 0 }];
52+
53+
const segments: MarkdownDetailsSegment[] = [];
54+
let cursor = 0;
55+
for (const fold of folds) {
56+
if (fold.start > cursor) {
57+
segments.push({
58+
kind: "markdown",
59+
markdown: markdown.slice(cursor, fold.start),
60+
sourceOffset: cursor,
61+
});
62+
}
63+
const closed = markdown.slice(fold.start, fold.end).match(/<\/details\s*>$/i);
64+
let body = markdown.slice(fold.openTagEnd, fold.end - (closed?.[0].length ?? 0));
65+
const summary = SUMMARY.exec(body);
66+
if (summary) body = body.slice(summary[0].length);
67+
const label = summary ? decodeHtmlEntities(summary[1]!.replace(/<[^>]*>/g, "")).trim() : "";
68+
segments.push({
69+
kind: "details",
70+
summary: label || "Details",
71+
body,
72+
open: fold.open,
73+
sourceOffset: fold.start,
74+
});
75+
cursor = fold.end;
76+
}
77+
if (cursor < markdown.length) {
78+
segments.push({ kind: "markdown", markdown: markdown.slice(cursor), sourceOffset: cursor });
79+
}
80+
return segments;
81+
}

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function decodeCodePoint(codePoint: number, entity: string): string {
3434
return String.fromCodePoint(codePoint);
3535
}
3636

37-
function decodeHtmlEntities(value: string): string {
37+
export function decodeHtmlEntities(value: string): string {
3838
return value.replace(
3939
/&(?:#(\d+)|#x([0-9a-f]+)|amp|apos|gt|lt|nbsp|quot);/gi,
4040
(entity, decimal: string | undefined, hexadecimal: string | undefined) => {

0 commit comments

Comments
 (0)