Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: resolve React console warning in UserMessageBody truncation (#1431)

Vasco Schiavof2fb7cafc928
diff --git a/__tests__/components/chat-message.test.tsx b/__tests__/components/chat-message.test.tsx
index dce0fb5dad..7005a9eaf7 100644
--- a/__tests__/components/chat-message.test.tsx
+++ b/__tests__/components/chat-message.test.tsx
@@ -1,9 +1,101 @@
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi } from "vitest";
+import util from "util";
import { ChatMessage } from "#/components/features/chat/chat-message";
describe("ChatMessage", () => {
+ it("does not update the parent while measuring user-message truncation", async () => {
+ // Regression test for the React warning:
+ // "Cannot update a component (`ChatMessage`) while rendering a different
+ // component (`UserMessageBody`)"
+ // The bug: UserMessageBody notified the parent (ChatMessage's
+ // setIsTruncatable) from inside its own render-phase state updater, which
+ // updates the parent while the child is rendering.
+ //
+ // Two conditions are needed to make the warning observable here β€” they are
+ // present in a real browser but hidden by the default test setup:
+ // 1. A ResizeObserver that actually fires its callback. The shared mock's
+ // `observe` is a no-op, so truncation is only ever measured once and
+ // React's eager-state bailout swallows the render-phase update.
+ // 2. A measured height that changes between measurements. We model the
+ // realistic settling: the content overflows on first measure, then
+ // shrinks once `line-clamp` is applied. The second (render-phase)
+ // measurement therefore produces a real state transition, defeating
+ // the eager-state bailout so the buggy parent update actually runs.
+ class FiringResizeObserver {
+ private cb: ResizeObserverCallback;
+
+ constructor(cb: ResizeObserverCallback) {
+ this.cb = cb;
+ }
+
+ // The real ResizeObserver invokes the callback once shortly after observe.
+ observe = () => this.cb([], this as unknown as ResizeObserver);
+
+ unobserve = () => {};
+
+ disconnect = () => {};
+ }
+
+ let measurements = 0;
+ const originalScrollHeight = Object.getOwnPropertyDescriptor(
+ HTMLElement.prototype,
+ "scrollHeight",
+ );
+ Object.defineProperty(HTMLElement.prototype, "scrollHeight", {
+ configurable: true,
+ get() {
+ measurements += 1;
+ // Overflow on the first measure (truncatable), shrink afterwards.
+ return measurements <= 1 ? 1000 : 0;
+ },
+ });
+
+ const previousResizeObserver = globalThis.ResizeObserver;
+ globalThis.ResizeObserver =
+ FiringResizeObserver as unknown as typeof ResizeObserver;
+
+ const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
+ // No newlines and <= 220 chars, so truncation depends only on the measured
+ // (mocked) height rather than the message length / newline heuristics.
+ const message = "word ".repeat(20).trim();
+
+ try {
+ render(<ChatMessage type="user" message={message} />);
+
+ await waitFor(() => {
+ expect(screen.getByTestId("user-message")).toBeInTheDocument();
+ });
+
+ // Guard against the test becoming vacuous: truncation must have been
+ // measured more than once for the render-phase update to be reachable.
+ expect(measurements).toBeGreaterThan(1);
+
+ const renderPhaseWarning = consoleError.mock.calls.find((args) =>
+ util
+ .format(...(args as [unknown, ...unknown[]]))
+ .includes(
+ "Cannot update a component (`ChatMessage`) while rendering a different component (`UserMessageBody`)",
+ ),
+ );
+ expect(renderPhaseWarning).toBeUndefined();
+ } finally {
+ consoleError.mockRestore();
+ globalThis.ResizeObserver = previousResizeObserver;
+ if (originalScrollHeight) {
+ Object.defineProperty(
+ HTMLElement.prototype,
+ "scrollHeight",
+ originalScrollHeight,
+ );
+ } else {
+ // @ts-expect-error -- remove our override to restore the jsdom default
+ delete HTMLElement.prototype.scrollHeight;
+ }
+ }
+ });
+
it("should render a user message", () => {
render(<ChatMessage type="user" message="Hello, World!" />);
expect(screen.getByTestId("user-message")).toBeInTheDocument();
diff --git a/src/components/features/chat/user-message-body.tsx b/src/components/features/chat/user-message-body.tsx
index a8851d25a9..00e6b0a7c1 100644
--- a/src/components/features/chat/user-message-body.tsx
+++ b/src/components/features/chat/user-message-body.tsx
@@ -28,11 +28,14 @@ export function UserMessageBody({
const contentRef = React.useRef<HTMLDivElement>(null);
const [isTruncatable, setIsTruncatable] = React.useState(false);
+ React.useEffect(() => {
+ onTruncatableChange(isTruncatable);
+ }, [isTruncatable, onTruncatableChange]);
+
React.useLayoutEffect(() => {
const content = contentRef.current;
if (!content || isExpanded) {
setIsTruncatable(false);
- onTruncatableChange(false);
return undefined;
}
@@ -52,12 +55,7 @@ export function UserMessageBody({
newlineCount >= USER_MESSAGE_MAX_LINES ||
message.trim().length > 220;
- setIsTruncatable((previous) => {
- if (previous !== truncatable) {
- onTruncatableChange(truncatable);
- }
- return truncatable;
- });
+ setIsTruncatable(truncatable);
};
measure();
@@ -66,7 +64,7 @@ export function UserMessageBody({
observer.observe(content);
return () => observer.disconnect();
- }, [message, isExpanded, onTruncatableChange]);
+ }, [message, isExpanded]);
const isCollapsed = isTruncatable && !isExpanded;