Skip to content
Merged
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
16 changes: 14 additions & 2 deletions apps/web/src/components/chat/MessageCopyButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@ import { memo } from "react";
import { CopyIcon, CheckIcon } from "lucide-react";
import { Button } from "../ui/button";
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { cn } from "~/lib/utils";

export const MessageCopyButton = memo(function MessageCopyButton({ text }: { text: string }) {
export const MessageCopyButton = memo(function MessageCopyButton({
text,
label = "message",
className,
}: {
text: string;
label?: string;
className?: string;
}) {
const { copyToClipboard, isCopied } = useCopyToClipboard();
const title = isCopied ? "Copied" : `Copy ${label}`;

return (
<Button
type="button"
size="xs"
variant="outline"
className={cn("gap-1.5", className)}
onClick={() => copyToClipboard(text)}
title="Copy message"
title={title}
aria-label={title}
>
{isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />}
</Button>
Expand Down
5 changes: 5 additions & 0 deletions apps/web/src/components/chat/MessagesTimeline.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { beforeAll, describe, expect, it, vi } from "vitest";
import { buildChatShortcutGuides } from "~/lib/chatShortcutGuidance";
import { I18nProvider } from "~/i18n/I18nProvider";

vi.mock("~/hooks/useFileViewNavigation", () => ({
useFileViewNavigation: () => () => {},
}));

function matchMedia() {
return {
matches: false,
Expand Down Expand Up @@ -263,6 +267,7 @@ describe("MessagesTimeline", () => {

expect(markup).toContain("Open diff");
expect(markup).toContain("Changed files (1)");
expect(markup).toContain("Copy response");
});

it("renders an open diff action when a turn diff exists but the file summary is empty", async () => {
Expand Down
192 changes: 101 additions & 91 deletions apps/web/src/components/chat/MessagesTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
row.message.role === "assistant" &&
(() => {
const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
const copyText = row.message.text.trim().length > 0 ? row.message.text : null;
return (
<>
{row.showCompletionDivider && (
Expand All @@ -554,106 +555,115 @@ export const MessagesTimeline = memo(function MessagesTimeline({
</div>
)}
<div className="min-w-0 px-1 py-0.5">
<ChatMarkdown
text={messageText}
cwd={markdownCwd}
isStreaming={Boolean(row.message.streaming)}
/>
{(() => {
const turnSummary = turnDiffSummaryByAssistantMessageId.get(row.message.id);
if (!turnSummary) return null;
const checkpointFiles = turnSummary.files;
const summaryStat = summarizeTurnDiffStats(checkpointFiles);
const changedFileCountLabel = String(checkpointFiles.length);
const allDirectoriesExpanded =
allDirectoriesExpandedByTurnId[turnSummary.turnId] ?? true;
const isFileSectionCollapsed =
collapsedFileSectionsByTurnId[turnSummary.turnId] ?? false;
return (
<div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
<div className="flex items-center justify-between gap-2">
{checkpointFiles.length > 0 ? (
<button
type="button"
className="group flex items-center gap-1.5 text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65 transition-colors duration-150 hover:text-muted-foreground/90"
onClick={() => onToggleFileSection(turnSummary.turnId)}
>
<ChevronRightIcon
aria-hidden="true"
className={cn(
"size-3 shrink-0 transition-transform duration-150",
!isFileSectionCollapsed && "rotate-90",
)}
/>
<span>Changed files ({changedFileCountLabel})</span>
{hasNonZeroStat(summaryStat) && (
<>
<span className="mx-1">•</span>
<DiffStatLabel
additions={summaryStat.additions}
deletions={summaryStat.deletions}
<div className="flex items-start gap-2">
<div className="min-w-0 flex-1">
<ChatMarkdown
text={messageText}
cwd={markdownCwd}
isStreaming={Boolean(row.message.streaming)}
/>
{(() => {
const turnSummary = turnDiffSummaryByAssistantMessageId.get(row.message.id);
if (!turnSummary) return null;
const checkpointFiles = turnSummary.files;
const summaryStat = summarizeTurnDiffStats(checkpointFiles);
const changedFileCountLabel = String(checkpointFiles.length);
const allDirectoriesExpanded =
allDirectoriesExpandedByTurnId[turnSummary.turnId] ?? true;
const isFileSectionCollapsed =
collapsedFileSectionsByTurnId[turnSummary.turnId] ?? false;
return (
<div className="mt-2 rounded-lg border border-border/80 bg-card/45 p-2.5">
<div className="flex items-center justify-between gap-2">
{checkpointFiles.length > 0 ? (
<button
type="button"
className="group flex items-center gap-1.5 text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65 transition-colors duration-150 hover:text-muted-foreground/90"
onClick={() => onToggleFileSection(turnSummary.turnId)}
>
<ChevronRightIcon
aria-hidden="true"
className={cn(
"size-3 shrink-0 transition-transform duration-150",
!isFileSectionCollapsed && "rotate-90",
)}
/>
</>
<span>Changed files ({changedFileCountLabel})</span>
{hasNonZeroStat(summaryStat) && (
<>
<span className="mx-1">•</span>
<DiffStatLabel
additions={summaryStat.additions}
deletions={summaryStat.deletions}
/>
</>
)}
</button>
) : (
<div className="flex items-center gap-1.5 text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65">
<EyeIcon className="size-3 shrink-0" />
<span>Diff available</span>
</div>
)}
</button>
) : (
<div className="flex items-center gap-1.5 text-[10px] uppercase tracking-[0.12em] text-muted-foreground/65">
<EyeIcon className="size-3 shrink-0" />
<span>Diff available</span>
<div className="flex items-center gap-1.5">
<Button
type="button"
size="xs"
variant="outline"
onClick={() => onOpenTurnDiff(turnSummary.turnId)}
>
Open diff
</Button>
{checkpointFiles.length > 0 && !isFileSectionCollapsed && (
<Button
type="button"
size="xs"
variant="outline"
onClick={() => onToggleAllDirectories(turnSummary.turnId)}
>
{allDirectoriesExpanded ? "Collapse all" : "Expand all"}
</Button>
)}
</div>
</div>
)}
<div className="flex items-center gap-1.5">
<Button
type="button"
size="xs"
variant="outline"
onClick={() => onOpenTurnDiff(turnSummary.turnId)}
>
Open diff
</Button>
{checkpointFiles.length > 0 && !isFileSectionCollapsed && (
<Button
type="button"
size="xs"
variant="outline"
onClick={() => onToggleAllDirectories(turnSummary.turnId)}
>
{allDirectoriesExpanded ? "Collapse all" : "Expand all"}
</Button>
<div className="mt-1.5">
<ChangedFilesTree
key={`changed-files-tree:${turnSummary.turnId}`}
turnId={turnSummary.turnId}
files={checkpointFiles}
allDirectoriesExpanded={allDirectoriesExpanded}
resolvedTheme={resolvedTheme}
cwd={markdownCwd}
onOpenTurnDiff={onOpenTurnDiff}
/>
</div>
)}
{checkpointFiles.length === 0 && (
<p className="mt-1.5 text-xs text-muted-foreground/75">
Open the diff to inspect changes when the file summary is unavailable.
</p>
)}
</div>
</div>
{checkpointFiles.length > 0 && !isFileSectionCollapsed && (
<div className="mt-1.5">
<ChangedFilesTree
key={`changed-files-tree:${turnSummary.turnId}`}
turnId={turnSummary.turnId}
files={checkpointFiles}
allDirectoriesExpanded={allDirectoriesExpanded}
resolvedTheme={resolvedTheme}
cwd={markdownCwd}
onOpenTurnDiff={onOpenTurnDiff}
/>
</div>
)}
{checkpointFiles.length === 0 && (
<p className="mt-1.5 text-xs text-muted-foreground/75">
Open the diff to inspect changes when the file summary is unavailable.
</p>
);
})()}
<p className="mt-1.5 text-[10px] text-muted-foreground/30">
{formatMessageMeta(
row.message.createdAt,
row.message.streaming
? formatElapsed(row.durationStart, nowIso)
: formatElapsed(row.durationStart, row.message.completedAt),
timestampFormat,
resolvedLocale,
)}
</p>
</div>
{copyText && (
<div className="flex shrink-0 items-start pt-0.5">
<MessageCopyButton text={copyText} label="response" />
</div>
);
})()}
<p className="mt-1.5 text-[10px] text-muted-foreground/30">
{formatMessageMeta(
row.message.createdAt,
row.message.streaming
? formatElapsed(row.durationStart, nowIso)
: formatElapsed(row.durationStart, row.message.completedAt),
timestampFormat,
resolvedLocale,
)}
</p>
</div>
</div>
</>
);
Expand Down
Loading