mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
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
This commit is contained in:
1 parent
b77af9bda3
commit
fca4467bda
5 files changed
+293
-198
No files matched your search
@@ -71,7 +71,7 @@ const DocumentBreadcrumb: React.FC<Props> = ({
|
||||
const can = usePolicy(collection);
|
||||
|
||||
React.useEffect(() => {
|
||||
void document.loadRelations();
|
||||
void document.loadRelations({ withoutPolicies: true });
|
||||
}, [document]);
|
||||
|
||||
let collectionNode: MenuInternalLink | undefined;
|
||||
|
||||
+268
-195
@@ -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<MenuTriggerProps> = ({ label, onTrigger }) => {
|
||||
const { t } = useTranslation();
|
||||
const isMobile = useMobile();
|
||||
const file = React.useRef<HTMLInputElement>(null);
|
||||
const { data, loading, request } = useRequest(() =>
|
||||
|
||||
const { subscriptions } = useStores();
|
||||
const { model: document, menuState } = useMenuContext<Document>();
|
||||
|
||||
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 ? (
|
||||
<MenuButton
|
||||
{...menuState}
|
||||
onPointerEnter={handlePointerEnter}
|
||||
onClick={onTrigger}
|
||||
>
|
||||
{label}
|
||||
</MenuButton>
|
||||
) : (
|
||||
<OverflowMenuButton
|
||||
aria-label={t("Show document menu")}
|
||||
onPointerEnter={handlePointerEnter}
|
||||
onClick={onTrigger}
|
||||
{...menuState}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
type MenuContentProps = {
|
||||
onOpen?: () => void;
|
||||
onClose?: () => void;
|
||||
onFindAndReplace?: () => void;
|
||||
onRename?: () => void;
|
||||
showDisplayOptions?: boolean;
|
||||
showToggleEmbeds?: boolean;
|
||||
};
|
||||
|
||||
const MenuContent: React.FC<MenuContentProps> = ({
|
||||
onOpen,
|
||||
onClose,
|
||||
onFindAndReplace,
|
||||
onRename,
|
||||
showDisplayOptions,
|
||||
showToggleEmbeds,
|
||||
}) => {
|
||||
const user = useCurrentUser();
|
||||
const { model: document, menuState } = useMenuContext<Document>();
|
||||
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<MenuItem[]>((filtered, collection) => {
|
||||
@@ -161,6 +200,182 @@ function DocumentMenu({
|
||||
],
|
||||
[collections.orderedData, handleRestore, policies]
|
||||
);
|
||||
|
||||
return !isEmpty(can) ? (
|
||||
<ContextMenu
|
||||
{...menuState}
|
||||
aria-label={t("Document options")}
|
||||
onOpen={onOpen}
|
||||
onClose={onClose}
|
||||
>
|
||||
<Template
|
||||
{...menuState}
|
||||
items={[
|
||||
{
|
||||
type: "button",
|
||||
title: t("Restore"),
|
||||
visible:
|
||||
((document.isWorkspaceTemplate || !!collection) && can.restore) ||
|
||||
!!can.unarchive,
|
||||
onClick: (ev) => handleRestore(ev),
|
||||
icon: <RestoreIcon />,
|
||||
},
|
||||
{
|
||||
type: "submenu",
|
||||
title: t("Restore"),
|
||||
visible:
|
||||
!document.isWorkspaceTemplate &&
|
||||
!collection &&
|
||||
!!can.restore &&
|
||||
restoreItems.length !== 0,
|
||||
style: {
|
||||
left: -170,
|
||||
position: "relative",
|
||||
top: -40,
|
||||
},
|
||||
icon: <RestoreIcon />,
|
||||
hover: true,
|
||||
items: [
|
||||
{
|
||||
type: "heading",
|
||||
title: t("Choose a collection"),
|
||||
},
|
||||
...restoreItems,
|
||||
],
|
||||
},
|
||||
actionToMenuItem(starDocument, context),
|
||||
actionToMenuItem(unstarDocument, context),
|
||||
actionToMenuItem(subscribeDocument, context),
|
||||
actionToMenuItem(unsubscribeDocument, context),
|
||||
{
|
||||
type: "button",
|
||||
title: `${t("Find and replace")}…`,
|
||||
visible: !!onFindAndReplace && isMobile,
|
||||
onClick: () => onFindAndReplace?.(),
|
||||
icon: <SearchIcon />,
|
||||
},
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
{
|
||||
type: "route",
|
||||
title: t("Edit"),
|
||||
to: documentEditPath(document),
|
||||
visible:
|
||||
!!can.update && user.separateEditMode && !document.template,
|
||||
icon: <EditIcon />,
|
||||
},
|
||||
{
|
||||
type: "button",
|
||||
title: `${t("Rename")}…`,
|
||||
visible: !!can.update && !user.separateEditMode && !!onRename,
|
||||
onClick: () => onRename?.(),
|
||||
icon: <InputIcon />,
|
||||
},
|
||||
actionToMenuItem(shareDocument, context),
|
||||
actionToMenuItem(createNestedDocument, context),
|
||||
actionToMenuItem(importDocument, context),
|
||||
actionToMenuItem(createTemplateFromDocument, context),
|
||||
actionToMenuItem(duplicateDocument, context),
|
||||
actionToMenuItem(publishDocument, context),
|
||||
actionToMenuItem(unpublishDocument, context),
|
||||
actionToMenuItem(archiveDocument, context),
|
||||
actionToMenuItem(moveDocument, context),
|
||||
actionToMenuItem(moveTemplate, context),
|
||||
actionToMenuItem(pinDocument, context),
|
||||
actionToMenuItem(createDocumentFromTemplate, context),
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
actionToMenuItem(openDocumentComments, context),
|
||||
actionToMenuItem(openDocumentHistory, context),
|
||||
actionToMenuItem(openDocumentInsights, context),
|
||||
actionToMenuItem(downloadDocument, context),
|
||||
actionToMenuItem(copyDocument, context),
|
||||
actionToMenuItem(printDocument, context),
|
||||
actionToMenuItem(searchInDocument, context),
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
actionToMenuItem(deleteDocument, context),
|
||||
actionToMenuItem(permanentlyDeleteDocument, context),
|
||||
]}
|
||||
/>
|
||||
{(showDisplayOptions || showToggleEmbeds) && can.update && (
|
||||
<>
|
||||
<Separator />
|
||||
<DisplayOptions>
|
||||
{showToggleEmbeds && (
|
||||
<Style>
|
||||
<ToggleMenuItem
|
||||
width={26}
|
||||
height={14}
|
||||
label={t("Enable embeds")}
|
||||
labelPosition="left"
|
||||
checked={!document.embedsDisabled}
|
||||
onChange={
|
||||
document.embedsDisabled
|
||||
? document.enableEmbeds
|
||||
: document.disableEmbeds
|
||||
}
|
||||
/>
|
||||
</Style>
|
||||
)}
|
||||
{showDisplayOptions && !isMobile && (
|
||||
<Style>
|
||||
<ToggleMenuItem
|
||||
width={26}
|
||||
height={14}
|
||||
label={t("Full width")}
|
||||
labelPosition="left"
|
||||
checked={document.fullWidth}
|
||||
onChange={(ev) => {
|
||||
const fullWidth = ev.currentTarget.checked;
|
||||
user.setPreference(
|
||||
UserPreference.FullWidthDocuments,
|
||||
fullWidth
|
||||
);
|
||||
void user.save();
|
||||
document.fullWidth = fullWidth;
|
||||
void document.save();
|
||||
}}
|
||||
/>
|
||||
</Style>
|
||||
)}
|
||||
</DisplayOptions>
|
||||
</>
|
||||
)}
|
||||
</ContextMenu>
|
||||
) : null;
|
||||
};
|
||||
|
||||
function DocumentMenu({
|
||||
document,
|
||||
modal = true,
|
||||
showToggleEmbeds,
|
||||
showDisplayOptions,
|
||||
label,
|
||||
onRename,
|
||||
onOpen,
|
||||
onClose,
|
||||
}: Props) {
|
||||
const { collections, documents } = useStores();
|
||||
const menuState = useMenuState({
|
||||
modal,
|
||||
unstable_preventOverflow: true,
|
||||
unstable_fixed: true,
|
||||
unstable_flip: true,
|
||||
});
|
||||
const history = useHistory();
|
||||
|
||||
const { t } = useTranslation();
|
||||
const [isMenuVisible, showMenu] = useBoolean(false);
|
||||
const file = React.useRef<HTMLInputElement>(null);
|
||||
|
||||
const collection = document.collectionId
|
||||
? collections.get(document.collectionId)
|
||||
: undefined;
|
||||
|
||||
const stopPropagation = React.useCallback((ev: React.SyntheticEvent) => {
|
||||
ev.stopPropagation();
|
||||
}, []);
|
||||
@@ -213,160 +428,18 @@ function DocumentMenu({
|
||||
/>
|
||||
</label>
|
||||
</VisuallyHidden>
|
||||
{label ? (
|
||||
<MenuButton {...menu}>{label}</MenuButton>
|
||||
) : (
|
||||
<OverflowMenuButton
|
||||
className={className}
|
||||
aria-label={t("Show menu")}
|
||||
{...menu}
|
||||
/>
|
||||
)}
|
||||
<ContextMenu
|
||||
{...menu}
|
||||
aria-label={t("Document options")}
|
||||
onOpen={handleOpen}
|
||||
onClose={onClose}
|
||||
>
|
||||
<Template
|
||||
{...menu}
|
||||
items={[
|
||||
{
|
||||
type: "button",
|
||||
title: t("Restore"),
|
||||
visible:
|
||||
((document.isWorkspaceTemplate || !!collection) &&
|
||||
can.restore) ||
|
||||
!!can.unarchive,
|
||||
onClick: (ev) => handleRestore(ev),
|
||||
icon: <RestoreIcon />,
|
||||
},
|
||||
{
|
||||
type: "submenu",
|
||||
title: t("Restore"),
|
||||
visible:
|
||||
!document.isWorkspaceTemplate &&
|
||||
!collection &&
|
||||
!!can.restore &&
|
||||
restoreItems.length !== 0,
|
||||
style: {
|
||||
left: -170,
|
||||
position: "relative",
|
||||
top: -40,
|
||||
},
|
||||
icon: <RestoreIcon />,
|
||||
hover: true,
|
||||
items: [
|
||||
{
|
||||
type: "heading",
|
||||
title: t("Choose a collection"),
|
||||
},
|
||||
...restoreItems,
|
||||
],
|
||||
},
|
||||
actionToMenuItem(starDocument, context),
|
||||
actionToMenuItem(unstarDocument, context),
|
||||
actionToMenuItem(subscribeDocument, context),
|
||||
actionToMenuItem(unsubscribeDocument, context),
|
||||
{
|
||||
type: "button",
|
||||
title: `${t("Find and replace")}…`,
|
||||
visible: !!onFindAndReplace && isMobile,
|
||||
onClick: () => onFindAndReplace?.(),
|
||||
icon: <SearchIcon />,
|
||||
},
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
{
|
||||
type: "route",
|
||||
title: t("Edit"),
|
||||
to: documentEditPath(document),
|
||||
visible:
|
||||
!!can.update && user.separateEditMode && !document.template,
|
||||
icon: <EditIcon />,
|
||||
},
|
||||
{
|
||||
type: "button",
|
||||
title: `${t("Rename")}…`,
|
||||
visible: !!can.update && !user.separateEditMode && !!onRename,
|
||||
onClick: () => onRename?.(),
|
||||
icon: <InputIcon />,
|
||||
},
|
||||
actionToMenuItem(shareDocument, context),
|
||||
actionToMenuItem(createNestedDocument, context),
|
||||
actionToMenuItem(importDocument, context),
|
||||
actionToMenuItem(createTemplateFromDocument, context),
|
||||
actionToMenuItem(duplicateDocument, context),
|
||||
actionToMenuItem(publishDocument, context),
|
||||
actionToMenuItem(unpublishDocument, context),
|
||||
actionToMenuItem(archiveDocument, context),
|
||||
actionToMenuItem(moveDocument, context),
|
||||
actionToMenuItem(moveTemplate, context),
|
||||
actionToMenuItem(pinDocument, context),
|
||||
actionToMenuItem(createDocumentFromTemplate, context),
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
actionToMenuItem(openDocumentComments, context),
|
||||
actionToMenuItem(openDocumentHistory, context),
|
||||
actionToMenuItem(openDocumentInsights, context),
|
||||
actionToMenuItem(downloadDocument, context),
|
||||
actionToMenuItem(copyDocument, context),
|
||||
actionToMenuItem(printDocument, context),
|
||||
actionToMenuItem(searchInDocument, context),
|
||||
{
|
||||
type: "separator",
|
||||
},
|
||||
actionToMenuItem(deleteDocument, context),
|
||||
actionToMenuItem(permanentlyDeleteDocument, context),
|
||||
]}
|
||||
/>
|
||||
{(showDisplayOptions || showToggleEmbeds) && can.update && (
|
||||
<>
|
||||
<Separator />
|
||||
<DisplayOptions>
|
||||
{showToggleEmbeds && (
|
||||
<Style>
|
||||
<ToggleMenuItem
|
||||
width={26}
|
||||
height={14}
|
||||
label={t("Enable embeds")}
|
||||
labelPosition="left"
|
||||
checked={!document.embedsDisabled}
|
||||
onChange={
|
||||
document.embedsDisabled
|
||||
? document.enableEmbeds
|
||||
: document.disableEmbeds
|
||||
}
|
||||
/>
|
||||
</Style>
|
||||
)}
|
||||
{showDisplayOptions && !isMobile && (
|
||||
<Style>
|
||||
<ToggleMenuItem
|
||||
width={26}
|
||||
height={14}
|
||||
label={t("Full width")}
|
||||
labelPosition="left"
|
||||
checked={document.fullWidth}
|
||||
onChange={(ev) => {
|
||||
const fullWidth = ev.currentTarget.checked;
|
||||
user.setPreference(
|
||||
UserPreference.FullWidthDocuments,
|
||||
fullWidth
|
||||
);
|
||||
void user.save();
|
||||
document.fullWidth = fullWidth;
|
||||
void document.save();
|
||||
}}
|
||||
/>
|
||||
</Style>
|
||||
)}
|
||||
</DisplayOptions>
|
||||
</>
|
||||
)}
|
||||
</ContextMenu>
|
||||
<MenuContext.Provider value={{ model: document, menuState }}>
|
||||
<MenuTrigger label={label} onTrigger={showMenu} />
|
||||
{isMenuVisible ? (
|
||||
<MenuContent
|
||||
onOpen={onOpen}
|
||||
onClose={onClose}
|
||||
onRename={onRename}
|
||||
showDisplayOptions={showDisplayOptions}
|
||||
showToggleEmbeds={showToggleEmbeds}
|
||||
/>
|
||||
) : null}
|
||||
</MenuContext.Provider>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import * as React from "react";
|
||||
import { MenuStateReturn } from "reakit";
|
||||
import Model from "~/models/base/Model";
|
||||
|
||||
export type MenuContext<T extends Model> = {
|
||||
/** Model for which the menu is to be designed. */
|
||||
model: T;
|
||||
/** Menu state */
|
||||
menuState: MenuStateReturn;
|
||||
};
|
||||
|
||||
export const MenuContext = React.createContext<MenuContext<Model>>(
|
||||
{} as MenuContext<Model>
|
||||
);
|
||||
|
||||
export const useMenuContext = <T extends Model>() =>
|
||||
React.useContext<MenuContext<T>>(
|
||||
MenuContext as unknown as React.Context<MenuContext<T>>
|
||||
);
|
||||
@@ -39,7 +39,9 @@ export default abstract class Model {
|
||||
*
|
||||
* @returns A promise that resolves when loading is complete.
|
||||
*/
|
||||
async loadRelations(): Promise<any> {
|
||||
async loadRelations(
|
||||
options: { withoutPolicies?: boolean } = {}
|
||||
): Promise<any> {
|
||||
const relations = getRelationsForModelClass(
|
||||
this.constructor as typeof Model
|
||||
);
|
||||
@@ -66,7 +68,7 @@ export default abstract class Model {
|
||||
}
|
||||
|
||||
const policy = this.store.rootStore.policies.get(this.id);
|
||||
if (!policy) {
|
||||
if (!policy && !options.withoutPolicies) {
|
||||
promises.push(this.store.fetch(this.id, { force: true }));
|
||||
}
|
||||
|
||||
|
||||
@@ -480,6 +480,7 @@
|
||||
"Alphabetical sort": "Alphabetical sort",
|
||||
"Manual sort": "Manual sort",
|
||||
"Comment options": "Comment options",
|
||||
"Show document menu": "Show document menu",
|
||||
"{{ documentName }} restored": "{{ documentName }} restored",
|
||||
"Document options": "Document options",
|
||||
"Restore": "Restore",
|
||||
|
||||
Reference in new issue
Block a user