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): guard Tally popup script removal to prevent NotFoundError (#14323)
seer-by-sentry[bot]2743ba8bd3aa
diff --git a/autogpt_platform/frontend/src/components/molecules/TallyPoup/__tests__/TallyPopup.test.tsx b/autogpt_platform/frontend/src/components/molecules/TallyPoup/__tests__/TallyPopup.test.tsxnew file mode 100644index 0000000000..ed895ffd50--- /dev/null+++ b/autogpt_platform/frontend/src/components/molecules/TallyPoup/__tests__/TallyPopup.test.tsx@@ -0,0 +1,44 @@+import { act, render } from "@testing-library/react";+import { StrictMode } from "react";+import { describe, expect, it, vi } from "vitest";+import TallyPopupSimple from "../TallyPopup";++vi.mock("@/lib/auth/actions", () => ({+ getCurrentUser: vi.fn().mockResolvedValue({ user: null }),+}));++vi.mock("@sentry/nextjs", () => ({+ getReplay: vi.fn(),+}));++const scriptSelector = 'script[src="https://tally.so/widgets/embed.js"]';++describe("Tally popup cleanup", () => {+ it("removes its script on unmount after StrictMode remounts", async () => {+ const { unmount } = render(+ <StrictMode>+ <TallyPopupSimple />+ </StrictMode>,+ );+ await act(async () => {});++ expect(document.head.querySelectorAll(scriptSelector)).toHaveLength(1);+ unmount();+ expect(document.head.querySelector(scriptSelector)).toBeNull();+ });++ it("still removes the message listener when its script is already detached", async () => {+ const removeListener = vi.spyOn(window, "removeEventListener");+ const { unmount } = render(<TallyPopupSimple />);+ await act(async () => {});+ const script = document.head.querySelector(scriptSelector);+ expect(script).not.toBeNull();+ script?.remove();++ expect(unmount).not.toThrow();+ expect(removeListener).toHaveBeenCalledWith(+ "message",+ expect.any(Function),+ );+ });+});diff --git a/autogpt_platform/frontend/src/components/molecules/TallyPoup/useTallyPopup.ts b/autogpt_platform/frontend/src/components/molecules/TallyPoup/useTallyPopup.tsindex c4de42130e..d5326b029a 100644--- a/autogpt_platform/frontend/src/components/molecules/TallyPoup/useTallyPopup.ts+++ b/autogpt_platform/frontend/src/components/molecules/TallyPoup/useTallyPopup.ts@@ -92,7 +92,7 @@ export function useTallyPopup() {window.addEventListener("message", handleTallyMessage);return () => {- document.head.removeChild(script);+ script.parentNode?.removeChild(script);window.removeEventListener("message", handleTallyMessage);};}, []);