Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend/copilot): prevent Tooltip from switching between controlled/uncontrolled (#14179)

seer-by-sentry[bot]ca7b6433c1e5
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index 276cbe5771..7970248cde 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -171,6 +171,7 @@ export const ChatContainer = ({
const isSessionUnavailable =
!!isReconnecting || isLoadingSession || !!isSessionError;
const isLimitReached = useIsUsageLimitReached();
+ const [isUsageTooltipOpen, setIsUsageTooltipOpen] = useState(false);
const isInputDisabled =
isSessionUnavailable ||
isLimitReached ||
@@ -363,7 +364,10 @@ export const ChatContainer = ({
/>
</div>
)}
- <Tooltip open={isLimitReached ? undefined : false}>
+ <Tooltip
+ open={Boolean(isLimitReached && isUsageTooltipOpen)}
+ onOpenChange={setIsUsageTooltipOpen}
+ >
<TooltipTrigger asChild>
<div>
<ChatInput
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
index 0a217f655d..f34dbc93a7 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
@@ -3,6 +3,7 @@ import { getGetV2GetChatShareStateMockHandler200 } from "@/app/api/__generated__
import type { ChatShareStateResponse } from "@/app/api/__generated__/models/chatShareStateResponse";
import { server } from "@/mocks/mock-server";
import {
+ act,
render,
screen,
cleanup,
@@ -107,19 +108,6 @@ vi.mock("@/app/(platform)/copilot/components/ChatInput/ChatInput", () => ({
),
}));
-vi.mock("@/components/atoms/Tooltip/BaseTooltip", () => ({
- TooltipProvider: ({ children }: { children: React.ReactNode }) => (
- <>{children}</>
- ),
- Tooltip: ({ children }: { children: React.ReactNode }) => <>{children}</>,
- TooltipContent: ({ children }: { children: React.ReactNode }) => (
- <div>{children}</div>
- ),
- TooltipTrigger: ({ children }: { children: React.ReactNode }) => (
- <>{children}</>
- ),
-}));
-
vi.mock("@/services/feature-flags/use-get-flag", () => ({
Flag: {
ARTIFACTS: "ARTIFACTS",
@@ -219,9 +207,38 @@ describe("ChatContainer", () => {
cleanup();
resetCopilotStore();
vi.clearAllMocks();
+ vi.restoreAllMocks();
vi.unstubAllGlobals();
});
+ it("keeps usage tooltips hoverable and dismissible as the limit changes", async () => {
+ const consoleWarn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const { rerender } = render(<ChatContainer {...baseProps} />);
+ const input = screen.getByTestId("chat-input");
+ fireEvent.change(input, { target: { value: "Unsent draft" } });
+ expect(screen.queryByRole("tooltip")).toBeNull();
+
+ mockIsUsageLimitReached.mockReturnValue(true);
+ rerender(<ChatContainer {...baseProps} />);
+ expect(screen.getByTestId("chat-input")).toBe(input);
+ expect((input as HTMLInputElement).value).toBe("Unsent draft");
+ expect(screen.queryByRole("tooltip")).toBeNull();
+
+ fireEvent.pointerMove(input.parentElement!, { pointerType: "mouse" });
+ expect(await screen.findByRole("tooltip")).toBeDefined();
+ await act(async () => {});
+ fireEvent.keyDown(document, { key: "Escape" });
+ await waitFor(() => expect(screen.queryByRole("tooltip")).toBeNull());
+
+ mockIsUsageLimitReached.mockReturnValue(false);
+ rerender(<ChatContainer {...baseProps} />);
+ expect(screen.queryByRole("tooltip")).toBeNull();
+ expect(consoleWarn.mock.calls.flat().join(" ")).not.toMatch(
+ /controlled|uncontrolled/,
+ );
+ consoleWarn.mockRestore();
+ });
+
it("renders the blurred usage-limit backdrop only when the limit is reached", () => {
mockIsUsageLimitReached.mockReturnValue(true);
diff --git a/autogpt_platform/frontend/src/components/atoms/Tooltip/BaseTooltip.tsx b/autogpt_platform/frontend/src/components/atoms/Tooltip/BaseTooltip.tsx
index be8e4365cb..2b22a279dc 100644
--- a/autogpt_platform/frontend/src/components/atoms/Tooltip/BaseTooltip.tsx
+++ b/autogpt_platform/frontend/src/components/atoms/Tooltip/BaseTooltip.tsx
@@ -7,19 +7,24 @@ import { cn } from "@/lib/utils";
const TooltipProvider = TooltipPrimitive.Provider;
-const Tooltip = ({
- children,
- delayDuration = 10,
- open,
-}: {
+interface Props {
children: React.ReactNode;
delayDuration?: number;
open?: boolean;
-}) => (
- <TooltipPrimitive.Root delayDuration={delayDuration} open={open}>
- {children}
- </TooltipPrimitive.Root>
-);
+ onOpenChange?: (open: boolean) => void;
+}
+
+function Tooltip({ children, delayDuration = 10, open, onOpenChange }: Props) {
+ return (
+ <TooltipPrimitive.Root
+ delayDuration={delayDuration}
+ open={open}
+ onOpenChange={onOpenChange}
+ >
+ {children}
+ </TooltipPrimitive.Root>
+ );
+}
const TooltipTrigger = TooltipPrimitive.Trigger;