π OpenHands: AI-Driven Development
fix(backends): redirect off detail pages when switching from Manage backends (#18024)
Engel Nyst61095dbc2217
diff --git a/__tests__/components/backends/manage-backends-modal.test.tsx b/__tests__/components/backends/manage-backends-modal.test.tsxindex 8afb314441..caea01e951 100644--- a/__tests__/components/backends/manage-backends-modal.test.tsx+++ b/__tests__/components/backends/manage-backends-modal.test.tsx@@ -15,6 +15,10 @@ import {ActiveBackendProvider,useActiveBackendContext,} from "#/contexts/active-backend-context";+import {+ NavigationProvider,+ type NavigationContextValue,+} from "#/context/navigation-context";import { ManageBackendsModal } from "#/components/features/backends/manage-backends-modal";import { BackendVersion } from "#/components/features/backends/backend-version";import { BackendRow } from "#/components/features/backends/backend-row";@@ -72,17 +76,75 @@ vi.mock("#/hooks/query/use-settings", () => ({}),}));-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>,);}+function navigationAt(currentPath: string): NavigationContextValue {+ return {+ currentPath,+ conversationId: null,+ isNavigating: false,+ navigate: vi.fn(),+ };+}++function ActiveBackendName() {+ const { active } = useActiveBackendContext();+ return <span data-testid="active-backend-name">{active.backend.name}</span>;+}++/**+ * Seed a second backend (which becomes active, BM-001) next to the default+ * Local backend, then select the Local row once its health probe connects.+ */+async function selectLocalRowFromSecondBackend(+ navigation: NavigationContextValue,+) {+ const user = userEvent.setup();+ renderWithProviders(+ <TestSeed+ onMount={(ctx) => {+ ctx.addBackend({+ name: "Second",+ host: "http://localhost:9000",+ apiKey: "second-key",+ kind: "local",+ });+ }}+ >+ <ActiveBackendName />+ <ManageBackendsModal onClose={vi.fn()} />+ </TestSeed>,+ navigation,+ );+ expect(await screen.findByTestId("active-backend-name")).toHaveTextContent(+ "Second",+ );+ const localRowButton = within(+ screen.getByTestId("manage-backends-row-Local"),+ ).getAllByRole("button")[0];+ await waitFor(() => expect(localRowButton).toBeEnabled());++ await user.click(localRowButton);+}+function TestSeed({onMount,children,@@ -201,6 +263,52 @@ describe("ManageBackendsModal", () => {).toHaveTextContent("Agent Canvas requires agent-server 1.51.0 or newer");});+ // @spec BM-002 β Switching backends from Manage backends redirects a+ // backend-scoped detail page to its section list, like the selector.+ it.each([+ ["/conversations/conv-on-second", "/conversations"],+ ["/automations/automation-on-second", "/automations"],+ ])(+ "redirects %s to %s when another backend is selected from its row",+ async (detailPath, listPath) => {+ const navigation = navigationAt(detailPath);++ await selectLocalRowFromSecondBackend(navigation);++ expect(screen.getByTestId("active-backend-name")).toHaveTextContent(+ "Local",+ );+ expect(navigation.navigate).toHaveBeenCalledExactlyOnceWith(listPath);+ },+ );++ it("switches backends without redirecting when not on a detail page", async () => {+ const navigation = navigationAt("/conversations");++ await selectLocalRowFromSecondBackend(navigation);++ expect(screen.getByTestId("active-backend-name")).toHaveTextContent(+ "Local",+ );+ expect(navigation.navigate).not.toHaveBeenCalled();+ });++ it("does not redirect when the already-active backend is selected", async () => {+ const user = userEvent.setup();+ const onClose = vi.fn();+ const navigation = navigationAt("/conversations/conv-on-local");+ renderWithProviders(<ManageBackendsModal onClose={onClose} />, navigation);+ const localRowButton = within(+ await screen.findByTestId("manage-backends-row-Local"),+ ).getAllByRole("button")[0];+ await waitFor(() => expect(localRowButton).toBeEnabled());++ await user.click(localRowButton);++ expect(onClose).toHaveBeenCalledTimes(1);+ expect(navigation.navigate).not.toHaveBeenCalled();+ });+it("closes when the header close button is clicked", async () => {const user = userEvent.setup();const onClose = vi.fn();diff --git a/src/components/features/backends/backend-form-modal.tsx b/src/components/features/backends/backend-form-modal.tsxindex 6e830614e1..cccc2e3469 100644--- a/src/components/features/backends/backend-form-modal.tsx+++ b/src/components/features/backends/backend-form-modal.tsx@@ -14,7 +14,6 @@ import { BrandButton } from "#/components/features/settings/brand-button";import { SettingsInput } from "#/components/features/settings/settings-input";import { SegmentedToggle } from "#/components/features/files-tab/segmented-toggle";import { useActiveBackendContext } from "#/contexts/active-backend-context";-import { useNavigation } from "#/context/navigation-context";import { useBackendsHealth } from "#/hooks/query/use-backends-health";import { useTracking } from "#/hooks/use-tracking";import type { CloudConnectionSource } from "#/services/cloud-funnel-analytics";@@ -39,6 +38,7 @@ import ServerIcon from "#/icons/server.svg?react";import { getBackendStatusLabel } from "./backend-status-label";import { BackendStatusDot } from "./backend-status-dot";import { DeviceFlowAuth } from "./device-flow-auth";+import { useBackendSwitchRedirect } from "./use-backend-switch-redirect";export type BackendFormMode = "add" | "edit";@@ -690,21 +690,6 @@ 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]);-}-interface BackendConnectionOptionsProps {onConnected: (payload: BackendFormSubmitPayload,@@ -1488,7 +1473,9 @@ function AddBackendConnectionOptions({source: BackendAddedSource;}) {const { addBackend } = useActiveBackendContext();- const redirectAfterAdd = useRedirectAfterAddBackend();+ // @spec BM-002 β Adding a backend auto-switches the active selection to it+ // (BM-001), so leave any detail page that belongs to the previous backend.+ const redirectAfterAdd = useBackendSwitchRedirect();const { trackBackendAdded } = useTracking();const lockedCloudHost = getLockedCloudHost();diff --git a/src/components/features/backends/manage-backends-modal.tsx b/src/components/features/backends/manage-backends-modal.tsxindex 56e6404cbd..8dae25d50d 100644--- a/src/components/features/backends/manage-backends-modal.tsx+++ b/src/components/features/backends/manage-backends-modal.tsx@@ -22,6 +22,7 @@ import { modalTitleLgClassName } from "#/utils/modal-classes";import { BackendFormModal } from "./backend-form-modal";import { BackendRow } from "./backend-row";import { DeviceFlowAuth } from "./device-flow-auth";+import { useBackendSwitchRedirect } from "./use-backend-switch-redirect";interface ManageBackendsModalProps {onClose: () => void;@@ -101,14 +102,18 @@ export function ManageBackendsModal({setPendingRemoval(null);};+ const redirectAfterSwitch = useBackendSwitchRedirect();+const handleSelectBackend = React.useCallback((backend: Backend) => {if (active.backend.id !== backend.id || active.orgId !== null) {+ // @spec BM-002 β Switching backends keeps the user on the same page+ redirectAfterSwitch();setActive(backend.id);}onClose();},- [active.backend.id, active.orgId, onClose, setActive],+ [active.backend.id, active.orgId, onClose, redirectAfterSwitch, setActive],);const handleCloudLogin = React.useCallback(diff --git a/src/components/features/backends/use-backend-switch-redirect.ts b/src/components/features/backends/use-backend-switch-redirect.tsnew file mode 100644index 0000000000..f9d2c04ba2--- /dev/null+++ b/src/components/features/backends/use-backend-switch-redirect.ts@@ -0,0 +1,18 @@+import React from "react";+import { useNavigation } from "#/context/navigation-context";++/**+ * @spec BM-002 β Switching the active backend (or org) makes a+ * backend-scoped detail page the user is viewing belong to the previous+ * backend. Redirect to that section's list so they never see stale data,+ * mirroring the switch-backend redirect in BackendSelector. Call it in the+ * same handler that changes the active backend.+ */+export function useBackendSwitchRedirect() {+ const { currentPath, navigate } = useNavigation();+ return React.useCallback(() => {+ if (/^\/automations\/[^/]+/.test(currentPath)) navigate("/automations");+ else if (/^\/conversations\/[^/]+/.test(currentPath))+ navigate("/conversations");+ }, [currentPath, navigate]);+}