fix: Close notifications popover when navigating to another page (#14654)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-19 17:57:19 -05:00
1 parent adba69af33
commit 97a00a37ce
4 files changed
+133 -15

No files matched your search

+6 -15
View File
@@ -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 (
<DialogProvider>
@@ -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<Props> = ({ 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();
+92
View File
@@ -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<typeof createMemoryHistory>,
props: Props
) =>
act(() =>
root.render(
<Router history={history}>
<Harness {...props} />
</Router>
)
);
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);
});
});
+31
View File
@@ -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]);
}