From fc333abb86d0f4a528ec361247e7eeb8002cb990 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 31 Oct 2024 20:35:13 -0400 Subject: [PATCH] perf: Improve sidebar performance when collection has large amount of root documents --- .../components/CollectionLinkChildren.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/components/Sidebar/components/CollectionLinkChildren.tsx b/app/components/Sidebar/components/CollectionLinkChildren.tsx index e74a6bec00..b5874a3ca3 100644 --- a/app/components/Sidebar/components/CollectionLinkChildren.tsx +++ b/app/components/Sidebar/components/CollectionLinkChildren.tsx @@ -2,6 +2,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { useDrop } from "react-dnd"; import { useTranslation } from "react-i18next"; +import { Waypoint } from "react-waypoint"; import { toast } from "sonner"; import styled from "styled-components"; import Collection from "~/models/Collection"; @@ -34,11 +35,13 @@ function CollectionLinkChildren({ expanded, prefetchDocument, }: Props) { + const pageSize = 250; const can = usePolicy(collection); const manualSort = collection.sort.field === "index"; const { documents, dialogs, collections } = useStores(); const { t } = useTranslation(); const childDocuments = useCollectionDocuments(collection, documents.active); + const [showing, setShowing] = React.useState(pageSize); // Drop to reorder document const [{ isOverReorder, isDraggingAnyDocument }, dropToReorder] = useDrop({ @@ -83,6 +86,18 @@ function CollectionLinkChildren({ }), }); + React.useEffect(() => { + if (!expanded) { + setShowing(pageSize); + } + }, [expanded]); + + const showMore = React.useCallback(() => { + if (childDocuments && childDocuments.length > showing) { + setShowing((value) => value + pageSize); + } + }, [childDocuments, showing]); + return ( {isDraggingAnyDocument && can.createDocument && manualSort && ( @@ -98,7 +113,7 @@ function CollectionLinkChildren({ )} - {childDocuments?.map((node, index) => ( + {childDocuments?.slice(0, showing).map((node, index) => ( )} + );