Files
outline/app/components/AuthenticatedLayout.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

131 lines
4.1 KiB
TypeScript

import { observer } from "mobx-react";
import * as React from "react";
import { DndProvider } from "react-dnd";
import { EditorAwareHTML5Backend } from "~/components/EditorAwareHTML5Backend";
import ErrorSuspended from "~/scenes/Errors/ErrorSuspended";
import Layout from "~/components/Layout";
import RegisterKeyDown from "~/components/RegisterKeyDown";
import { RightSidebarProvider } from "~/components/RightSidebarContext";
import Sidebar from "~/components/Sidebar";
import { rootWebMCPActions } from "~/actions/definitions/webmcp";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import useKeyDown from "~/hooks/useKeyDown";
import useWebMCPActions from "~/hooks/useWebMCPActions";
import { usePostLoginPath } from "~/hooks/useLastVisitedPath";
import usePolicy from "~/hooks/usePolicy";
import { useLocationSelector } from "~/hooks/useLocationSelector";
import useStores from "~/hooks/useStores";
import Logger from "~/utils/Logger";
import history from "~/utils/history";
import lazyWithRetry from "@shared/utils/lazyWithRetry";
import {
searchPath,
newDocumentPath,
settingsPath,
homePath,
} from "~/utils/routeHelpers";
import { DocumentContextProvider } from "./DocumentContext";
import Fade from "./Fade";
import NotificationBadge from "./NotificationBadge";
import { PortalContext } from "./Portal";
import CommandBar from "./CommandBar";
const SettingsSidebar = lazyWithRetry(
() => import("~/components/Sidebar/Settings")
);
type Props = {
children?: React.ReactNode;
};
const AuthenticatedLayout: React.FC<Props> = ({ children }: Props) => {
const { ui, auth } = useStores();
const isSettings = useLocationSelector((location) =>
location.pathname.startsWith(settingsPath())
);
// State rather than a ref, so that portals receive the element once mounted.
const [layoutElement, setLayoutElement] =
React.useState<HTMLDivElement | null>(null);
const canCollection = usePolicy(ui.activeCollectionId);
const team = useCurrentTeam();
const [spendPostLoginPath] = usePostLoginPath();
useKeyDown(".", () => ui.toggleCollapsedSidebar(), { metaKey: true });
useWebMCPActions(rootWebMCPActions);
const goToSearch = (ev: KeyboardEvent) => {
if (!ev.metaKey && !ev.ctrlKey) {
ev.preventDefault();
ev.stopPropagation();
history.push(searchPath());
}
};
const goToNewDocument = (event: KeyboardEvent) => {
if (event.metaKey || event.altKey) {
return;
}
const { activeCollectionId } = ui;
if (!activeCollectionId || !canCollection.createDocument) {
return;
}
history.push(newDocumentPath(activeCollectionId));
};
React.useEffect(() => {
const postLoginPath = spendPostLoginPath();
if (postLoginPath) {
try {
history.replace(postLoginPath);
} catch (err) {
Logger.warn("Failed to navigate to post login path, falling back", {
path: postLoginPath,
error: err,
});
history.replace(homePath());
}
}
}, [spendPostLoginPath]);
if (auth.isSuspended) {
return <ErrorSuspended />;
}
const sidebar = (
<Fade>
<React.Suspense fallback={null}>
{isSettings && <SettingsSidebar />}
</React.Suspense>
<div style={isSettings ? { display: "none" } : undefined}>
<Sidebar />
</div>
</Fade>
);
return (
<DocumentContextProvider>
<RightSidebarProvider>
<PortalContext.Provider value={layoutElement}>
<DndProvider backend={EditorAwareHTML5Backend}>
<Layout
title={team.name}
sidebar={sidebar}
sidebarCanCollapse={!isSettings}
ref={setLayoutElement}
>
<RegisterKeyDown trigger="n" handler={goToNewDocument} />
<RegisterKeyDown trigger="t" handler={goToSearch} />
<RegisterKeyDown trigger="/" handler={goToSearch} />
{children}
<CommandBar />
<NotificationBadge />
</Layout>
</DndProvider>
</PortalContext.Provider>
</RightSidebarProvider>
</DocumentContextProvider>
);
};
export default observer(AuthenticatedLayout);