Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: redirect to section list after adding a backend (automations) (#927)

Hiep Lef8db3fab6beb
diff --git a/__tests__/components/backends/add-backend-modal.test.tsx b/__tests__/components/backends/add-backend-modal.test.tsx
index 786f94b98a..523738305c 100644
--- a/__tests__/components/backends/add-backend-modal.test.tsx
+++ b/__tests__/components/backends/add-backend-modal.test.tsx
@@ -5,15 +5,28 @@ import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { __resetActiveStoreForTests } from "#/api/backend-registry/active-store";
import { ActiveBackendProvider } from "#/contexts/active-backend-context";
+import {
+ NavigationProvider,
+ type NavigationContextValue,
+} from "#/context/navigation-context";
import { AddBackendModal } from "#/components/features/backends/add-backend-modal";
-function renderWithProviders(ui: React.ReactElement) {
+function renderWithProviders(
+ ui: React.ReactElement,
+ navigation?: NavigationContextValue,
+) {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
});
return render(
<QueryClientProvider client={queryClient}>
- <ActiveBackendProvider>{ui}</ActiveBackendProvider>
+ <ActiveBackendProvider>
+ {navigation ? (
+ <NavigationProvider value={navigation}>{ui}</NavigationProvider>
+ ) : (
+ ui
+ )}
+ </ActiveBackendProvider>
</QueryClientProvider>,
);
}
@@ -182,3 +195,58 @@ describe("AddBackendModal – two-column layout", () => {
expect(loginButton.textContent).not.toContain("πŸ”‘");
});
});
+
+// @spec BM-002 β€” adding a backend auto-switches the active selection, so a
+// backend-scoped detail page is now stale; the user must land on the section
+// list rather than the previous backend's detail page.
+describe("AddBackendModal – redirect after adding a backend", () => {
+ function renderOnPath(currentPath: string) {
+ const navigate = vi.fn();
+ const navigation: NavigationContextValue = {
+ currentPath,
+ conversationId: null,
+ isNavigating: false,
+ navigate,
+ };
+ renderWithProviders(<AddBackendModal onClose={vi.fn()} />, navigation);
+ return { navigate };
+ }
+
+ async function addLocalBackend() {
+ const user = userEvent.setup();
+ await user.type(screen.getByTestId("add-backend-name"), "Local Extra");
+ await user.type(
+ screen.getByTestId("add-backend-host"),
+ "http://127.0.0.1:18002",
+ );
+ await user.click(screen.getByTestId("add-backend-submit"));
+ }
+
+ it.each([
+ { path: "/automations/auto-1", expected: "/automations" },
+ { path: "/conversations/abc", expected: "/conversations" },
+ ])(
+ "redirects to the section list when adding from $path",
+ async ({ path, expected }) => {
+ // Arrange
+ const { navigate } = renderOnPath(path);
+
+ // Act
+ await addLocalBackend();
+
+ // Assert
+ expect(navigate).toHaveBeenCalledWith(expected);
+ },
+ );
+
+ it("does not redirect when adding from a section list page", async () => {
+ // Arrange
+ const { navigate } = renderOnPath("/automations");
+
+ // Act
+ await addLocalBackend();
+
+ // Assert
+ expect(navigate).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/components/features/backends/backend-form-modal.tsx b/src/components/features/backends/backend-form-modal.tsx
index 0ce99982d5..1bcdfb1753 100644
--- a/src/components/features/backends/backend-form-modal.tsx
+++ b/src/components/features/backends/backend-form-modal.tsx
@@ -12,6 +12,7 @@ import { ModalCloseButton } from "#/components/shared/modals/modal-close-button"
import { BrandButton } from "#/components/features/settings/brand-button";
import { SettingsInput } from "#/components/features/settings/settings-input";
import { useActiveBackendContext } from "#/contexts/active-backend-context";
+import { useNavigation } from "#/context/navigation-context";
import { useBackendsHealth } from "#/hooks/query/use-backends-health";
import { getAgentServerClientOptions } from "#/api/agent-server-client-options";
import ChevronDownSmallIcon from "#/icons/chevron-down-small.svg?react";
@@ -392,6 +393,21 @@ export function BackendForm({
// ── Add-mode two-column layout ──────────────────────────────────────
+/**
+ * @spec BM-002 β€” Adding a backend auto-switches the active selection to it
+ * (BM-001), so a backend-scoped detail page the user is viewing now belongs
+ * to the previous backend. Redirect to that section's list so they never see
+ * stale data, mirroring the switch-backend redirect in BackendSelector.
+ */
+function useRedirectAfterAddBackend() {
+ const { currentPath, navigate } = useNavigation();
+ return React.useCallback(() => {
+ if (/^\/automations\/[^/]+/.test(currentPath)) navigate("/automations");
+ else if (/^\/conversations\/[^/]+/.test(currentPath))
+ navigate("/conversations");
+ }, [currentPath, navigate]);
+}
+
/**
* Left column of the "Add a Backend" modal: manual connection via
* Host + API Key. Designed for self-hosted agent servers and
@@ -400,6 +416,7 @@ export function BackendForm({
function ManualConnectionColumn({ onClose }: { onClose: () => void }) {
const { t } = useTranslation("openhands");
const { addBackend } = useActiveBackendContext();
+ const redirectAfterAdd = useRedirectAfterAddBackend();
const [name, setName] = React.useState("");
const [host, setHost] = React.useState("");
@@ -420,6 +437,7 @@ function ManualConnectionColumn({ onClose }: { onClose: () => void }) {
apiKey: apiKey.trim(),
kind,
});
+ redirectAfterAdd();
onClose();
};
@@ -496,6 +514,7 @@ function ManualConnectionColumn({ onClose }: { onClose: () => void }) {
function CloudLoginColumn({ onClose }: { onClose: () => void }) {
const { t } = useTranslation("openhands");
const { addBackend } = useActiveBackendContext();
+ const redirectAfterAdd = useRedirectAfterAddBackend();
const [advancedOpen, setAdvancedOpen] = React.useState(false);
const [customHost, setCustomHost] = React.useState("");
@@ -509,6 +528,7 @@ function CloudLoginColumn({ onClose }: { onClose: () => void }) {
apiKey,
kind: "cloud",
});
+ redirectAfterAdd();
onClose();
};