diff --git a/app/components/Dialogs.tsx b/app/components/Dialogs.tsx index b943401b32..195adba371 100644 --- a/app/components/Dialogs.tsx +++ b/app/components/Dialogs.tsx @@ -1,6 +1,6 @@ import { observer } from "mobx-react"; -import { Suspense, useEffect } from "react"; -import { useHistory } from "react-router-dom"; +import { Suspense } from "react"; +import { usePathnameChange } from "~/hooks/usePathnameChange"; import useStores from "~/hooks/useStores"; import lazyWithRetry from "@shared/utils/lazyWithRetry"; import { DialogProvider } from "./DialogContext"; @@ -10,23 +10,14 @@ const Modal = lazyWithRetry(() => import("~/components/Modal")); function Dialogs() { const { dialogs } = useStores(); - const history = useHistory(); const { guide, modalStack } = dialogs; const modals = [...modalStack]; // Close open modals when navigating to another page. - useEffect(() => { - let pathname = history.location.pathname; - - return history.listen((location) => { - if (location.pathname === pathname) { - return; - } - pathname = location.pathname; - dialogs.modalStack.forEach((modal) => modal.onClose?.()); - dialogs.closeAllModals(); - }); - }, [dialogs, history]); + usePathnameChange(() => { + dialogs.modalStack.forEach((modal) => modal.onClose?.()); + dialogs.closeAllModals(); + }); return ( diff --git a/app/components/Notifications/NotificationsPopover.tsx b/app/components/Notifications/NotificationsPopover.tsx index 1b826c010d..c51dd9b457 100644 --- a/app/components/Notifications/NotificationsPopover.tsx +++ b/app/components/Notifications/NotificationsPopover.tsx @@ -14,6 +14,7 @@ import { } from "~/components/primitives/Popover"; import useConsumeQueryParam from "~/hooks/useConsumeQueryParam"; import useMobile from "~/hooks/useMobile"; +import { usePathnameChange } from "~/hooks/usePathnameChange"; import useStores from "~/hooks/useStores"; import { isTruthyQueryValue } from "~/utils/urls"; import Notifications from "./Notifications"; @@ -39,6 +40,9 @@ const NotificationsPopover: React.FC = ({ children }: Props) => { setOpen(false); }, []); + // Close on navigation because the trigger can be hidden on the new page. + usePathnameChange(handleRequestClose, open); + const handleAutoFocus = useCallback((event: Event) => { // Prevent focus from moving to the popover content event.preventDefault(); diff --git a/app/hooks/usePathnameChange.test.tsx b/app/hooks/usePathnameChange.test.tsx new file mode 100644 index 0000000000..352461b557 --- /dev/null +++ b/app/hooks/usePathnameChange.test.tsx @@ -0,0 +1,92 @@ +import { createMemoryHistory } from "history"; +import { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { Router } from "react-router-dom"; +import { usePathnameChange } from "./usePathnameChange"; + +interface Props { + callback: () => void; + enabled?: boolean; +} + +function Harness({ callback, enabled }: Props) { + usePathnameChange(callback, enabled); + return null; +} + +describe("usePathnameChange", () => { + let container: HTMLDivElement; + let root: Root; + + beforeAll(() => { + Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true); + }); + + beforeEach(() => { + container = document.createElement("div"); + root = createRoot(container); + }); + + afterEach(() => { + act(() => root.unmount()); + }); + + const render = ( + history: ReturnType, + props: Props + ) => + act(() => + root.render( + + + + ) + ); + + it("calls the callback when the pathname changes", () => { + const history = createMemoryHistory({ initialEntries: ["/home"] }); + const callback = vi.fn(); + render(history, { callback }); + + act(() => history.push("/settings")); + expect(callback).toHaveBeenCalledTimes(1); + + act(() => history.push("/settings/notifications")); + expect(callback).toHaveBeenCalledTimes(2); + }); + + it("ignores changes to only the search or hash", () => { + const history = createMemoryHistory({ initialEntries: ["/home"] }); + const callback = vi.fn(); + render(history, { callback }); + + act(() => history.replace("/home?notifications=true")); + act(() => history.push("/home#section")); + expect(callback).not.toHaveBeenCalled(); + }); + + it("does not call the callback when disabled", () => { + const history = createMemoryHistory({ initialEntries: ["/home"] }); + const callback = vi.fn(); + render(history, { callback, enabled: false }); + + act(() => history.push("/settings")); + expect(callback).not.toHaveBeenCalled(); + + render(history, { callback, enabled: true }); + act(() => history.push("/settings/notifications")); + expect(callback).toHaveBeenCalledTimes(1); + }); + + it("calls the latest callback", () => { + const history = createMemoryHistory({ initialEntries: ["/home"] }); + const first = vi.fn(); + const second = vi.fn(); + render(history, { callback: first }); + render(history, { callback: second }); + + act(() => history.push("/settings")); + expect(first).not.toHaveBeenCalled(); + expect(second).toHaveBeenCalledTimes(1); + }); +}); diff --git a/app/hooks/usePathnameChange.ts b/app/hooks/usePathnameChange.ts new file mode 100644 index 0000000000..b4276192f3 --- /dev/null +++ b/app/hooks/usePathnameChange.ts @@ -0,0 +1,31 @@ +import { useEffect, useEffectEvent } from "react"; +import { useHistory } from "react-router-dom"; + +/** + * Calls the callback when the location pathname changes. Changes to only the + * search or hash do not call it. The component does not re-render on + * navigation, so the hook is safe to use in components that stay mounted. + * + * @param callback the function to call after the pathname changes. + * @param enabled whether to listen for changes, defaults to true. + */ +export function usePathnameChange(callback: () => void, enabled = true) { + const history = useHistory(); + const onChange = useEffectEvent(callback); + + useEffect(() => { + if (!enabled) { + return; + } + + let pathname = history.location.pathname; + + return history.listen((location) => { + if (location.pathname === pathname) { + return; + } + pathname = location.pathname; + onChange(); + }); + }, [enabled, history]); +}