From 02bdb2e464082338eefd1e1acc0c1db7cec03ff4 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 24 Oct 2025 02:23:38 +0200 Subject: [PATCH] fix: Render-per-model type, 4x improvement on perf (#10465) * fix: Render-per-model type, 4x improvement on perf * fix: Sidebar CollectionLinkChildren render when @mention changes --- .../Sidebar/hooks/useCollectionDocuments.ts | 31 ++++++++------- app/editor/components/BlockMenu.tsx | 24 +++++++----- app/editor/components/EmojiMenu.tsx | 23 ++++++----- app/editor/components/MentionMenu.tsx | 39 ++++++++++++------- app/editor/components/PasteMenu.tsx | 23 ++++++----- app/editor/components/SuggestionsMenu.tsx | 6 ++- app/editor/components/SuggestionsMenuItem.tsx | 2 +- app/models/Collection.ts | 3 +- app/models/base/Model.ts | 5 +++ 9 files changed, 99 insertions(+), 57 deletions(-) diff --git a/app/components/Sidebar/hooks/useCollectionDocuments.ts b/app/components/Sidebar/hooks/useCollectionDocuments.ts index b3414c1a8b..a519472864 100644 --- a/app/components/Sidebar/hooks/useCollectionDocuments.ts +++ b/app/components/Sidebar/hooks/useCollectionDocuments.ts @@ -7,18 +7,28 @@ export default function useCollectionDocuments( collection: Collection | undefined, activeDocument: Document | undefined ) { + const insertDraftDocument = useMemo( + () => + activeDocument && + activeDocument.isActive && + activeDocument.isDraft && + activeDocument.collectionId === collection?.id && + !activeDocument.parentDocumentId, + [ + activeDocument?.isActive, + activeDocument?.isDraft, + activeDocument?.collectionId, + activeDocument?.parentDocumentId, + collection?.id, + ] + ); + return useMemo(() => { if (!collection?.sortedDocuments) { return undefined; } - const insertDraftDocument = - activeDocument?.isActive && - activeDocument?.isDraft && - activeDocument?.collectionId === collection.id && - !activeDocument?.parentDocumentId; - - return insertDraftDocument + return insertDraftDocument && activeDocument ? sortNavigationNodes( [activeDocument.asNavigationNode, ...collection.sortedDocuments], collection.sort, @@ -26,14 +36,9 @@ export default function useCollectionDocuments( ) : collection.sortedDocuments; }, [ - activeDocument?.isActive, - activeDocument?.isDraft, - activeDocument?.collectionId, - activeDocument?.parentDocumentId, + insertDraftDocument, activeDocument?.asNavigationNode, - collection, collection?.sortedDocuments, - collection?.id, collection?.sort, ]); } diff --git a/app/editor/components/BlockMenu.tsx b/app/editor/components/BlockMenu.tsx index 98531513c3..43207540da 100644 --- a/app/editor/components/BlockMenu.tsx +++ b/app/editor/components/BlockMenu.tsx @@ -1,3 +1,4 @@ +import { useCallback } from "react"; import useDictionary from "~/hooks/useDictionary"; import getMenuItems from "../menus/block"; import { useEditor } from "./EditorContext"; @@ -13,20 +14,25 @@ function BlockMenu(props: Props) { const dictionary = useDictionary(); const { elementRef } = useEditor(); + const renderMenuItem = useCallback( + (item, _index, options) => ( + + ), + [] + ); + return ( ( - - )} + renderMenuItem={renderMenuItem} items={getMenuItems(dictionary, elementRef)} /> ); diff --git a/app/editor/components/EmojiMenu.tsx b/app/editor/components/EmojiMenu.tsx index a5d97d0452..9551491c03 100644 --- a/app/editor/components/EmojiMenu.tsx +++ b/app/editor/components/EmojiMenu.tsx @@ -1,5 +1,5 @@ import capitalize from "lodash/capitalize"; -import { useMemo } from "react"; +import { useCallback, useMemo } from "react"; import { emojiMartToGemoji, snakeCase } from "@shared/editor/lib/emoji"; import { search as emojiSearch } from "@shared/utils/emoji"; import EmojiMenuItem from "./EmojiMenuItem"; @@ -45,18 +45,23 @@ const EmojiMenu = (props: Props) => { [search] ); + const renderMenuItem = useCallback( + (item, _index, options) => ( + + ), + [] + ); + return ( ( - - )} + renderMenuItem={renderMenuItem} items={items} /> ); diff --git a/app/editor/components/MentionMenu.tsx b/app/editor/components/MentionMenu.tsx index 9829502df8..b7505a6b08 100644 --- a/app/editor/components/MentionMenu.tsx +++ b/app/editor/components/MentionMenu.tsx @@ -25,6 +25,7 @@ import SuggestionsMenu, { Props as SuggestionsMenuProps, } from "./SuggestionsMenu"; import SuggestionsMenuItem from "./SuggestionsMenuItem"; +import { runInAction } from "mobx"; interface MentionItem extends MenuItem { attrs: { @@ -53,12 +54,17 @@ function MentionMenu({ search, isActive, ...rest }: Props) { const { loading, request } = useRequest( useCallback(async () => { - const res = await client.post("/suggestions.mention", { query: search }); + const res = await client.post("/suggestions.mention", { + query: search, + limit: maxResultsInSection, + }); - res.data.documents.map(documents.add); - res.data.users.map(users.add); - res.data.collections.map(collections.add); - res.data.groups.map(groups.add); + runInAction(() => { + res.data.documents.map(documents.add); + res.data.users.map(users.add); + res.data.collections.map(collections.add); + res.data.groups.map(groups.add); + }); }, [search, documents, users, collections]) ); @@ -274,6 +280,19 @@ function MentionMenu({ search, isActive, ...rest }: Props) { [t, users, documentId, groups] ); + const renderMenuItem = useCallback( + (item, _index, options) => ( + + ), + [] + ); + // Prevent showing the menu until we have data otherwise it will be positioned // incorrectly due to the height being unknown. if (!loaded) { @@ -287,15 +306,7 @@ function MentionMenu({ search, isActive, ...rest }: Props) { filterable={false} search={search} onSelect={handleSelect} - renderMenuItem={(item, _index, options) => ( - - )} + renderMenuItem={renderMenuItem} items={items} /> ); diff --git a/app/editor/components/PasteMenu.tsx b/app/editor/components/PasteMenu.tsx index fd1a9eeda6..3ab7544f40 100644 --- a/app/editor/components/PasteMenu.tsx +++ b/app/editor/components/PasteMenu.tsx @@ -1,6 +1,6 @@ import { observer } from "mobx-react"; import { EmailIcon, LinkIcon } from "outline-icons"; -import React from "react"; +import React, { useCallback } from "react"; import { useTranslation } from "react-i18next"; import { EmbedDescriptor } from "@shared/editor/embeds"; import { MenuItem } from "@shared/editor/types"; @@ -26,6 +26,18 @@ type Props = Omit< export const PasteMenu = observer(({ pastedText, embeds, ...props }: Props) => { const items = useItems({ pastedText, embeds }); + const renderMenuItem = useCallback( + (item, _index, options) => ( + + ), + [] + ); + if (!items) { props.onClose(); return null; @@ -36,14 +48,7 @@ export const PasteMenu = observer(({ pastedText, embeds, ...props }: Props) => { {...props} trigger="" filterable={false} - renderMenuItem={(item, _index, options) => ( - - )} + renderMenuItem={renderMenuItem} items={items} /> ); diff --git a/app/editor/components/SuggestionsMenu.tsx b/app/editor/components/SuggestionsMenu.tsx index 700c5abf70..98025fd66e 100644 --- a/app/editor/components/SuggestionsMenu.tsx +++ b/app/editor/components/SuggestionsMenu.tsx @@ -641,6 +641,10 @@ function SuggestionsMenu(props: Props) { } }; + const handleOnClick = () => { + handleClickItem(item); + }; + const currentHeading = "section" in item ? item.section?.({ t }) : undefined; @@ -657,7 +661,7 @@ function SuggestionsMenu(props: Props) { > {props.renderMenuItem(item as any, index, { selected: index === selectedIndex, - onClick: () => handleClickItem(item), + onClick: handleOnClick, })} diff --git a/app/editor/components/SuggestionsMenuItem.tsx b/app/editor/components/SuggestionsMenuItem.tsx index 23ebff4417..0ab0832d1d 100644 --- a/app/editor/components/SuggestionsMenuItem.tsx +++ b/app/editor/components/SuggestionsMenuItem.tsx @@ -92,4 +92,4 @@ const Shortcut = styled.span<{ $active?: boolean }>` text-align: right; `; -export default SuggestionsMenuItem; +export default React.memo(SuggestionsMenuItem); diff --git a/app/models/Collection.ts b/app/models/Collection.ts index 8cf5099e34..a4c062301c 100644 --- a/app/models/Collection.ts +++ b/app/models/Collection.ts @@ -16,6 +16,7 @@ import { client } from "~/utils/ApiClient"; import User from "./User"; import Field from "./decorators/Field"; import { AfterChange } from "./decorators/Lifecycle"; +import isEqual from "lodash/isEqual"; export default class Collection extends ParanoidModel { static modelName = "Collection"; @@ -156,7 +157,7 @@ export default class Collection extends ParanoidModel { return this.sort.field === "index"; } - @computed + @computed({ equals: isEqual }) get sortedDocuments(): NavigationNode[] | undefined { if (!this.documents) { return undefined; diff --git a/app/models/base/Model.ts b/app/models/base/Model.ts index a88939463e..0872cc361c 100644 --- a/app/models/base/Model.ts +++ b/app/models/base/Model.ts @@ -6,6 +6,7 @@ import Logger from "~/utils/Logger"; import { getFieldsForModel } from "../decorators/Field"; import { LifecycleManager } from "../decorators/Lifecycle"; import { getRelationsForModelClass } from "../decorators/Relation"; +import { isEqual } from "lodash"; export default abstract class Model { static modelName: string; @@ -147,6 +148,10 @@ export default abstract class Model { continue; } // @ts-expect-error TODO + if (isEqual(this[key], data[key])) { + continue; + } + // @ts-expect-error TODO this[key] = data[key]; } catch (error) { Logger.warn(`Error setting ${key} on model`, error);