Skip to content

Commit e57ae5a

Browse files
authored
feat(web): pasted text, voice notes, snapshots and sending files get their own shapes (#27377)
1 parent 1da41f9 commit e57ae5a

7 files changed

Lines changed: 189 additions & 39 deletions

‎apps/web/src/components/ContextChip.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const contextChipVariants = cva(
3535
"doc-slides": "[--context-chip-accent:var(--context-chip-doc-slides)]",
3636
"doc-epub": "[--context-chip-accent:var(--context-chip-doc-epub)]",
3737
},
38+
shape: {
39+
inline: "",
40+
bubble: "w-64 gap-2 rounded-2xl bg-message px-3 py-2 text-sm text-message-foreground",
41+
},
3842
state: {
3943
unresolved: "data-[state=unresolved]:[--context-chip-accent:currentColor]",
4044
invalid:
@@ -49,11 +53,12 @@ type ContextChipKind = NonNullable<VariantProps<typeof contextChipVariants>["kin
4953
interface ContextChipProps extends useRender.ComponentProps<"span"> {
5054
kind?: ContextChipKind;
5155
state?: "unresolved" | "invalid";
56+
shape?: "inline" | "bubble";
5257
}
5358

54-
function ContextChip({ className, kind, state, render, ...props }: ContextChipProps) {
59+
function ContextChip({ className, kind, state, shape, render, ...props }: ContextChipProps) {
5560
const defaultProps = {
56-
className: cn(contextChipVariants({ kind, state }), className),
61+
className: cn(contextChipVariants({ kind, state, shape }), className),
5762
"data-slot": "context-chip",
5863
"data-state": state,
5964
};

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { onTestFinished } from "vite-plus/test";
33
import { page } from "vite-plus/test/browser";
44
import { stubGlobal } from "../../../test/render";
55
import {
6+
ComposerContextId,
67
EnvironmentId,
78
MessageId,
89
RunAttemptId,
@@ -32,6 +33,11 @@ import { toastManager } from "../ui/toast";
3233
import type { CitedHistory } from "./citationProcedure";
3334
import type { TimelineNavigation } from "./timelineViewport";
3435
import { rememberTimelinePosition } from "./timelineScrollAnchoring";
36+
import {
37+
COMPOSER_CONTEXT_CLIPBOARD_MIME,
38+
decodeComposerContextFragment,
39+
} from "@t3tools/shared/composerContextClipboard";
40+
import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences";
3541

3642
const environmentQuery = vi.hoisted(() => ({
3743
error: "Pull request not found",
@@ -642,6 +648,75 @@ describe("MessagesTimeline agent messages", () => {
642648
});
643649

644650
describe("MessagesTimeline attachments", () => {
651+
it.each(["image", "file"] as const)(
652+
"copies only matching context records for a wordless %s attachment",
653+
async (kind) => {
654+
const attachment = {
655+
type: kind,
656+
id: "media",
657+
name: kind === "image" ? "photo.png" : "clip.mp4",
658+
mimeType: kind === "image" ? "image/png" : "video/mp4",
659+
sizeBytes: 100,
660+
downloadable: false,
661+
};
662+
const record = {
663+
version: 1 as const,
664+
...(kind === "image" ? { kind: "image" as const } : { kind: "file" as const }),
665+
contextId: ComposerContextId.make("media-context"),
666+
label: attachment.name,
667+
attachmentId: attachment.id,
668+
name: attachment.name,
669+
mimeType: attachment.mimeType,
670+
sizeBytes: attachment.sizeBytes,
671+
};
672+
await using screen = await renderTimeline([message("", { attachments: [attachment] })]);
673+
expect(screen.buttons("Copy message")).toHaveLength(0);
674+
await screen.rerender(
675+
<MessagesTimeline
676+
{...baseProps()}
677+
timelineEntries={[
678+
message("", {
679+
attachments: [attachment],
680+
context: { version: 1, records: [{ ...record, attachmentId: "other" }] },
681+
}),
682+
]}
683+
/>,
684+
);
685+
expect(screen.buttons("Copy message")).toHaveLength(0);
686+
await screen.rerender(
687+
<MessagesTimeline
688+
{...baseProps()}
689+
timelineEntries={[
690+
message("", {
691+
attachments: [attachment],
692+
context: {
693+
version: 1,
694+
records: [
695+
record,
696+
{
697+
...record,
698+
contextId: ComposerContextId.make("other-context"),
699+
attachmentId: "other",
700+
},
701+
],
702+
},
703+
}),
704+
]}
705+
/>,
706+
);
707+
using write = vi.spyOn(navigator.clipboard, "write").mockResolvedValue();
708+
await click(screen.button("Copy message"));
709+
await vi.waitFor(() => expect(write).toHaveBeenCalledOnce());
710+
const [item] = write.mock.calls[0]![0];
711+
const text = await (await item!.getType("text/plain")).text();
712+
const fragment = await (await item!.getType(COMPOSER_CONTEXT_CLIPBOARD_MIME)).text();
713+
expect(
714+
collectComposerContextReferences(text).map((reference) => reference.contextId),
715+
).toEqual([record.contextId]);
716+
expect(decodeComposerContextFragment(fragment)?.records).toEqual([record]);
717+
},
718+
);
719+
645720
it.each(["loadedmetadata", "loadeddata", "canplay"])(
646721
"plays audio with unknown duration after %s and updates the total later",
647722
async (readyEvent) => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { type ChatImageAttachment } from "../../types";
1+
import { type ChatFileAttachment, type ChatImageAttachment } from "../../types";
22
import type { ComposerContextRecord, KnownComposerContextRecord } from "@t3tools/contracts";
33
import { asKnownContextRecord } from "~/lib/composerContextRecords";
44

5+
export function userMessageFileLabel(file: Pick<ChatFileAttachment, "name" | "source">) {
6+
return file.source?._tag === "pasted-text" ? "Pasted text" : file.name;
7+
}
8+
59
export function resolvePreviewAnnotationImage(input: {
610
record: Extract<KnownComposerContextRecord, { kind: "preview-annotation" }>;
711
recordsById: ReadonlyMap<string, ComposerContextRecord>;

0 commit comments

Comments
 (0)