Files
outline/app/components/Sidebar/components/Collections.tsx
T
Tom Moor ef6e51401b perf: Reduce main-thread cost of opening a document from the sidebar (#14033)
* perf: Read active models lazily in ActionContextProvider

The root provider read the active document and models during render, so
every navigation rebuilt the context and re-rendered all consumers. These
values are now getters, and overrides are merged without evaluating them.

* perf: Skip urlId scan in Store.get for UUIDs

When an id was not loaded, get() searched every item by urlId, so each
sidebar row observed the whole store. A UUID cannot end with a urlId, so
the search is skipped for UUIDs.

* perf: Stop sidebar rows re-rendering on navigation

- Track expanded nodes in an observable map, as ObservableSet.has tracks
  the whole set.
- Do not rebuild the document tree rows when the active document changes.
- Auto-expand the active path in a reaction instead of reading the active
  document during render.
- Compute active state per row, and keep row props stable.

* perf: Add useLocationSelector for persistent components

useLocation re-renders on every navigation. The new hook re-renders only
when the selected value changes, and is used where components stay mounted
across navigation. AuthenticatedLayout now keeps its portal element in
state, as it no longer re-renders after mount.

* chore: Send Document-Policy js-profiling header in development

* docs: Add render performance guidance to AGENTS.md

* fix: Check document or collection star exclusively for active state

* fix: Read location from each action context provider's own history

Nested providers inherited the root location getter, so menus inside a
split view pane read the browser route instead of the pane's route.

* fix: Expand starred collection when the active document changes
2026-10-11 15:17:01 -04:00

127 lines
4.1 KiB
TypeScript

import fractionalIndex from "fractional-index";
import { observer } from "mobx-react";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import type Collection from "~/models/Collection";
import Flex from "~/components/Flex";
import Error from "~/components/List/Error";
import PaginatedList from "~/components/PaginatedList";
import { createCollection } from "~/actions/definitions/collections";
import { useActiveSidebarContext } from "~/hooks/useActiveSidebarContext";
import useStores from "~/hooks/useStores";
import { type DragObject, useDropRef } from "../hooks/useDragAndDrop";
import DraggableCollectionLink from "./DraggableCollectionLink";
import DropCursor from "./DropCursor";
import Header from "./Header";
import PlaceholderCollections from "./PlaceholderCollections";
import Relative from "./Relative";
import SidebarAction from "./SidebarAction";
import SidebarContext from "./SidebarContext";
import SidebarLink from "./SidebarLink";
import Text from "@shared/components/Text";
import usePolicy from "~/hooks/usePolicy";
const headerActions = [createCollection];
function Collections() {
const { auth, collections, policies, ui } = useStores();
const activeSidebarContext = useActiveSidebarContext() ?? "collections";
const { t } = useTranslation();
const can = usePolicy(auth.team?.id);
const orderedCollections = collections.allActive;
const params = useMemo(
() => ({
limit: 100,
}),
[]
);
const [
{ isCollectionDropping, isDraggingAnyCollection },
dropToReorderCollection,
] = useDropRef({
accept: "collection",
drop: async (item: DragObject) => {
void collections.move(
item.id,
fractionalIndex(null, orderedCollections[0].index)
);
},
canDrop: (item) =>
item.id !== orderedCollections[0]?.id &&
!!policies.abilities(item.id).move,
collect: (monitor) => ({
isCollectionDropping: monitor.isOver(),
isDraggingAnyCollection: monitor.canDrop(),
}),
});
return (
<SidebarContext.Provider value="collections">
<Flex column>
<Header
id="collections"
title={t("Collections")}
actions={headerActions}
primaryAction={createCollection}
>
<Relative>
<PaginatedList<Collection>
options={params}
aria-label={t("Collections")}
items={orderedCollections}
activeItemId={
activeSidebarContext === "collections"
? ui.activeCollectionId
: undefined
}
loading={<PlaceholderCollections />}
heading={
isDraggingAnyCollection ? (
<DropCursor
isActiveDrop={isCollectionDropping}
innerRef={dropToReorderCollection}
position="top"
/>
) : undefined
}
empty={
// No need for empty state if we're displaying the createCollection action
can.createCollection ? null : (
<SidebarLink
label={
<Text type="tertiary" size="small" italic>
{t("No collections")}
</Text>
}
onClick={() => {}}
depth={1.5}
/>
)
}
renderError={(props) => <StyledError {...props} />}
renderItem={(item, index) => (
<DraggableCollectionLink
key={item.id}
collection={item}
belowCollection={orderedCollections[index + 1]}
/>
)}
/>
<SidebarAction action={createCollection} depth={0} />
</Relative>
</Header>
</Flex>
</SidebarContext.Provider>
);
}
export const StyledError = styled(Error)`
font-size: 15px;
padding: 0 8px;
`;
export default observer(Collections);