mirror of
https://github.com/outline/outline.git
synced 2026-11-27 07:58:51 +03:00
* feat: Make model.save() optimistic with rollback on failure Apply saved params to the model immediately so MobX observers update without waiting for the server, and roll the model back to its previous state when the request fails. New models with a client-generated id are optimistically added to the store and removed again on failure. * chore: Apply formatter and test naming convention * fix: Scope optimistic saves to passed fields and confirmed values Only fields passed to save() are applied optimistically, and a failure reverts a field only while it still holds the optimistic value. Change hooks compare against the last confirmed values so they run when the server responds. The optimistic store add is removed. * refactor: Use optimistic save for preferences and field toggles Add savePreference to User, Team and Document, and pass changed fields to save() so they revert on failure. * fix: Only compare incoming fields against confirmed values in change hooks --------- Co-authored-by: Claude <noreply@anthropic.com>
279 lines
7.7 KiB
TypeScript
279 lines
7.7 KiB
TypeScript
import { noop } from "es-toolkit/compat";
|
|
import { observer } from "mobx-react";
|
|
import * as React from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import styled from "styled-components";
|
|
import { breakpoint } from "@shared/styles/breakpoint";
|
|
import { s } from "@shared/styles";
|
|
import { SubscriptionType, UserPreference } from "@shared/types";
|
|
import type Document from "~/models/Document";
|
|
import type Template from "~/models/Template";
|
|
import { DropdownMenu } from "~/components/Menu/DropdownMenu";
|
|
import { OverflowMenuButton } from "~/components/Menu/OverflowMenuButton";
|
|
import { toMenuItems, toMobileMenuItems } from "~/components/Menu/transformer";
|
|
import Switch from "~/components/Switch";
|
|
import { actionToMenuItem } from "~/actions";
|
|
import { changeHeadingPrefix } from "~/actions/definitions/documents";
|
|
import useActionContext, {
|
|
ActionContextProvider,
|
|
} from "~/hooks/useActionContext";
|
|
import useCurrentUser from "~/hooks/useCurrentUser";
|
|
import { useDocumentActiveModels } from "~/hooks/useDocumentActiveModels";
|
|
import useMobile from "~/hooks/useMobile";
|
|
import usePolicy from "~/hooks/usePolicy";
|
|
import useRequest from "~/hooks/useRequest";
|
|
import useStores from "~/hooks/useStores";
|
|
import { MenuSeparator } from "~/components/primitives/components/Menu";
|
|
import { useDocumentMenuAction } from "~/hooks/useDocumentMenuAction";
|
|
|
|
type Props = {
|
|
/** Document for which the menu is to be shown */
|
|
document: Document;
|
|
/** Alignment w.r.t trigger - defaults to start */
|
|
align?: "start" | "end";
|
|
/** Trigger's variant - renders nude variant if unset */
|
|
neutral?: boolean;
|
|
/** Pass true if the document is currently being displayed */
|
|
showDisplayOptions?: boolean;
|
|
/** Whether to include the option of toggling embeds as menu item */
|
|
showToggleEmbeds?: boolean;
|
|
/** Invoked when the "Find and replace" menu item is clicked */
|
|
onFindAndReplace?: () => void;
|
|
/** Callback when a template is selected to apply its content to the document */
|
|
onSelectTemplate?: (template: Template) => 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,
|
|
align,
|
|
neutral,
|
|
showToggleEmbeds,
|
|
showDisplayOptions,
|
|
onSelectTemplate,
|
|
onRename,
|
|
onOpen,
|
|
onClose,
|
|
onFindAndReplace,
|
|
}: Props) {
|
|
const { t } = useTranslation();
|
|
const user = useCurrentUser();
|
|
const isMobile = useMobile();
|
|
const can = usePolicy(document);
|
|
|
|
const { subscriptions, pins } = useStores();
|
|
const activeModels = useDocumentActiveModels(document);
|
|
|
|
const {
|
|
loading: auxDataLoading,
|
|
loaded: auxDataLoaded,
|
|
request: auxDataRequest,
|
|
} = useRequest(() =>
|
|
Promise.all([
|
|
subscriptions.fetchOne({
|
|
documentId: document.id,
|
|
event: SubscriptionType.Document,
|
|
}),
|
|
document.collectionId
|
|
? subscriptions.fetchOne({
|
|
collectionId: document.collectionId,
|
|
event: SubscriptionType.Document,
|
|
})
|
|
: noop,
|
|
pins.fetchOne({
|
|
documentId: document.id,
|
|
collectionId: document.collectionId ?? null,
|
|
}),
|
|
])
|
|
);
|
|
|
|
const handlePointerEnter = React.useCallback(() => {
|
|
if (!auxDataLoading && !auxDataLoaded) {
|
|
void auxDataRequest();
|
|
void document.loadRelations();
|
|
}
|
|
}, [auxDataLoading, auxDataLoaded, auxDataRequest, document]);
|
|
|
|
const handleEmbedsToggle = React.useCallback(
|
|
(checked: boolean) => {
|
|
if (checked) {
|
|
document.enableEmbeds();
|
|
} else {
|
|
document.disableEmbeds();
|
|
}
|
|
},
|
|
[document]
|
|
);
|
|
|
|
const handleFullWidthToggle = React.useCallback(
|
|
(checked: boolean) => {
|
|
void user.savePreference(UserPreference.FullWidthDocuments, checked);
|
|
void document.save({ fullWidth: checked });
|
|
},
|
|
[user, document]
|
|
);
|
|
|
|
const handleInsightsToggle = React.useCallback(
|
|
(checked: boolean) => {
|
|
void document.save({ insightsEnabled: checked });
|
|
},
|
|
[document]
|
|
);
|
|
|
|
const rootAction = useDocumentMenuAction({
|
|
documentId: document.id,
|
|
isViewing: showDisplayOptions,
|
|
onFindAndReplace,
|
|
onRename,
|
|
onSelectTemplate,
|
|
});
|
|
|
|
const toggleSwitches = React.useMemo<React.ReactNode>(() => {
|
|
if (!can.update || !(showDisplayOptions || showToggleEmbeds)) {
|
|
return;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<MenuSeparator />
|
|
{showDisplayOptions && <HeadingPrefixMenuItem />}
|
|
<DisplayOptions>
|
|
{can.updateInsights && (
|
|
<Style>
|
|
<ToggleMenuItem
|
|
width={26}
|
|
height={14}
|
|
label={t("Enable viewer insights")}
|
|
labelPosition="left"
|
|
checked={document.insightsEnabled}
|
|
onChange={handleInsightsToggle}
|
|
/>
|
|
</Style>
|
|
)}
|
|
{showToggleEmbeds && (
|
|
<Style>
|
|
<ToggleMenuItem
|
|
width={26}
|
|
height={14}
|
|
label={t("Enable embeds")}
|
|
labelPosition="left"
|
|
checked={!document.embedsDisabled}
|
|
onChange={handleEmbedsToggle}
|
|
/>
|
|
</Style>
|
|
)}
|
|
{showDisplayOptions && !isMobile && (
|
|
<Style>
|
|
<ToggleMenuItem
|
|
width={26}
|
|
height={14}
|
|
label={t("Full width")}
|
|
labelPosition="left"
|
|
checked={document.fullWidth}
|
|
onChange={handleFullWidthToggle}
|
|
/>
|
|
</Style>
|
|
)}
|
|
</DisplayOptions>
|
|
</>
|
|
);
|
|
}, [
|
|
t,
|
|
can.update,
|
|
can.updateInsights,
|
|
document.embedsDisabled,
|
|
document.fullWidth,
|
|
document.insightsEnabled,
|
|
isMobile,
|
|
showDisplayOptions,
|
|
showToggleEmbeds,
|
|
handleEmbedsToggle,
|
|
handleFullWidthToggle,
|
|
handleInsightsToggle,
|
|
]);
|
|
|
|
return (
|
|
<ActionContextProvider value={{ activeModels }}>
|
|
<DropdownMenu
|
|
action={rootAction}
|
|
align={align}
|
|
onOpen={onOpen}
|
|
onClose={onClose}
|
|
ariaLabel={t("Document options")}
|
|
append={toggleSwitches}
|
|
>
|
|
<OverflowMenuButton
|
|
neutral={neutral}
|
|
onPointerEnter={handlePointerEnter}
|
|
/>
|
|
</DropdownMenu>
|
|
</ActionContextProvider>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Renders the heading numbering submenu as part of the display options block.
|
|
* A separate component so the action context is read inside the provider.
|
|
*/
|
|
const HeadingPrefixMenuItem = observer(function HeadingPrefixMenuItem_() {
|
|
const context = useActionContext({ isMenu: true });
|
|
const isMobile = useMobile();
|
|
const item = actionToMenuItem(changeHeadingPrefix, context);
|
|
|
|
// On mobile the display options are appended to a drawer rather than to menu
|
|
// content, so there is no menu root for the submenu primitives to attach to.
|
|
// Flatten the submenu into an inline group of options instead, which also
|
|
// matches the toggles it sits alongside — picking one leaves the drawer open.
|
|
if (isMobile) {
|
|
if (item.type !== "submenu") {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{toMobileMenuItems(
|
|
[
|
|
{
|
|
type: "group",
|
|
title: item.title,
|
|
visible: item.visible,
|
|
items: item.items,
|
|
},
|
|
],
|
|
noop,
|
|
noop
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
return <>{toMenuItems([item])}</>;
|
|
});
|
|
|
|
const ToggleMenuItem = styled(Switch)`
|
|
* {
|
|
font-weight: normal;
|
|
color: ${s("textSecondary")};
|
|
}
|
|
`;
|
|
|
|
const DisplayOptions = styled.div`
|
|
padding: 8px 0 0;
|
|
`;
|
|
|
|
const Style = styled.div`
|
|
padding: 12px;
|
|
|
|
${breakpoint("tablet")`
|
|
padding: 4px 12px;
|
|
font-size: 14px;
|
|
`};
|
|
`;
|
|
|
|
export default observer(DocumentMenu);
|