Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): close delete organization dialog and navigate right after deletion (#19568)

Tobias Wochinger86e437e4adfc
diff --git a/web/src/features/organizations/components/DeleteOrganizationDialogController.clienttest.tsx b/web/src/features/organizations/components/DeleteOrganizationDialogController.clienttest.tsx
new file mode 100644
index 0000000000..8c6948cc16
--- /dev/null
+++ b/web/src/features/organizations/components/DeleteOrganizationDialogController.clienttest.tsx
@@ -0,0 +1,101 @@
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { beforeAll, describe, expect, it, vi } from "vitest";
+
+import { DeleteOrganizationDialogController } from "./DeleteOrganizationDialogController";
+
+const mocks = vi.hoisted(() => ({
+ calls: [] as string[],
+ mutateAsync: vi.fn(async () => true),
+ showSuccessToast: vi.fn(),
+}));
+
+vi.mock("next/router", () => ({
+ useRouter: () => ({
+ query: { organizationId: "org-1" },
+ push: async (url: string) => {
+ mocks.calls.push(`push:${url}`);
+ return true;
+ },
+ }),
+}));
+
+vi.mock("next-auth/react", () => ({
+ useSession: () => ({
+ status: "authenticated",
+ data: null,
+ update: async () => {
+ mocks.calls.push("session.update");
+ return null;
+ },
+ }),
+}));
+
+vi.mock("@/src/features/organizations/hooks", () => ({
+ useQueryOrganization: () => ({ id: "org-1", name: "Acme", projects: [] }),
+}));
+
+vi.mock("@/src/features/rbac", () => ({
+ useHasOrganizationAccess: () => true,
+}));
+
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+
+vi.mock("@/src/features/notifications", () => ({
+ showSuccessToast: mocks.showSuccessToast,
+}));
+
+vi.mock("@/src/utils/api", () => ({
+ api: {
+ organizations: {
+ delete: {
+ useMutation: () => ({
+ mutateAsync: mocks.mutateAsync,
+ isPending: false,
+ }),
+ },
+ },
+ },
+ reportNonTrpcError: vi.fn(),
+}));
+
+describe("DeleteOrganizationDialogController", () => {
+ beforeAll(() => {
+ vi.stubGlobal(
+ "ResizeObserver",
+ class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ );
+ });
+
+ it("closes the dialog and navigates home with a refreshed session after deletion", async () => {
+ render(
+ <DeleteOrganizationDialogController>
+ {({ openDialog }) => (
+ <button type="button" onClick={openDialog}>
+ Open
+ </button>
+ )}
+ </DeleteOrganizationDialogController>,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Open" }));
+ fireEvent.change(screen.getByPlaceholderText("acme"), {
+ target: { value: "acme" },
+ });
+ fireEvent.click(
+ screen.getByRole("button", { name: "Delete Organization" }),
+ );
+
+ await waitFor(() =>
+ expect(mocks.calls).toEqual(["session.update", "push:/"]),
+ );
+ expect(mocks.mutateAsync).toHaveBeenCalledWith({ orgId: "org-1" });
+ expect(mocks.showSuccessToast).toHaveBeenCalledOnce();
+ expect(screen.queryByRole("dialog")).toBeNull();
+ });
+});
diff --git a/web/src/features/organizations/components/DeleteOrganizationDialogController.tsx b/web/src/features/organizations/components/DeleteOrganizationDialogController.tsx
index 62ab712a76..26c3424abd 100644
--- a/web/src/features/organizations/components/DeleteOrganizationDialogController.tsx
+++ b/web/src/features/organizations/components/DeleteOrganizationDialogController.tsx
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-deprecated -- Legacy UI usage retained during migration to design-system components. */
import { showSuccessToast } from "@/src/features/notifications";
import { type ReactNode, useState } from "react";
+import { useRouter } from "next/router";
+import { useSession } from "next-auth/react";
import { Dialog, DialogContent } from "@/src/components/ui/dialog";
import { DeleteOrganizationDialogContent } from "@/src/features/organizations/components/DeleteOrganizationDialogContent";
@@ -8,7 +10,6 @@ import { usePostHogClientCapture } from "@/src/features/posthog-analytics";
import { useQueryOrganization } from "@/src/features/organizations/hooks";
import { useHasOrganizationAccess } from "@/src/features/rbac";
import { api, reportNonTrpcError } from "@/src/utils/api";
-import { env } from "@/src/env.mjs";
type DeleteOrganizationDialogControllerProps = {
children: (control: {
@@ -21,6 +22,8 @@ export function DeleteOrganizationDialogController({
children,
}: DeleteOrganizationDialogControllerProps) {
const [open, setOpen] = useState(false);
+ const router = useRouter();
+ const session = useSession();
const capture = usePostHogClientCapture();
const organization = useQueryOrganization();
const confirmMessage =
@@ -48,13 +51,15 @@ export function DeleteOrganizationDialogController({
orgId: organization.id,
});
capture("organization_settings:delete_organization");
+ setOpen(false);
showSuccessToast({
operation: "organization.delete",
title: "Organization Deleted",
description: "The organization has been successfully deleted.",
});
- await new Promise((resolve) => setTimeout(resolve, 5000));
- window.location.href = env.NEXT_PUBLIC_BASE_PATH ?? "/";
+ // Refresh the session first so the landing page no longer lists the deleted organization.
+ await session.update();
+ await router.push("/");
} catch (error) {
// tRPC failures were already classified + toasted by the react-query
// default onError; only report failures of the post-success work here.