mirror of
https://github.com/outline/outline.git
synced 2026-11-30 17:10:36 +03:00
* fix: centralize sidebar expansion state to eliminate O(N²) tree traversals Each DocumentLink previously traversed the full collection tree independently to determine whether to auto-expand (pathToDocument / descendants), which is O(N) per row and quadratic overall. With thousands of documents this makes the sidebar unusable. Replaces per-node expansion state with a single MobX-backed SidebarExpansionState per tree root. The ObservableSet ensures only the toggled node re-renders. Alt-click cascade, auto-expand on navigation, and drag-to-reparent expansion all go through the same centralized state instead of the per-node SidebarDisclosureContext relay. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: move SidebarExpansionContext alongside other sidebar contexts Rename hooks/useSidebarExpansion.ts to components/SidebarExpansionContext.ts to match the convention of SidebarContext.ts and SidebarDisclosureContext.ts. The context is now the default export with hooks as named exports. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: scope sidebar expansion to its own tree and restore alt-click cascade `useSidebarExpansionState` was unconditionally adding the active document id to every per-tree expansion set, which made `SharedWithMeLink` auto- expand whenever the user navigated anywhere in the matching sidebar context. `computeAncestorPath` now includes the target when found and returns empty when absent, so the hook only expands ids that actually belong to its tree. Also restores alt-click cascade for `StarredDocumentLink` and `SharedWithMeLink`: the parents still broadcast disclosure events but `DocumentLink` no longer listens, so nested children weren't expanded. `StarredDocumentLink` now subscribes via `useSidebarDisclosure` (mirroring `CollectionLinkChildren`), and `SharedWithMeLink` calls `expansion.expandAll`/`collapseAll` directly on alt-click. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: collapse expanded nodes when children are removed and deduplicate shared expansion provider Restores the effect that collapses a node in the expansion state when it no longer has children, preventing the reorder drop logic from treating leaf nodes as expanded containers. Also removes the redundant SidebarExpansionContext.Provider from SharedCollectionLink since the parent SharedSidebar already provides one. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
165 lines
4.7 KiB
TypeScript
165 lines
4.7 KiB
TypeScript
import { useKBar } from "kbar";
|
|
import { observer } from "mobx-react";
|
|
import { SearchIcon } from "outline-icons";
|
|
import { useCallback, useEffect, useMemo } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import styled from "styled-components";
|
|
import { s } from "@shared/styles";
|
|
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
|
import { metaDisplay, shortcutSeparator } from "@shared/utils/keyboard";
|
|
import type Share from "~/models/Share";
|
|
import Flex from "~/components/Flex";
|
|
import Scrollable from "~/components/Scrollable";
|
|
import useCurrentUser from "~/hooks/useCurrentUser";
|
|
import useShareBranding from "~/hooks/useShareBranding";
|
|
import useStores from "~/hooks/useStores";
|
|
import history from "~/utils/history";
|
|
import { homePath, sharedModelPath } from "~/utils/routeHelpers";
|
|
import { AvatarSize } from "../Avatar";
|
|
import TeamLogo from "../TeamLogo";
|
|
import Sidebar from "./Sidebar";
|
|
import SidebarExpansionContext, {
|
|
useSidebarExpansionState,
|
|
} from "./components/SidebarExpansionContext";
|
|
import Section from "./components/Section";
|
|
import { SharedCollectionLink } from "./components/SharedCollectionLink";
|
|
import { SharedDocumentLink } from "./components/SharedDocumentLink";
|
|
import SidebarButton from "./components/SidebarButton";
|
|
|
|
type Props = {
|
|
share: Share;
|
|
};
|
|
|
|
function SharedSidebar({ share }: Props) {
|
|
const user = useCurrentUser({ rejectOnEmpty: false });
|
|
const { ui, documents, collections } = useStores();
|
|
const { t } = useTranslation();
|
|
const { query } = useKBar();
|
|
|
|
const { displayName, displayLogoUrl, displayLogoModel, brandingAvailable } =
|
|
useShareBranding(share);
|
|
const rootNode = share.tree;
|
|
const shareId = share.urlId || share.id;
|
|
const collection = collections.get(rootNode?.id);
|
|
const hideRootNode = collection
|
|
? ProsemirrorHelper.isEmptyData(collection?.data)
|
|
: false;
|
|
|
|
const handleOpenSearch = useCallback(() => {
|
|
query.toggle();
|
|
}, [query]);
|
|
|
|
const rootChildren = useMemo(
|
|
() => (rootNode ? [rootNode] : undefined),
|
|
[rootNode]
|
|
);
|
|
const expansion = useSidebarExpansionState(rootChildren, ui.activeDocumentId);
|
|
|
|
useEffect(() => {
|
|
ui.tocVisible = share.showTOC;
|
|
}, []);
|
|
|
|
if (!rootNode?.children.length) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<Sidebar canCollapse={false}>
|
|
{brandingAvailable && (
|
|
<SidebarButton
|
|
title={displayName}
|
|
image={
|
|
<TeamLogo
|
|
model={displayLogoModel}
|
|
src={displayLogoUrl ?? undefined}
|
|
size={AvatarSize.XLarge}
|
|
alt={t("Logo")}
|
|
/>
|
|
}
|
|
disabled={hideRootNode}
|
|
onClick={
|
|
hideRootNode
|
|
? undefined
|
|
: () => history.push(user ? homePath() : sharedModelPath(shareId))
|
|
}
|
|
/>
|
|
)}
|
|
<ScrollContainer topShadow flex>
|
|
<TopSection>
|
|
<SearchButton onClick={handleOpenSearch}>
|
|
<SearchIcon size={20} />
|
|
<SearchLabel>{t("Search")}</SearchLabel>
|
|
<Shortcut>
|
|
{metaDisplay}
|
|
{shortcutSeparator}K
|
|
</Shortcut>
|
|
</SearchButton>
|
|
</TopSection>
|
|
<Section as="nav" aria-label={t("Documents")}>
|
|
<SidebarExpansionContext.Provider value={expansion}>
|
|
{share.collectionId ? (
|
|
<SharedCollectionLink
|
|
node={rootNode}
|
|
shareId={shareId}
|
|
hideRootNode={hideRootNode}
|
|
/>
|
|
) : (
|
|
<SharedDocumentLink
|
|
index={0}
|
|
// If the root node has an icon we need some extra space for it
|
|
depth={rootNode.icon ? 1 : 0}
|
|
shareId={shareId}
|
|
node={rootNode}
|
|
prefetchDocument={documents.prefetchDocument}
|
|
activeDocumentId={ui.activeDocumentId}
|
|
activeDocument={documents.active}
|
|
/>
|
|
)}
|
|
</SidebarExpansionContext.Provider>
|
|
</Section>
|
|
</ScrollContainer>
|
|
</Sidebar>
|
|
);
|
|
}
|
|
|
|
const ScrollContainer = styled(Scrollable)`
|
|
padding-bottom: 16px;
|
|
`;
|
|
|
|
const TopSection = styled(Flex)`
|
|
padding: 8px;
|
|
flex-shrink: 0;
|
|
`;
|
|
|
|
const SearchButton = styled.button`
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
width: 100%;
|
|
padding: 6px 12px;
|
|
margin: 8px 0;
|
|
border: 1px solid ${s("inputBorder")};
|
|
border-radius: 16px;
|
|
background: ${s("background")};
|
|
color: ${s("textTertiary")};
|
|
cursor: var(--pointer);
|
|
font-size: 14px;
|
|
|
|
&:hover {
|
|
border-color: ${s("inputBorderFocused")};
|
|
color: ${s("textSecondary")};
|
|
}
|
|
`;
|
|
|
|
const SearchLabel = styled.span`
|
|
flex-grow: 1;
|
|
text-align: start;
|
|
`;
|
|
|
|
const Shortcut = styled.span`
|
|
flex-shrink: 0;
|
|
font-size: 13px;
|
|
`;
|
|
|
|
export default observer(SharedSidebar);
|