Skip to content

πŸ™Œ OpenHands: AI-Driven Development

refactor: extract PendingStopIcon and UserMessageBody from chat-message (#1374)

Hiep Le063158679b63
diff --git a/__tests__/components/chat-message.test.tsx b/__tests__/components/chat-message.test.tsx
index d43b8bff60..dce0fb5dad 100644
--- a/__tests__/components/chat-message.test.tsx
+++ b/__tests__/components/chat-message.test.tsx
@@ -1,6 +1,6 @@
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
-import { describe, it, expect } from "vitest";
+import { describe, it, expect, vi } from "vitest";
import { ChatMessage } from "#/components/features/chat/chat-message";
describe("ChatMessage", () => {
@@ -80,4 +80,25 @@ describe("ChatMessage", () => {
expect(screen.queryByTestId("chat-message-truncation-gradient")).not.toBeInTheDocument();
expect(screen.queryByTestId("chat-message-view-more")).not.toBeInTheDocument();
});
+
+ it("shows a stop control for a sending user message and calls onStop when clicked", () => {
+ const onStop = vi.fn();
+ render(
+ <ChatMessage
+ type="user"
+ message="Working on it"
+ pendingStatus="sending"
+ onStop={onStop}
+ />,
+ );
+
+ expect(screen.getByTestId("chat-message-sending")).toBeInTheDocument();
+ const stopButton = screen.getByTestId("chat-message-stop");
+
+ // The stop control only becomes interactive once the bubble is hovered.
+ fireEvent.mouseEnter(screen.getByTestId("user-message"));
+ fireEvent.click(stopButton);
+
+ expect(onStop).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/src/components/features/chat/chat-message.tsx b/src/components/features/chat/chat-message.tsx
index ac3764a827..2d5f2ade67 100644
--- a/src/components/features/chat/chat-message.tsx
+++ b/src/components/features/chat/chat-message.tsx
@@ -7,142 +7,15 @@ import { StyledTooltip } from "#/components/shared/buttons/styled-tooltip";
import { I18nKey } from "#/i18n/declaration";
import { TextShimmer } from "#/components/shared/text-shimmer";
import { MarkdownRenderer } from "../markdown/markdown-renderer";
+import { PendingStopIcon } from "./pending-stop-icon";
+import {
+ UserMessageBody,
+ chatBubbleMarkdownComponents,
+ USER_MESSAGE_LINE_HEIGHT_PX,
+} from "./user-message-body";
export type ChatMessagePendingStatus = "sending" | "error";
-const USER_MESSAGE_MAX_LINES = 3;
-const USER_MESSAGE_LINE_HEIGHT_PX = 24;
-
-function PendingStopIcon({ className }: { className?: string }) {
- return (
- <svg
- xmlns="http://www.w3.org/2000/svg"
- viewBox="0 0 24 24"
- fill="none"
- aria-hidden
- className={className}
- >
- <circle
- cx="12"
- cy="12"
- r="10"
- className="fill-[var(--oh-foreground)] transition-colors duration-150 group-hover:fill-[var(--oh-text-secondary)]"
- />
- <rect
- x="9"
- y="9"
- width="6"
- height="6"
- rx="1"
- className="fill-[var(--oh-color-tertiary)]"
- />
- </svg>
- );
-}
-
-const chatBubbleMarkdownComponents = {
- p: ({ children }: React.ComponentProps<"p">) => (
- <p className="m-0 leading-6">{children}</p>
- ),
-};
-
-function UserMessageBody({
- message,
- isHovering,
- isExpanded,
- onTruncatableChange,
-}: {
- message: string;
- isHovering: boolean;
- isExpanded: boolean;
- onTruncatableChange: (truncatable: boolean) => void;
-}) {
- const { t } = useTranslation("openhands");
- const contentRef = React.useRef<HTMLDivElement>(null);
- const [isTruncatable, setIsTruncatable] = React.useState(false);
-
- React.useLayoutEffect(() => {
- const content = contentRef.current;
- if (!content || isExpanded) {
- setIsTruncatable(false);
- onTruncatableChange(false);
- return undefined;
- }
-
- const measure = () => {
- const lineHeight = Number.parseFloat(
- getComputedStyle(content).lineHeight,
- );
- const linePx =
- Number.isFinite(lineHeight) && lineHeight > 0
- ? lineHeight
- : USER_MESSAGE_LINE_HEIGHT_PX;
- const maxHeight = USER_MESSAGE_MAX_LINES * linePx;
- const newlineCount = (message.match(/\n/g) ?? []).length;
-
- const truncatable =
- content.scrollHeight > maxHeight + 1 ||
- newlineCount >= USER_MESSAGE_MAX_LINES ||
- message.trim().length > 220;
-
- setIsTruncatable((previous) => {
- if (previous !== truncatable) {
- onTruncatableChange(truncatable);
- }
- return truncatable;
- });
- };
-
- measure();
-
- const observer = new ResizeObserver(measure);
- observer.observe(content);
-
- return () => observer.disconnect();
- }, [message, isExpanded, onTruncatableChange]);
-
- const isCollapsed = isTruncatable && !isExpanded;
-
- return (
- <div className="relative min-w-0">
- <div
- ref={contentRef}
- className={cn(
- "text-sm leading-6 whitespace-normal [word-break:break-word]",
- isCollapsed && "line-clamp-3",
- )}
- >
- <MarkdownRenderer
- includeStandard
- includeHeadings
- components={chatBubbleMarkdownComponents}
- >
- {message}
- </MarkdownRenderer>
- </div>
-
- {isCollapsed ? (
- <>
- <div
- aria-hidden
- data-testid="chat-message-truncation-gradient"
- className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-tertiary to-transparent"
- />
- <span
- data-testid="chat-message-view-more"
- className={cn(
- "pointer-events-none absolute bottom-1 left-1/2 z-10 inline-flex -translate-x-1/2 items-center rounded-full border border-[var(--oh-border-subtle)] bg-[var(--oh-surface-raised)] px-2.5 py-0.5 text-xs font-normal text-[var(--oh-foreground)] transition-opacity duration-150",
- isHovering ? "opacity-100" : "opacity-0",
- )}
- >
- {t(I18nKey.COMMON$VIEW_MORE)}
- </span>
- </>
- ) : null}
- </div>
- );
-}
-
interface ChatMessageProps {
type: SourceType;
message: string;
diff --git a/src/components/features/chat/pending-stop-icon.tsx b/src/components/features/chat/pending-stop-icon.tsx
new file mode 100644
index 0000000000..ce8f50a925
--- /dev/null
+++ b/src/components/features/chat/pending-stop-icon.tsx
@@ -0,0 +1,26 @@
+export function PendingStopIcon({ className }: { className?: string }) {
+ return (
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 24 24"
+ fill="none"
+ aria-hidden
+ className={className}
+ >
+ <circle
+ cx="12"
+ cy="12"
+ r="10"
+ className="fill-[var(--oh-foreground)] transition-colors duration-150 group-hover:fill-[var(--oh-text-secondary)]"
+ />
+ <rect
+ x="9"
+ y="9"
+ width="6"
+ height="6"
+ rx="1"
+ className="fill-[var(--oh-color-tertiary)]"
+ />
+ </svg>
+ );
+}
diff --git a/src/components/features/chat/user-message-body.tsx b/src/components/features/chat/user-message-body.tsx
new file mode 100644
index 0000000000..a8851d25a9
--- /dev/null
+++ b/src/components/features/chat/user-message-body.tsx
@@ -0,0 +1,111 @@
+import React from "react";
+import { useTranslation } from "react-i18next";
+import { cn } from "#/utils/utils";
+import { I18nKey } from "#/i18n/declaration";
+import { MarkdownRenderer } from "../markdown/markdown-renderer";
+
+const USER_MESSAGE_MAX_LINES = 3;
+export const USER_MESSAGE_LINE_HEIGHT_PX = 24;
+
+export const chatBubbleMarkdownComponents = {
+ p: ({ children }: React.ComponentProps<"p">) => (
+ <p className="m-0 leading-6">{children}</p>
+ ),
+};
+
+export function UserMessageBody({
+ message,
+ isHovering,
+ isExpanded,
+ onTruncatableChange,
+}: {
+ message: string;
+ isHovering: boolean;
+ isExpanded: boolean;
+ onTruncatableChange: (truncatable: boolean) => void;
+}) {
+ const { t } = useTranslation("openhands");
+ const contentRef = React.useRef<HTMLDivElement>(null);
+ const [isTruncatable, setIsTruncatable] = React.useState(false);
+
+ React.useLayoutEffect(() => {
+ const content = contentRef.current;
+ if (!content || isExpanded) {
+ setIsTruncatable(false);
+ onTruncatableChange(false);
+ return undefined;
+ }
+
+ const measure = () => {
+ const lineHeight = Number.parseFloat(
+ getComputedStyle(content).lineHeight,
+ );
+ const linePx =
+ Number.isFinite(lineHeight) && lineHeight > 0
+ ? lineHeight
+ : USER_MESSAGE_LINE_HEIGHT_PX;
+ const maxHeight = USER_MESSAGE_MAX_LINES * linePx;
+ const newlineCount = (message.match(/\n/g) ?? []).length;
+
+ const truncatable =
+ content.scrollHeight > maxHeight + 1 ||
+ newlineCount >= USER_MESSAGE_MAX_LINES ||
+ message.trim().length > 220;
+
+ setIsTruncatable((previous) => {
+ if (previous !== truncatable) {
+ onTruncatableChange(truncatable);
+ }
+ return truncatable;
+ });
+ };
+
+ measure();
+
+ const observer = new ResizeObserver(measure);
+ observer.observe(content);
+
+ return () => observer.disconnect();
+ }, [message, isExpanded, onTruncatableChange]);
+
+ const isCollapsed = isTruncatable && !isExpanded;
+
+ return (
+ <div className="relative min-w-0">
+ <div
+ ref={contentRef}
+ className={cn(
+ "text-sm leading-6 whitespace-normal [word-break:break-word]",
+ isCollapsed && "line-clamp-3",
+ )}
+ >
+ <MarkdownRenderer
+ includeStandard
+ includeHeadings
+ components={chatBubbleMarkdownComponents}
+ >
+ {message}
+ </MarkdownRenderer>
+ </div>
+
+ {isCollapsed ? (
+ <>
+ <div
+ aria-hidden
+ data-testid="chat-message-truncation-gradient"
+ className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-tertiary to-transparent"
+ />
+ <span
+ data-testid="chat-message-view-more"
+ className={cn(
+ "pointer-events-none absolute bottom-1 left-1/2 z-10 inline-flex -translate-x-1/2 items-center rounded-full border border-[var(--oh-border-subtle)] bg-[var(--oh-surface-raised)] px-2.5 py-0.5 text-xs font-normal text-[var(--oh-foreground)] transition-opacity duration-150",
+ isHovering ? "opacity-100" : "opacity-0",
+ )}
+ >
+ {t(I18nKey.COMMON$VIEW_MORE)}
+ </span>
+ </>
+ ) : null}
+ </div>
+ );
+}