Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions app/src/components/channels/chat-transcript.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ import { splitChatOrigin } from "./chat-origin";
import type { QueuedMessage } from "./composer";
import { transcriptColumn } from "./conversation-columns";
import { FileTypeIcon } from "./file-icon";
import { FoldedMessage } from "./folded-message";
import { FollowScroll, RESUME_PX, useFollowIntent } from "./follow-intent";
import { LateRenderersContext, LateRenderersWatch } from "./late-renderers";
import { usePacedReveal } from "./paced-reveal";
Expand Down Expand Up @@ -1311,30 +1312,33 @@ const TranscriptMessage = memo(function TranscriptMessage({
// emphasis. The chip is the one exception, and it is not reformatting: it is drawing
// the thing that was already a chip in the composer as a chip here too, so the
// transcript shows a skill was used rather than a slash that was typed.
<span className="whitespace-pre-wrap">
{origin && (
<span className="mr-1.5 inline-flex items-center rounded bg-foreground/10 px-1.5 py-0.5 align-middle text-foreground/70 text-xs">
via {origin.via}
</span>
)}
{invoked ? (
<>
{/*
* The same icon the sidebar uses for Skills, so the badge says WHAT KIND of
* thing was invoked before it says which one. `inline-flex` with
* `align-middle` rather than a block: this sits mid-sentence, and a badge that
* breaks the line it is in reads as a separate message.
*/}
<span className="mr-1 inline-flex items-center gap-1 rounded bg-foreground/10 px-1.5 py-0.5 align-middle text-foreground/80 text-xs">
<IconBox className="size-3 shrink-0" />
{invoked.chip}
// A long one folds to its first lines (see `FoldedMessage`).
<FoldedMessage>
<span className="whitespace-pre-wrap">
{origin && (
<span className="mr-1.5 inline-flex items-center rounded bg-foreground/10 px-1.5 py-0.5 align-middle text-foreground/70 text-xs">
via {origin.via}
</span>
{invoked.rest}
</>
) : (
said
)}
</span>
)}
{invoked ? (
<>
{/*
* The same icon the sidebar uses for Skills, so the badge says WHAT KIND of
* thing was invoked before it says which one. `inline-flex` with
* `align-middle` rather than a block: this sits mid-sentence, and a badge that
* breaks the line it is in reads as a separate message.
*/}
<span className="mr-1 inline-flex items-center gap-1 rounded bg-foreground/10 px-1.5 py-0.5 align-middle text-foreground/80 text-xs">
<IconBox className="size-3 shrink-0" />
{invoked.chip}
</span>
{invoked.rest}
</>
) : (
said
)}
</span>
</FoldedMessage>
) : (
/*
* A Bot's prose is markdown, and it arrives in pieces.
Expand Down
105 changes: 105 additions & 0 deletions app/src/components/channels/folded-message.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
type ReactNode,
useId,
useLayoutEffect,
useRef,
useState,
} from "react";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";

/** How many lines of a long message show while it is folded. */
export const FOLD_LINES = 8;

/**
* How many lines more than `FOLD_LINES` a message needs before it folds. A message one line too long
* would fold to hide that one line, and "Show more" would cost more room than it saves.
*/
export const FOLD_SLACK_LINES = 2;

/** Used when the browser gives no line height to measure with, as in a test's DOM. */
const FALLBACK_LINE_HEIGHT = 24;

/**
* A person's message, folded to its first lines when it is long, with Show more and Show less.
*
* A pasted prompt of forty lines pushed the Bot's answer below the screen, and the person scrolled
* past their own words to read it every time. Claude and ChatGPT fold a long message the same way.
*
* WHAT IS LONG is measured, not counted: lines are what the screen draws, so a message is long by its
* height at the width it has, and is measured again when that width changes. The text is all there
* while folded, only clipped (`max-h-[8lh]`, eight of its own lines), so a screen reader reads it
* all, Copy copies it all, and the browser's find reaches it: find scrolls the clipped box to show a
* match, and a scroll of a folded box unfolds it. The button is a real button and says, with
* `aria-expanded`, which way it went.
*/
export function FoldedMessage({ children }: { children: ReactNode }) {
const id = useId();
const body = useRef<HTMLDivElement>(null);
const toggle = useRef<HTMLButtonElement>(null);
const [long, setLong] = useState(false);
const [open, setOpen] = useState(false);

// Before paint, so a long message never shows whole for a frame and then jumps.
useLayoutEffect(() => {
const element = body.current;
if (!element) return;
const measure = () => {
const line = Number.parseFloat(getComputedStyle(element).lineHeight);
const lineHeight = line > 0 ? line : FALLBACK_LINE_HEIGHT;
// The whole text's height, folded or not: clipping leaves `scrollHeight` alone.
setLong(
element.scrollHeight > lineHeight * (FOLD_LINES + FOLD_SLACK_LINES),
);
};
measure();
if (typeof ResizeObserver === "undefined") return;
const observer = new ResizeObserver(measure);
observer.observe(element);
return () => observer.disconnect();
}, []);

const folded = long && !open;

return (
<>
<div
className={cn(folded && "max-h-[8lh] overflow-hidden mask-b-from-75%")}
data-folded={folded ? "" : undefined}
data-slot="folded-message"
id={id}
onScroll={(event) => {
// The browser's find scrolled the clipped text to a match: show the whole message.
if (!folded) return;
event.currentTarget.scrollTop = 0;
setOpen(true);
}}
ref={body}
>
{children}
</div>
{long ? (
<Button
aria-controls={id}
aria-expanded={open}
className="-ml-2"
data-slot="folded-message-toggle"
onClick={() => {
setOpen(!open);
// Folding a long message moves its button up: bring it back into view.
if (open)
requestAnimationFrame(() =>
toggle.current?.scrollIntoView?.({ block: "nearest" }),
);
}}
ref={toggle}
size="xs"
type="button"
variant="link"
>
{open ? "Show less" : "Show more"}
</Button>
) : null}
</>
);
}
25 changes: 21 additions & 4 deletions app/src/lib/markdown-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ const MENU_ITEM =
*/
const CELLS_AT_TOP = "[&_td]:align-top";

/*
* The second change: Copy, Download and Full screen show while the pointer is on the table or focus
* is in it, as a reply's own Copy shows on hover, so a reply with three tables isn't nine icons at
* rest. Where there is no hover (a touch screen), they always show. They fade rather than leave, so
* their row keeps its room and the table doesn't move when they appear, and they stay in the tab
* order: focus on one shows them all.
*
* An open Copy or Download menu keeps them shown (`has-[[data-slot=markdown-table-menu]]`), with the
* pointer anywhere. Focus alone doesn't: Safari doesn't focus a button on a click, so there the menu
* faded away with its buttons as soon as the pointer left the table.
*/
const ACTIONS_ON_HOVER =
"transition-opacity focus-within:opacity-100 motion-reduce:transition-none [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-hover/table:opacity-100 has-[[data-slot=markdown-table-menu]]:opacity-100";

function Icon({ d, ...props }: SVGProps<SVGSVGElement> & { d: string }) {
return (
<svg
Expand Down Expand Up @@ -308,7 +322,7 @@ function CopyMenu() {
<Icon d={copied ? ICONS.check : ICONS.copy} height={14} width={14} />
</button>
{open ? (
<div className={MENU}>
<div className={MENU} data-slot="markdown-table-menu">
<button
className={MENU_ITEM}
onClick={() => void copy("markdown")}
Expand Down Expand Up @@ -367,7 +381,7 @@ function DownloadMenu() {
<Icon d={ICONS.download} />
</button>
{open ? (
<div className={MENU}>
<div className={MENU} data-slot="markdown-table-menu">
<button
className={MENU_ITEM}
onClick={() => download("csv")}
Expand Down Expand Up @@ -556,12 +570,15 @@ export function MarkdownTable({

return (
<div
className="my-4 flex flex-col gap-2 rounded-lg border border-border bg-sidebar p-2"
className="group/table my-4 flex flex-col gap-2 rounded-lg border border-border bg-sidebar p-2"
data-streamdown="table-wrapper"
>
{showCopy || showDownload || showFullscreen ? (
<div
className="flex items-center justify-end gap-1"
className={cn(
"flex items-center justify-end gap-1",
ACTIONS_ON_HOVER,
)}
data-slot="markdown-table-actions"
>
{showCopy ? <CopyMenu /> : null}
Expand Down
Loading
Loading