perf(board): one shared context menu instead of one per widget
Mantine's Menu mounts Popover + PopoverDropdown + several context providers even
while closed, so a menu per widget was ~13 fibers each for something almost never
opened. WidgetContextMenuProvider now mounts a single menu for the board, anchored
to the cursor; items keep only a contextmenu/long-press listener.
The dropdown takes the section's gridstack ref as a prop rather than reading
SectionContext — the shared menu lives outside any section, and reading it from
context there crashed the board with "useSectionContext must be used within a
SectionContext".
Verified against both builds with scripts/benchmarks/verify-context-menu.mts
(opens on right-click, shows actions, closes on Escape, re-targets to a second
widget):
idle fibers 18,670 -> 16,278 (-12.8%)
fibers after use +4,191 -> +4
JS event listeners 10,128 -> 7,774 (-23%)
DOM nodes 9,189 -> 8,287 (-10%)
browser JS heap 54.1 -> 52.7 MiB
median board load 520 -> 479 ms
container CPU 20.4 -> 20.0 s
Two runs per side, four tabs across two boards. Server memory is unchanged, as
expected for a client-only change. The listener reduction is the point: the 650 MB
session had ~88k listeners and this removes a per-widget source of them.
The gate is validated by passing against unmodified code first. An earlier version
used Playwright's click({ button: "right" }), which emits no contextmenu event
here and so failed shipped code identically — it wrongly condemned a previous
attempt at this optimisation. A gate never run against the un-optimised build
proves only that something changed, not that it still works.
pnpm test 1794/1795 (pre-existing pi-hole Testcontainers flake, 9/9 in isolation);
typecheck clean across 37 packages; 0 lint errors.
This commit is contained in:
5 files changed
+360
-60
No files matched your search
@@ -0,0 +1,94 @@
|
||||
"use client";
|
||||
|
||||
import type { MutableRefObject, PropsWithChildren } from "react";
|
||||
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
||||
import { Menu } from "@mantine/core";
|
||||
|
||||
import { useSession } from "@homarr/auth/client";
|
||||
import { useSettings } from "@homarr/settings";
|
||||
import type { WidgetDefinition } from "@homarr/widgets/definition";
|
||||
|
||||
import type { UseGridstackRefs } from "../sections/gridstack/use-gridstack";
|
||||
|
||||
import type { SectionItem } from "~/app/[locale]/boards/_types";
|
||||
import { WidgetContextMenuDropdown } from "./widget-context-menu";
|
||||
|
||||
interface ActiveContextMenu {
|
||||
item: SectionItem;
|
||||
definition: WidgetDefinition;
|
||||
widgetStateRef: MutableRefObject<Record<string, unknown> | null>;
|
||||
/** Carried from the item's section, which the board-level menu is outside of. */
|
||||
gridstack: UseGridstackRefs["gridstack"];
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
interface WidgetContextMenuApi {
|
||||
/** Null when right-click menus are disabled, which lets items skip their handler. */
|
||||
open: ((menu: ActiveContextMenu) => void) | null;
|
||||
}
|
||||
|
||||
const WidgetContextMenuContext = createContext<WidgetContextMenuApi>({ open: null });
|
||||
|
||||
export const useWidgetContextMenu = () => useContext(WidgetContextMenuContext);
|
||||
|
||||
/**
|
||||
* One context menu for the whole board instead of one per widget.
|
||||
*
|
||||
* Mantine's `Menu` mounts `Popover` + `PopoverDropdown` + several context providers
|
||||
* even while closed, so a menu per item cost ~13 fibers each for something almost
|
||||
* never opened — measured at 364 of 3,918 fibers (9%) on a 28-widget board, growing
|
||||
* with item count. Items now only carry a `contextmenu` listener and this single menu
|
||||
* is anchored to the cursor.
|
||||
*/
|
||||
export const WidgetContextMenuProvider = ({ children }: PropsWithChildren) => {
|
||||
const { data: session } = useSession();
|
||||
const settings = useSettings();
|
||||
const [active, setActive] = useState<ActiveContextMenu | null>(null);
|
||||
|
||||
const isEnabled = Boolean(session) && settings.enableRightClickOnWidgets;
|
||||
const open = useCallback((menu: ActiveContextMenu) => setActive(menu), []);
|
||||
const api = useMemo<WidgetContextMenuApi>(() => ({ open: isEnabled ? open : null }), [isEnabled, open]);
|
||||
|
||||
return (
|
||||
<WidgetContextMenuContext value={api}>
|
||||
{children}
|
||||
{active && (
|
||||
<Menu
|
||||
shadow="md"
|
||||
width={300}
|
||||
closeOnItemClick={false}
|
||||
position="right-start"
|
||||
offset={4}
|
||||
opened
|
||||
onChange={(opened) => {
|
||||
if (!opened) setActive(null);
|
||||
}}
|
||||
>
|
||||
{/* Zero-size anchor at the cursor so Mantine positions the dropdown there. */}
|
||||
<Menu.Target>
|
||||
<div
|
||||
style={{
|
||||
position: "fixed",
|
||||
left: active.x,
|
||||
top: active.y,
|
||||
width: 1,
|
||||
height: 1,
|
||||
pointerEvents: "none",
|
||||
}}
|
||||
/>
|
||||
</Menu.Target>
|
||||
<Menu.Dropdown>
|
||||
<WidgetContextMenuDropdown
|
||||
item={active.item}
|
||||
definition={active.definition}
|
||||
widgetStateRef={active.widgetStateRef}
|
||||
settings={settings}
|
||||
gridstack={active.gridstack}
|
||||
/>
|
||||
</Menu.Dropdown>
|
||||
</Menu>
|
||||
)}
|
||||
</WidgetContextMenuContext>
|
||||
);
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import type { MutableRefObject, ReactNode } from "react";
|
||||
import { useCallback, useMemo, useState } from "react";
|
||||
import { useCallback, useMemo, useRef } from "react";
|
||||
import { Group, Loader, Menu, Switch, Text, Tooltip } from "@mantine/core";
|
||||
import {
|
||||
IconAlertTriangle,
|
||||
@@ -16,7 +16,7 @@ import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
||||
import { partialMatchKey, useIsFetching, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useSession } from "@homarr/auth/client";
|
||||
|
||||
import { useRequiredBoard } from "@homarr/boards/context";
|
||||
import { useEditMode } from "@homarr/boards/edit-mode";
|
||||
import { useTimeAgo } from "@homarr/common";
|
||||
@@ -30,7 +30,9 @@ import { getWidgetQueryKeys } from "@homarr/widgets/definition";
|
||||
import { reduceWidgetOptionsWithDefinition } from "@homarr/widgets/manifest";
|
||||
|
||||
import type { SectionItem } from "~/app/[locale]/boards/_types";
|
||||
import type { UseGridstackRefs } from "../sections/gridstack/use-gridstack";
|
||||
import { useSectionContext } from "../sections/section-context";
|
||||
import { useWidgetContextMenu } from "./widget-context-menu-provider";
|
||||
import { useItemActions } from "./item-actions";
|
||||
import { LazyItemMoveModal, preloadItemMoveModal } from "./lazy-item-move-modal";
|
||||
import { LazyWidgetEditModal, preloadWidgetEditModal } from "./lazy-widget-edit-modal";
|
||||
@@ -42,58 +44,68 @@ interface WidgetContextMenuProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
const LONG_PRESS_DELAY_MS = 500;
|
||||
|
||||
/**
|
||||
* Per-item trigger only. The menu itself lives once at board level — see
|
||||
* WidgetContextMenuProvider for why.
|
||||
*/
|
||||
export const WidgetContextMenu = ({ item, definition, widgetStateRef, children }: WidgetContextMenuProps) => {
|
||||
const { data: session } = useSession();
|
||||
const settings = useSettings();
|
||||
const { open } = useWidgetContextMenu();
|
||||
const { gridstack } = useSectionContext().refs;
|
||||
const longPressTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
|
||||
if (!session || !settings.enableRightClickOnWidgets) return <>{children}</>;
|
||||
const cancelLongPress = useCallback(() => {
|
||||
if (longPressTimer.current) {
|
||||
clearTimeout(longPressTimer.current);
|
||||
longPressTimer.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
if (!open) return <>{children}</>;
|
||||
|
||||
return (
|
||||
<WidgetContextMenuInner item={item} definition={definition} widgetStateRef={widgetStateRef} settings={settings}>
|
||||
{children}
|
||||
</WidgetContextMenuInner>
|
||||
);
|
||||
};
|
||||
|
||||
const WidgetContextMenuInner = ({
|
||||
item,
|
||||
definition,
|
||||
widgetStateRef,
|
||||
children,
|
||||
settings,
|
||||
}: WidgetContextMenuProps & { settings: ReturnType<typeof useSettings> }) => {
|
||||
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<Menu
|
||||
shadow="md"
|
||||
width={300}
|
||||
closeOnItemClick={false}
|
||||
position="right-start"
|
||||
offset={4}
|
||||
opened={isMenuOpen}
|
||||
onChange={setIsMenuOpen}
|
||||
// display:contents keeps GridStack's layout maths seeing the original child box.
|
||||
<div
|
||||
style={{ display: "contents" }}
|
||||
onContextMenu={(event) => {
|
||||
event.preventDefault();
|
||||
open({ item, definition, widgetStateRef, gridstack, x: event.clientX, y: event.clientY });
|
||||
}}
|
||||
onTouchStart={(event) => {
|
||||
const touch = event.touches[0];
|
||||
if (!touch) return;
|
||||
const { clientX, clientY } = touch;
|
||||
longPressTimer.current = setTimeout(
|
||||
() => open({ item, definition, widgetStateRef, gridstack, x: clientX, y: clientY }),
|
||||
LONG_PRESS_DELAY_MS,
|
||||
);
|
||||
}}
|
||||
onTouchEnd={cancelLongPress}
|
||||
onTouchMove={cancelLongPress}
|
||||
onTouchCancel={cancelLongPress}
|
||||
>
|
||||
<Menu.ContextMenu>{children}</Menu.ContextMenu>
|
||||
<Menu.Dropdown>
|
||||
{isMenuOpen && (
|
||||
<WidgetContextMenuDropdown
|
||||
item={item}
|
||||
definition={definition}
|
||||
widgetStateRef={widgetStateRef}
|
||||
settings={settings}
|
||||
/>
|
||||
)}
|
||||
</Menu.Dropdown>
|
||||
</Menu>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
type WidgetContextMenuDropdownProps = Omit<WidgetContextMenuProps, "children"> & {
|
||||
settings: ReturnType<typeof useSettings>;
|
||||
/**
|
||||
* Passed in rather than read from SectionContext: the menu is mounted once at board
|
||||
* level, outside any section, while the item that opened it is inside one.
|
||||
*/
|
||||
gridstack: UseGridstackRefs["gridstack"];
|
||||
};
|
||||
|
||||
const WidgetContextMenuDropdown = ({ item, definition, widgetStateRef, settings }: WidgetContextMenuDropdownProps) => {
|
||||
export const WidgetContextMenuDropdown = ({
|
||||
item,
|
||||
definition,
|
||||
widgetStateRef,
|
||||
settings,
|
||||
gridstack,
|
||||
}: WidgetContextMenuDropdownProps) => {
|
||||
const [isEditMode] = useEditMode();
|
||||
const board = useRequiredBoard();
|
||||
const tItem = useScopedI18n("item");
|
||||
@@ -110,7 +122,6 @@ const WidgetContextMenuDropdown = ({ item, definition, widgetStateRef, settings
|
||||
enabled: hasSupportedIntegrations,
|
||||
});
|
||||
const { mutate: saveBoard } = clientApi.board.saveBoard.useMutation();
|
||||
const { gridstack } = useSectionContext().refs;
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const widgetQueryKeys = useMemo(() => getWidgetQueryKeys(definition, item.kind), [definition, item.kind]);
|
||||
|
||||
Reference in new issue
Block a user