From fca4467bdab003fbcd9b40607fae782fb34a5e0d Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Mon, 9 Sep 2024 22:46:37 +0530 Subject: [PATCH] Delay loading of document policies until document menu is opened (#7442) * fix: loading of doc policies should be delayed until menu is opened * fix: fetch upon hover and display menu only when data is loaded * fix: remove documents.isFetching * fix: wait for policies to load * fix: bifurcate * fix: className is never used at any callsite of * fix: MenuContext * fix: collection is derived from document * fix: mount MenuContent only when button is clicked * fix: jsdoc * fix: aria-label * fix: review --- app/components/DocumentBreadcrumb.tsx | 2 +- app/menus/DocumentMenu.tsx | 463 ++++++++++++--------- app/menus/MenuContext.tsx | 19 + app/models/base/Model.ts | 6 +- shared/i18n/locales/en_US/translation.json | 1 + 5 files changed, 293 insertions(+), 198 deletions(-) create mode 100644 app/menus/MenuContext.tsx diff --git a/app/components/DocumentBreadcrumb.tsx b/app/components/DocumentBreadcrumb.tsx index 60bc7c1314..9f87573aa2 100644 --- a/app/components/DocumentBreadcrumb.tsx +++ b/app/components/DocumentBreadcrumb.tsx @@ -71,7 +71,7 @@ const DocumentBreadcrumb: React.FC = ({ const can = usePolicy(collection); React.useEffect(() => { - void document.loadRelations(); + void document.loadRelations({ withoutPolicies: true }); }, [document]); let collectionNode: MenuInternalLink | undefined; diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index 2846700707..04fee5e12d 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -1,4 +1,6 @@ import capitalize from "lodash/capitalize"; +import isEmpty from "lodash/isEmpty"; +import isUndefined from "lodash/isUndefined"; import { observer } from "mobx-react"; import { EditIcon, InputIcon, RestoreIcon, SearchIcon } from "outline-icons"; import * as React from "react"; @@ -48,6 +50,7 @@ import { moveTemplate, } from "~/actions/definitions/documents"; import useActionContext from "~/hooks/useActionContext"; +import useBoolean from "~/hooks/useBoolean"; import useCurrentUser from "~/hooks/useCurrentUser"; import useMobile from "~/hooks/useMobile"; import usePolicy from "~/hooks/usePolicy"; @@ -55,68 +58,108 @@ import useRequest from "~/hooks/useRequest"; import useStores from "~/hooks/useStores"; import { MenuItem } from "~/types"; import { documentEditPath } from "~/utils/routeHelpers"; +import { MenuContext, useMenuContext } from "./MenuContext"; type Props = { + /** Document for which the menu is to be shown */ document: Document; - className?: string; isRevision?: boolean; /** Pass true if the document is currently being displayed */ showDisplayOptions?: boolean; + /** Whether to display menu as a modal */ modal?: boolean; + /** Whether to include the option of toggling embeds as menu item */ showToggleEmbeds?: boolean; showPin?: boolean; + /** Label for menu button */ label?: (props: MenuButtonHTMLProps) => React.ReactNode; + /** Invoked when the "Find and replace" menu item is clicked */ onFindAndReplace?: () => void; + /** Invoked when the "Rename" menu item is clicked */ onRename?: () => void; + /** Invoked when menu is opened */ onOpen?: () => void; + /** Invoked when menu is closed */ onClose?: () => void; }; -function DocumentMenu({ - document, - className, - modal = true, - showToggleEmbeds, - showDisplayOptions, - label, - onFindAndReplace, - onRename, - onOpen, - onClose, -}: Props) { - const user = useCurrentUser(); - const { policies, collections, documents, subscriptions } = useStores(); - const menu = useMenuState({ - modal, - unstable_preventOverflow: true, - unstable_fixed: true, - unstable_flip: true, - }); - const history = useHistory(); - const context = useActionContext({ - isContextMenu: true, - activeDocumentId: document.id, - activeCollectionId: document.collectionId ?? undefined, - }); +type MenuTriggerProps = { + label?: (props: MenuButtonHTMLProps) => React.ReactNode; + onTrigger: () => void; +}; + +const MenuTrigger: React.FC = ({ label, onTrigger }) => { const { t } = useTranslation(); - const isMobile = useMobile(); - const file = React.useRef(null); - const { data, loading, request } = useRequest(() => + + const { subscriptions } = useStores(); + const { model: document, menuState } = useMenuContext(); + + const { data, loading, error, request } = useRequest(() => subscriptions.fetchPage({ documentId: document.id, event: "documents.update", }) ); - const handleOpen = React.useCallback(async () => { - if (!data && !loading) { - await request(); + const handlePointerEnter = React.useCallback(() => { + if (isUndefined(data ?? error) && !loading) { + void request(); + void document.loadRelations(); } + }, [data, error, loading, request, document]); - if (onOpen) { - onOpen(); - } - }, [data, loading, onOpen, request]); + return label ? ( + + {label} + + ) : ( + + ); +}; + +type MenuContentProps = { + onOpen?: () => void; + onClose?: () => void; + onFindAndReplace?: () => void; + onRename?: () => void; + showDisplayOptions?: boolean; + showToggleEmbeds?: boolean; +}; + +const MenuContent: React.FC = ({ + onOpen, + onClose, + onFindAndReplace, + onRename, + showDisplayOptions, + showToggleEmbeds, +}) => { + const user = useCurrentUser(); + const { model: document, menuState } = useMenuContext(); + const can = usePolicy(document); + const { t } = useTranslation(); + const { policies, collections } = useStores(); + + const collection = document.collectionId + ? collections.get(document.collectionId) + : undefined; + + const context = useActionContext({ + isContextMenu: true, + activeDocumentId: document.id, + activeCollectionId: document.collectionId ?? undefined, + }); + + const isMobile = useMobile(); const handleRestore = React.useCallback( async ( @@ -135,10 +178,6 @@ function DocumentMenu({ [t, document] ); - const collection = document.collectionId - ? collections.get(document.collectionId) - : undefined; - const can = usePolicy(document); const restoreItems = React.useMemo( () => [ ...collections.orderedData.reduce((filtered, collection) => { @@ -161,6 +200,182 @@ function DocumentMenu({ ], [collections.orderedData, handleRestore, policies] ); + + return !isEmpty(can) ? ( + +