Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(frontend): Refetch conversations when toggling the conversation panel (#6190)

sp.wack053723a4d40a
diff --git a/frontend/__tests__/components/features/conversation-panel/conversation-panel.test.tsx b/frontend/__tests__/components/features/conversation-panel/conversation-panel.test.tsx
index beb89a9562..6845fa4244 100644
--- a/frontend/__tests__/components/features/conversation-panel/conversation-panel.test.tsx
+++ b/frontend/__tests__/components/features/conversation-panel/conversation-panel.test.tsx
@@ -1,4 +1,4 @@
-import { render, screen, within } from "@testing-library/react";
+import { render, screen, waitFor, within } from "@testing-library/react";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import {
QueryClientProvider,
@@ -7,10 +7,12 @@ import {
} from "@tanstack/react-query";
import userEvent from "@testing-library/user-event";
import { createRoutesStub } from "react-router";
+import React from "react";
import { ConversationPanel } from "#/components/features/conversation-panel/conversation-panel";
import OpenHands from "#/api/open-hands";
import { AuthProvider } from "#/context/auth-context";
import { clickOnEditButton } from "./utils";
+import { queryClientConfig } from "#/query-client-config";
describe("ConversationPanel", () => {
const onCloseMock = vi.fn();
@@ -231,4 +233,47 @@ describe("ConversationPanel", () => {
expect(onCloseMock).toHaveBeenCalledOnce();
});
+
+ it("should refetch data on rerenders", async () => {
+ // We need to simulate the toggling of the component to test the refetching
+ function PanelWithToggle() {
+ const [isOpen, setIsOpen] = React.useState(true);
+ return (
+ <>
+ <button type="button" onClick={() => setIsOpen((prev) => !prev)}>
+ Toggle
+ </button>
+ {isOpen && <ConversationPanel onClose={onCloseMock} />}
+ </>
+ );
+ }
+
+ const MyRouterStub = createRoutesStub([
+ {
+ Component: PanelWithToggle,
+ path: "/",
+ },
+ ]);
+
+ const getUserConversationsSpy = vi.spyOn(OpenHands, "getUserConversations");
+ render(<MyRouterStub />, {
+ wrapper: ({ children }) => (
+ <AuthProvider>
+ <QueryClientProvider client={new QueryClient(queryClientConfig)}>
+ {children}
+ </QueryClientProvider>
+ </AuthProvider>
+ ),
+ });
+
+ await waitFor(() => expect(getUserConversationsSpy).toHaveBeenCalledOnce());
+
+ const button = screen.getByText("Toggle");
+ await userEvent.click(button);
+ await userEvent.click(button);
+
+ await waitFor(() =>
+ expect(getUserConversationsSpy).toHaveBeenCalledTimes(2),
+ );
+ });
});
diff --git a/frontend/__tests__/components/features/sidebar/sidebar.test.tsx b/frontend/__tests__/components/features/sidebar/sidebar.test.tsx
index 7b478219e2..1f2ad6c57a 100644
--- a/frontend/__tests__/components/features/sidebar/sidebar.test.tsx
+++ b/frontend/__tests__/components/features/sidebar/sidebar.test.tsx
@@ -8,6 +8,9 @@ import { MULTI_CONVERSATION_UI } from "#/utils/feature-flags";
import OpenHands from "#/api/open-hands";
import { MOCK_USER_PREFERENCES } from "#/mocks/handlers";
+// These tests will now fail because the conversation panel is rendered through a portal
+// and technically not a child of the Sidebar component.
+
const renderSidebar = () => {
const RouterStub = createRoutesStub([
{
diff --git a/frontend/src/entry.client.tsx b/frontend/src/entry.client.tsx
index 47f0090489..673dabec11 100644
--- a/frontend/src/entry.client.tsx
+++ b/frontend/src/entry.client.tsx
@@ -11,15 +11,11 @@ import { hydrateRoot } from "react-dom/client";
import { Provider } from "react-redux";
import posthog from "posthog-js";
import "./i18n";
-import {
- QueryCache,
- QueryClient,
- QueryClientProvider,
-} from "@tanstack/react-query";
-import toast from "react-hot-toast";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import store from "./store";
import { useConfig } from "./hooks/query/use-config";
import { AuthProvider } from "./context/auth-context";
+import { queryClientConfig } from "./query-client-config";
import { SettingsProvider } from "./context/settings-context";
function PosthogInit() {
@@ -50,27 +46,7 @@ async function prepareApp() {
}
}
-const QUERY_KEYS_TO_IGNORE = ["authenticated", "hosts"];
-const queryClient = new QueryClient({
- queryCache: new QueryCache({
- onError: (error, query) => {
- if (!QUERY_KEYS_TO_IGNORE.some((key) => query.queryKey.includes(key))) {
- toast.error(error.message);
- }
- },
- }),
- defaultOptions: {
- queries: {
- staleTime: 1000 * 60 * 5, // 5 minutes
- gcTime: 1000 * 60 * 15, // 15 minutes
- },
- mutations: {
- onError: (error) => {
- toast.error(error.message);
- },
- },
- },
-});
+const queryClient = new QueryClient(queryClientConfig);
prepareApp().then(() =>
startTransition(() => {
diff --git a/frontend/src/hooks/query/use-user-conversations.ts b/frontend/src/hooks/query/use-user-conversations.ts
index 5157c91d56..9f923c3f68 100644
--- a/frontend/src/hooks/query/use-user-conversations.ts
+++ b/frontend/src/hooks/query/use-user-conversations.ts
@@ -9,5 +9,6 @@ export const useUserConversations = () => {
queryKey: ["user", "conversations"],
queryFn: OpenHands.getUserConversations,
enabled: !!userIsAuthenticated,
+ staleTime: 0,
});
};
diff --git a/frontend/src/query-client-config.ts b/frontend/src/query-client-config.ts
new file mode 100644
index 0000000000..2dba240dda
--- /dev/null
+++ b/frontend/src/query-client-config.ts
@@ -0,0 +1,25 @@
+import { QueryClientConfig, QueryCache } from "@tanstack/react-query";
+import toast from "react-hot-toast";
+
+const QUERY_KEYS_TO_IGNORE = ["authenticated", "hosts"];
+
+export const queryClientConfig: QueryClientConfig = {
+ queryCache: new QueryCache({
+ onError: (error, query) => {
+ if (!QUERY_KEYS_TO_IGNORE.some((key) => query.queryKey.includes(key))) {
+ toast.error(error.message);
+ }
+ },
+ }),
+ defaultOptions: {
+ queries: {
+ staleTime: 1000 * 60 * 5, // 5 minutes
+ gcTime: 1000 * 60 * 15, // 15 minutes
+ },
+ mutations: {
+ onError: (error) => {
+ toast.error(error.message);
+ },
+ },
+ },
+};