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
37 changes: 37 additions & 0 deletions apps/web/src/components/ChatView.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,43 @@ it.each(["new", "existing"] as const)(
},
);

it("a file picked after the phone composer collapses still attaches", async () => {
const thread = threadWithHistory("thread-40993", 1);
using _environment = installFakeEnvironment([thread]);
await page.viewport(390, 844);
__setClientSettingsForTests({
...DEFAULT_CLIENT_SETTINGS,
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
});
const router = getRouter(
createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }),
);
await router.load();
await using _app = await render(<AppRoot router={router} />, {
// AppRoot brings its own router.
router: false,
});
using _uploads = acceptAttachmentUploads();
const editor = page.getByTestId("composer-editor");
await expect.element(page.getByText(`Answer 0 for ${thread.id}.`)).toBeVisible();
await userEvent.click(editor);
await expect.element(page.getByRole("button", { name: "Attach files" })).toBeVisible();
const input = document.querySelector<HTMLInputElement>(
'form[data-chat-composer-form] input[type="file"]',
);
expect(input).not.toBeNull();

// Opening the native picker blurs the editor, which collapses the phone composer.
(editor.element() as HTMLElement).blur();
await expect.element(page.getByRole("button", { name: "Attach files" })).not.toBeInTheDocument();
const files = new DataTransfer();
files.items.add(new File([PNG], "picked.png", { type: "image/png" }));
input!.files = files.files;
input!.dispatchEvent(new Event("change", { bubbles: true }));

await expect.element(page.getByRole("img", { name: "picked.png" })).toBeVisible();
});

it("opens the thread actions menu for an older settled thread the shell leaves out", async () => {
const thread = { ...threadWithHistory("older-settled", 1), olderSettled: true as const };
using _environment = installFakeEnvironment([thread]);
Expand Down
14 changes: 10 additions & 4 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ import {
} from "./ComposerCommandMenu";
import {
ComposerAttachButton,
ComposerAttachInput,
ComposerAttachmentFileList,
ComposerAttachmentMediaRow,
ComposerFilePreviewDialog,
Expand Down Expand Up @@ -1081,6 +1082,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
const composerEditorRef = useRef<ComposerPromptEditorHandle>(null);
const pasteAsTextShortcutUntilRef = useRef(0);
const composerFormRef = useRef<HTMLFormElement>(null);
const composerAttachInputRef = useRef<HTMLInputElement>(null);
const composerSurfaceRef = useRef<HTMLDivElement>(null);
const providerInputRejectedRef = useRef(false);
const composerSelectLockRef = useRef(false);
Expand Down Expand Up @@ -3551,10 +3553,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
/>
) : null;
const composerAttachButton = showComposerAttachAction ? (
<ComposerAttachButton
addComposerAttachments={addComposerAttachments}
focusComposer={focusComposer}
/>
<ComposerAttachButton onOpenPicker={() => composerAttachInputRef.current?.click()} />
) : null;
return (
<form
Expand Down Expand Up @@ -3655,6 +3654,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
onToggleMenu={toggleStashMenuStable}
/>
</ComposerBanner.Dock>
{showComposerAttachAction ? (
<ComposerAttachInput
ref={composerAttachInputRef}
addComposerAttachments={addComposerAttachments}
focusComposer={focusComposer}
/>
) : null}
<div className="relative">
<ComposerSurface.Main
ref={composerMainSurfaceRef}
Expand Down
88 changes: 47 additions & 41 deletions apps/web/src/components/chat/ComposerAttachmentStrip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
import { formatAttachmentSize } from "@t3tools/client-runtime/state/attachments";
import { T, useGT, useMessages } from "gt-react";
import { CircleAlertIcon, ImageIcon, PaperclipIcon, PlayIcon, XIcon } from "lucide-react";
import { type ComponentProps, useLayoutEffect, useRef } from "react";
import { type ComponentProps, type Ref, useLayoutEffect, useRef } from "react";

import {
type ComposerFileAttachment,
Expand Down Expand Up @@ -731,53 +731,59 @@ export function ComposerQueuedAttachmentRow({
);
}

export function ComposerAttachButton({
// Kept outside the composer footer: on mobile the picker blurs the editor and the
// footer unmounts, and a detached input never delivers its change event.
export function ComposerAttachInput({
ref,
addComposerAttachments,
focusComposer,
}: {
readonly ref: Ref<HTMLInputElement>;
readonly addComposerAttachments: (files: File[]) => Promise<boolean>;
readonly focusComposer: () => void;
}) {
return (
<input
ref={ref}
type="file"
multiple
className="hidden"
onChange={(event) => {
const files = Array.from(event.currentTarget.files ?? []);
event.currentTarget.value = "";
// Inserting a chip refocuses the editor after the draft renders;
// focusing synchronously here would report the editor's stale text
// over the prompt that was just written.
void addComposerAttachments(files).then((inserted) => {
if (!inserted) focusComposer();
});
}}
/>
);
}

export function ComposerAttachButton({ onOpenPicker }: { readonly onOpenPicker: () => void }) {
const gt = useGT();
const inputRef = useRef<HTMLInputElement>(null);
return (
<>
<input
ref={inputRef}
type="file"
multiple
className="hidden"
onChange={(event) => {
const files = Array.from(event.currentTarget.files ?? []);
event.currentTarget.value = "";
// Inserting a chip refocuses the editor after the draft renders;
// focusing synchronously here would report the editor's stale text
// over the prompt that was just written.
void addComposerAttachments(files).then((inserted) => {
if (!inserted) focusComposer();
});
}}
/>
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
variant="ghost"
size="icon-sm"
onPointerDown={(event) => event.preventDefault()}
onClick={() => inputRef.current?.click()}
aria-label={gt("Attach files")}
/>
}
>
{/* The clip's weight sits in its upper loop; 1px down reads as centered on the row. */}
<PaperclipIcon className="translate-y-px" />
</TooltipTrigger>
<TooltipPopup>
<T>Attach files</T>
</TooltipPopup>
</Tooltip>
</>
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
variant="ghost"
size="icon-sm"
onPointerDown={(event) => event.preventDefault()}
onClick={onOpenPicker}
aria-label={gt("Attach files")}
/>
}
>
{/* The clip's weight sits in its upper loop; 1px down reads as centered on the row. */}
<PaperclipIcon className="translate-y-px" />
</TooltipTrigger>
<TooltipPopup>
<T>Attach files</T>
</TooltipPopup>
</Tooltip>
);
}
7 changes: 6 additions & 1 deletion apps/web/test/fakeEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ const descriptor = {
// A matching version, since a mismatch adds an update banner a typical user never sees.
serverVersion: APP_VERSION,
orchestrationProtocolVersion: ORCHESTRATION_PROTOCOL_VERSION,
capabilities: { repositoryIdentity: true, sideChats: true, freshContext: true },
capabilities: {
repositoryIdentity: true,
sideChats: true,
freshContext: true,
fileAttachments: { maxUploadBytes: 10_000_000 },
},
};
const auth = {
policy: "loopback-browser",
Expand Down
Loading