Skip to content
Closed
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
29 changes: 12 additions & 17 deletions site/src/pages/AgentsPage/components/AgentComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
type ComposerContextValue,
useAgentComposer,
} from "./AgentComposer/context";
import { Frame, FrameEventBoundary } from "./AgentComposer/Frame";
import { Frame } from "./AgentComposer/Frame";
import { useComposerRuntime } from "./AgentComposer/useComposerRuntime";
import { AttachmentPreview } from "./AttachmentPreview";
import { ChatMessageInput } from "./ChatMessageInput/ChatMessageInput";
Expand Down Expand Up @@ -111,7 +111,6 @@ function Attachments() {
const [previewImage, setPreviewImage] = useState<string | null>(null);
const [previewText, setPreviewText] = useState<TextPreview | null>(null);
const { files } = state;
const isPreviewOpen = previewImage !== null || previewText !== null;

const handleTextPreview = (
content: string,
Expand Down Expand Up @@ -141,21 +140,17 @@ function Attachments() {
onRemove={actions.removeWorkspaceUpload}
/>
)}
{isPreviewOpen && (
<FrameEventBoundary>
{previewImage && (
<ImageLightbox
src={previewImage}
onClose={() => setPreviewImage(null)}
/>
)}
{previewText && (
<TextPreviewDialog
{...previewText}
onClose={() => setPreviewText(null)}
/>
)}
</FrameEventBoundary>
{previewImage && (
<ImageLightbox
src={previewImage}
onClose={() => setPreviewImage(null)}
/>
)}
{previewText && (
<TextPreviewDialog
{...previewText}
onClose={() => setPreviewText(null)}
/>
)}
</>
);
Expand Down
61 changes: 22 additions & 39 deletions site/src/pages/AgentsPage/components/AgentComposer/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,42 +157,13 @@ function synchronizeDropdownViewport(composerElement: HTMLDivElement) {
};
}

const stopFrameEvent = (event: React.SyntheticEvent) => {
event.stopPropagation();
};

const stopFrameEscape = (event: React.KeyboardEvent) => {
if (event.key === "Escape") {
event.stopPropagation();
}
};

/**
* Wraps dialogs rendered inside the frame. Dialogs portal out of the
* frame's DOM but still bubble React events through it, so this stops the
* events the frame handles: Escape and file drops inside a dialog do not
* cancel an edit, interrupt the stream, or attach files.
* Portaled menus and dialogs leave the frame's DOM but still bubble React
* events through it. Their Escape and drop actions belong to them, not to
* the draft behind them.
*/
export function FrameEventBoundary({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) {
return (
<div
role="presentation"
className={className}
onKeyDown={stopFrameEscape}
onDragOver={stopFrameEvent}
onDragLeave={stopFrameEvent}
onDrop={stopFrameEvent}
>
{children}
</div>
);
}
const isFromFrameDOM = (event: React.SyntheticEvent<HTMLElement>) =>
event.target instanceof Node && event.currentTarget.contains(event.target);

/** Anchors composer interactions and synchronizes mobile dropdown geometry. */
export function Frame({
Expand All @@ -214,8 +185,8 @@ export function Frame({
return synchronizeDropdownViewport(composerElement);
}, [composerElement]);

const handleKeyDown = (event: React.KeyboardEvent) => {
if (event.key !== "Escape") {
const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key !== "Escape" || !isFromFrameDOM(event)) {
return;
}

Expand All @@ -232,15 +203,23 @@ export function Frame({
}
};

const handleDragOver = (event: React.DragEvent) => {
const handleDragOver = (event: React.DragEvent<HTMLDivElement>) => {
if (!isFromFrameDOM(event)) {
return;
}

event.preventDefault();

if (event.dataTransfer.types.includes("Files")) {
setIsDragging(true);
}
};

const handleDragLeave = (event: React.DragEvent) => {
const handleDragLeave = (event: React.DragEvent<HTMLDivElement>) => {
if (!isFromFrameDOM(event)) {
return;
}

const { relatedTarget } = event;
const leftFrame =
!(relatedTarget instanceof Node) ||
Expand All @@ -251,7 +230,11 @@ export function Frame({
}
};

const handleDrop = (event: React.DragEvent) => {
const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
if (!isFromFrameDOM(event)) {
return;
}

event.preventDefault();
setIsDragging(false);

Expand Down
23 changes: 10 additions & 13 deletions site/src/pages/AgentsPage/components/AgentComposerOptionsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { isBelowMdViewport } from "#/utils/mobile";
import { useMCPOAuthFlow } from "../hooks/useMCPOAuthFlow";
import { chatAttachmentAcceptAttribute } from "../utils/chatAttachments";
import { useAgentComposer } from "./AgentComposer/context";
import { FrameEventBoundary } from "./AgentComposer/Frame";
import {
enabledMcpServers,
MCPServerMenuItem,
Expand Down Expand Up @@ -264,18 +263,16 @@ export const AgentComposerOptionsMenu = () => {
className="hidden"
/>
)}
<FrameEventBoundary className="hidden">
<ConfirmDialog
open={disconnectTarget !== null}
title={`Disconnect ${disconnectTarget?.display_name ?? "MCP server"}?`}
description="This removes your credentials for this MCP server from Coder. You can authenticate again later."
type="delete"
confirmText="Disconnect"
confirmLoading={disconnectMutation.isPending}
onConfirm={confirmDisconnect}
onClose={() => setDisconnectTarget(null)}
/>
</FrameEventBoundary>
<ConfirmDialog
open={disconnectTarget !== null}
title={`Disconnect ${disconnectTarget?.display_name ?? "MCP server"}?`}
description="This removes your credentials for this MCP server from Coder. You can authenticate again later."
type="delete"
confirmText="Disconnect"
confirmLoading={disconnectMutation.isPending}
onConfirm={confirmDisconnect}
onClose={() => setDisconnectTarget(null)}
/>
</>
);
};
28 changes: 28 additions & 0 deletions site/src/pages/AgentsPage/components/AgentComposers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,34 @@ describe("ChatComposer", () => {
expect(onMCPSelectionChange).not.toHaveBeenCalled();
});

it.each([
{ isEditingHistoryMessage: false },
{ isEditingHistoryMessage: true },
])(
"leaves the draft alone when Escape closes the plus menu (%o)",
async ({ isEditingHistoryMessage }) => {
const user = userEvent.setup();
const onInterrupt = vi.fn();
const onCancelHistoryEdit = vi.fn();
renderInput(
<ChatComposer
{...withBindings({
isStreaming: true,
onInterrupt,
isEditingHistoryMessage,
onCancelHistoryEdit,
})}
/>,
);

await user.click(screen.getByRole("button", { name: "More options" }));
await screen.findByRole("menuitemcheckbox", { name: "Plan first" });
await user.keyboard("{Escape}");
expect(onInterrupt).not.toHaveBeenCalled();
expect(onCancelHistoryEdit).not.toHaveBeenCalled();
},
);

it("swaps Stop for the Queue button once a draft is entered while streaming", async () => {
const user = userEvent.setup();
const inputRef = createRef<ChatMessageInputRef>();
Expand Down
Loading