Skip to content

Commit e5efee2

Browse files
t3dotggclaude
andcommitted
fix(web): "Show full message" only appears when it at least doubles the message
The fold was decided from text length but clipped at a fixed height, so it often hid only a few lines. It is now decided from the rendered height: a message folds only when it is at least twice the folded height. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 54a5c51 commit e5efee2

6 files changed

Lines changed: 72 additions & 70 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,25 @@ describe("MessagesTimeline messages", () => {
482482
expect(maxNestedCommits).toBeLessThan(20);
483483
});
484484

485+
it("folds a user message only when the fold hides at least as much as it shows", async () => {
486+
setup();
487+
const lines = (count: number) =>
488+
Array.from({ length: count }, (_, index) => `Line ${index}`).join("\n");
489+
await using screen = await renderTimeline([
490+
message(lines(12), {}, "medium"),
491+
message(lines(60), {}, "long"),
492+
]);
493+
await vi.waitFor(() => expect(screen.buttons("Show full message")).toHaveLength(1));
494+
const [medium, long] = [
495+
...screen.container.querySelectorAll<HTMLElement>("[data-user-message-body]"),
496+
];
497+
expect(medium!.dataset.userMessageCollapsible).toBe("false");
498+
expect(medium!.textContent).toContain("Line 11");
499+
const foldedHeight = long!.offsetHeight;
500+
await click(screen.button("Show full message"));
501+
expect(long!.offsetHeight).toBeGreaterThanOrEqual(foldedHeight * 2);
502+
});
503+
485504
it("keeps the first and last message headings reachable from either end", async () => {
486505
setup();
487506
const props = baseProps();

‎apps/web/src/components/chat/MessagesTimeline.logic.test.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ import {
2121
import { makeStreamingTimelineFixture } from "../../test-fixtures";
2222
import type { TurnDiffSummary } from "../../types";
2323
import { describe, expect, it } from "vite-plus/test";
24-
import { EnvironmentId, MessageId, RunId } from "@t3tools/contracts";
25-
import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
24+
import { MessageId, RunId } from "@t3tools/contracts";
2625
import {
2726
computeStableMessagesTimelineRows,
2827
deriveMessagesTimelineRows,
2928
timelineTurnFoldRunIdsByEntryId,
3029
liveAnswerPreview,
3130
deriveMessagesTimelineRowsWithState,
32-
shouldCollapseUserMessage,
3331
resolveAssistantMessageCopyState,
3432
type MessagesTimelineRow,
3533
workEntryDisplayLabel,
@@ -4703,24 +4701,3 @@ describe("subagent continued by a later turn", () => {
47034701
]);
47044702
});
47054703
});
4706-
4707-
describe("shouldCollapseUserMessage", () => {
4708-
it("measures a quote chip by its label, not its encoded link", () => {
4709-
const quote = "A long assistant paragraph that the user quoted. ".repeat(40);
4710-
const citation = serializeAssistantCitation({
4711-
version: 1,
4712-
environmentId: EnvironmentId.make("environment"),
4713-
threadId: ThreadId.make("thread"),
4714-
messageId: MessageId.make("source"),
4715-
text: quote,
4716-
comment: "Why does this matter?",
4717-
start: 0,
4718-
end: quote.length,
4719-
prefix: "",
4720-
suffix: "",
4721-
});
4722-
4723-
expect(shouldCollapseUserMessage(`${citation} Can you expand on this?`)).toBe(false);
4724-
expect(shouldCollapseUserMessage(`${citation} ${"More text. ".repeat(60)}`)).toBe(true);
4725-
});
4726-
});

‎apps/web/src/components/chat/MessagesTimeline.logic.ts‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { resolveUserMessagePresentation } from "@t3tools/client-runtime/user-mes
33
import { isLiveSubagentTurnItem } from "@t3tools/client-runtime/state/subagentRuntime";
44
import * as DateTime from "effect/DateTime";
55
import * as Equal from "effect/Equal";
6-
import {
7-
assistantCitationLabel,
8-
collectAssistantCitations,
9-
} from "@t3tools/shared/assistantCitations";
106
import { shallow } from "zustand/vanilla/shallow";
117
import { claudeResumeCompactionAge } from "@t3tools/shared/claudeCompaction";
128
import {
@@ -2690,33 +2686,6 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
26902686
}
26912687
}
26922688

2693-
const MAX_COLLAPSED_USER_MESSAGE_LINES = 8;
2694-
const MAX_COLLAPSED_USER_MESSAGE_LENGTH = 600;
2695-
const MARKDOWN_LINK = /!?\[([^\]\n]*)\]\((?:<[^>\n]*>|[^\s)]*)\)/g;
2696-
2697-
function visibleUserMessageText(text: string): string {
2698-
const linkLabels = (segment: string) => segment.replace(MARKDOWN_LINK, "$1");
2699-
let visible = "";
2700-
let cursor = 0;
2701-
for (const match of collectAssistantCitations(text)) {
2702-
visible += linkLabels(text.slice(cursor, match.start)) + assistantCitationLabel(match.citation);
2703-
cursor = match.end;
2704-
}
2705-
return visible + linkLabels(text.slice(cursor));
2706-
}
2707-
2708-
export function shouldCollapseUserMessage(text: string): boolean {
2709-
const visible = visibleUserMessageText(text);
2710-
if (visible.trim().length === 0) {
2711-
return false;
2712-
}
2713-
2714-
return (
2715-
visible.length > MAX_COLLAPSED_USER_MESSAGE_LENGTH ||
2716-
visible.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES
2717-
);
2718-
}
2719-
27202689
export function handoffRunsSignature(runs: ReadonlyArray<HandoffTimelineRun>): string {
27212690
return runs
27222691
.map(

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ import { MessageCopyButton } from "./MessageCopyButton";
5757
import { promptLinkFor } from "../promptLinks/copyPromptLink";
5858
import { PierreEntryIcon } from "./PierreEntryIcon";
5959
import { formatBytes } from "../../lib/formatBytes";
60-
import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
6160
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6261
import {
6362
asKnownContextRecord,
@@ -80,6 +79,7 @@ import { markdownProseDirection } from "~/textDirection";
8079
import { useMediaQuery } from "~/hooks/useMediaQuery";
8180
import { formatChatTimestampTooltip, formatDayAwareTimestamp } from "../../timestampFormat";
8281
import { WorkflowOpeningBlock } from "./WorkflowOpeningBlock";
82+
import { FOLDED_HEIGHT_REM, useTallEnoughToFold } from "./useTallEnoughToFold";
8383
import { TimelineRowCtx, TimelineRowActivityCtx, type TimelineRow } from "./timelineRowContext";
8484
import {
8585
MESSAGE_HEADING_LEVEL,
@@ -1130,8 +1130,9 @@ const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(prop
11301130
const revealForFind = () => setExpanded(true);
11311131
const revealRef = findRevealRef(revealForFind);
11321132
const hasVisibleBody = props.text.trim().length > 0;
1133-
const canCollapse =
1134-
props.collapsible !== false && hasVisibleBody && shouldCollapseUserMessage(props.text);
1133+
const { ref: contentRef, tallEnough: canCollapse } = useTallEnoughToFold(
1134+
props.collapsible !== false && hasVisibleBody,
1135+
);
11351136
const isCollapsed = canCollapse && !expanded;
11361137
const showCollapseControl = canCollapse;
11371138

@@ -1140,7 +1141,7 @@ const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(prop
11401141
{hasVisibleBody ? (
11411142
<div
11421143
ref={revealRef}
1143-
className={cn("relative", isCollapsed && "max-h-44 overflow-hidden")}
1144+
className={cn("relative", isCollapsed && "overflow-hidden")}
11441145
data-user-message-body="true"
11451146
data-thread-find-text="true"
11461147
data-thread-find-fold={isCollapsed ? "" : undefined}
@@ -1150,18 +1151,21 @@ const CollapsibleUserMessageBody = memo(function CollapsibleUserMessageBody(prop
11501151
style={
11511152
isCollapsed
11521153
? {
1154+
maxHeight: `${FOLDED_HEIGHT_REM}rem`,
11531155
WebkitMaskImage: COLLAPSED_USER_MESSAGE_FADE_MASK,
11541156
maskImage: COLLAPSED_USER_MESSAGE_FADE_MASK,
11551157
}
11561158
: undefined
11571159
}
11581160
>
1159-
<UserMessageBody
1160-
text={props.text}
1161-
renderContextReference={props.renderContextReference}
1162-
skills={props.skills}
1163-
markdownCwd={props.markdownCwd}
1164-
/>
1161+
<div ref={contentRef}>
1162+
<UserMessageBody
1163+
text={props.text}
1164+
renderContextReference={props.renderContextReference}
1165+
skills={props.skills}
1166+
markdownCwd={props.markdownCwd}
1167+
/>
1168+
</div>
11651169
</div>
11661170
) : null}
11671171
{showCollapseControl ? (

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Button } from "../ui/button";
2323
import { Tooltip, TooltipTrigger } from "../ui/tooltip";
2424
import { AgentElapsed } from "./AgentElapsed";
2525
import { findRevealRef } from "./markdownFindContext";
26-
import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
26+
import { FOLDED_HEIGHT_REM, useTallEnoughToFold } from "./useTallEnoughToFold";
2727
import { SubagentTooltipContent } from "./SubagentTooltipContent";
2828
import { EnvironmentSubagentAvatar } from "./V2LifecycleRow";
2929

@@ -204,22 +204,28 @@ function WorkflowMemberRow(props: {
204204
function WorkflowScript(props: { readonly script: string }) {
205205
const [expanded, setExpanded] = useState(false);
206206
const revealRef = findRevealRef(() => setExpanded(true));
207-
const canFold = shouldCollapseUserMessage(props.script);
207+
const { ref: contentRef, tallEnough: canFold } = useTallEnoughToFold();
208208
const folded = canFold && !expanded;
209209
return (
210210
<div>
211211
<div
212212
ref={revealRef}
213-
className={cn("relative", folded && "max-h-44 overflow-hidden")}
213+
className={cn("relative", folded && "overflow-hidden")}
214214
data-thread-find-text="true"
215215
data-thread-find-fold={folded ? "" : undefined}
216216
style={
217217
folded
218-
? { WebkitMaskImage: FOLDED_SCRIPT_MASK, maskImage: FOLDED_SCRIPT_MASK }
218+
? {
219+
maxHeight: `${FOLDED_HEIGHT_REM}rem`,
220+
WebkitMaskImage: FOLDED_SCRIPT_MASK,
221+
maskImage: FOLDED_SCRIPT_MASK,
222+
}
219223
: undefined
220224
}
221225
>
222-
<ChatMarkdown text={scriptMarkdown(props.script)} cwd={undefined} />
226+
<div ref={contentRef}>
227+
<ChatMarkdown text={scriptMarkdown(props.script)} cwd={undefined} />
228+
</div>
223229
</div>
224230
{canFold ? (
225231
<div className="-ms-1">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useLayoutEffect, useRef, useState } from "react";
2+
3+
import { observeResize } from "~/lib/observeResize";
4+
5+
export const FOLDED_HEIGHT_REM = 11;
6+
7+
/**
8+
* Put `ref` on the unclipped content inside a fold. `tallEnough` is true when that content is at
9+
* least twice the folded height, so opening a fold always at least doubles what was on screen.
10+
*/
11+
export function useTallEnoughToFold(enabled = true) {
12+
const ref = useRef<HTMLDivElement>(null);
13+
const [tallEnough, setTallEnough] = useState(false);
14+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the fold depends on the rendered height, which only the DOM knows
15+
useLayoutEffect(() => {
16+
const content = ref.current;
17+
if (!enabled || !content) return;
18+
const measure = () => {
19+
const remPx = Number.parseFloat(getComputedStyle(document.documentElement).fontSize) || 16;
20+
setTallEnough(content.offsetHeight >= FOLDED_HEIGHT_REM * 2 * remPx);
21+
};
22+
// Measured here too: waiting for the observer would paint one unfolded frame.
23+
measure();
24+
return observeResize(content, measure);
25+
}, [enabled]);
26+
return { ref, tallEnough: enabled && tallEnough };
27+
}

0 commit comments

Comments
 (0)