mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
* chore: Prune redundant comments and normalize lint directives Removes comments that restate the following line and stale TODOs, drops the passive event listener feature check now that all supported browsers support it, and renames eslint directives to oxlint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * fix: Evaluate restricted team domains at validation time The restricted email provider list was captured when the model loaded, so it could not follow the hosting environment and the tests that covered it never reached the model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH * chore: Replace type suppressions with accurate types Adds a Concrete helper type for registries of abstract classes, declares missing Sequelize and refractor typings, uses public ProseMirror APIs, a typed mock context helper in middleware tests, and a StaticLink component for sidebar rows being edited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH --------- Co-authored-by: Claude <noreply@anthropic.com>
281 lines
8.5 KiB
TypeScript
281 lines
8.5 KiB
TypeScript
import type { Location, LocationDescriptor } from "history";
|
|
import { observer } from "mobx-react";
|
|
import { PlusIcon } from "outline-icons";
|
|
import * as React from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { mergeRefs } from "react-merge-refs";
|
|
import type { match } from "react-router";
|
|
import { CollectionValidation, DocumentValidation } from "@shared/validations";
|
|
import type Collection from "~/models/Collection";
|
|
import EditableTitle, { type RefHandle } from "~/components/EditableTitle";
|
|
import Fade from "~/components/Fade";
|
|
import CollectionIcon from "~/components/Icons/CollectionIcon";
|
|
import NudeButton from "~/components/NudeButton";
|
|
import Tooltip from "~/components/Tooltip";
|
|
import useBoolean from "~/hooks/useBoolean";
|
|
import { ActionContextProvider } from "~/hooks/useActionContext";
|
|
import DropToImport from "./DropToImport";
|
|
import Relative from "./Relative";
|
|
import SidebarLink from "./SidebarLink";
|
|
import { StaticLink } from "./StaticLink";
|
|
import type { SidebarContextType } from "./SidebarContext";
|
|
import { useSidebarContext } from "./SidebarContext";
|
|
import { useSidebarNavigation } from "./SidebarNavigation";
|
|
import type { ActionWithChildren } from "~/types";
|
|
|
|
export type CollectionRowProps = {
|
|
/** Collection model for the row. */
|
|
collection: Collection;
|
|
/** Indentation depth of the row. */
|
|
depth?: number;
|
|
|
|
/** Navigation target for the row. */
|
|
to: LocationDescriptor;
|
|
/** Click handler for the row. */
|
|
onClick?: () => void;
|
|
/** Called on click intent for prefetching. */
|
|
onClickIntent?: () => void;
|
|
/** Optional override for the active-match function. */
|
|
isActiveOverride?: (
|
|
match: match | null,
|
|
location: Location<{ sidebarContext?: SidebarContextType }>
|
|
) => boolean;
|
|
|
|
/** Icon displayed to the left of the label. Defaults to CollectionIcon. */
|
|
icon?: React.ReactNode;
|
|
|
|
/** Whether the row is expanded. Pass undefined to hide the disclosure. */
|
|
expanded?: boolean;
|
|
/** Called when the disclosure caret toggles expansion. */
|
|
onDisclosureClick: (ev?: React.MouseEvent<HTMLElement>) => void;
|
|
/** Imperative expand, used by the "+" button to auto-expand. */
|
|
onExpand?: () => void;
|
|
|
|
/** When true, the name renders as an EditableTitle. */
|
|
canEdit?: boolean;
|
|
/** Title displayed and edited when canEdit is true. */
|
|
labelText?: string;
|
|
/** Submit handler for the edited title. */
|
|
onTitleChange?: (value: string) => Promise<void>;
|
|
/** Forwarded ref to the EditableTitle so the container can trigger rename. */
|
|
editableTitleRef?: React.Ref<RefHandle>;
|
|
/** Notifies the container when the inline title's editing state changes. */
|
|
onEditingChange?: (editing: boolean) => void;
|
|
|
|
/** Context menu action for the row. */
|
|
contextAction?: ActionWithChildren;
|
|
/** Menu content rendered by the container; wrapped in Fade. */
|
|
menu?: React.ReactNode;
|
|
/** Whether the menu's action slot is visible (e.g. while the menu is open). */
|
|
menuOpen?: boolean;
|
|
|
|
/** When true, the "+" new-child button is rendered in the menu slot. */
|
|
canCreateChild?: boolean;
|
|
/** Submit handler for the inline new-child title input. */
|
|
onCreateChild?: (title: string) => Promise<void>;
|
|
/** Depth of the inline new-child SidebarLink. Defaults to one level below the row. */
|
|
newChildDepth?: number;
|
|
|
|
/** Ref forwarded to the outer Relative; for drag hover timers and keyboard navigation. */
|
|
parentRef: React.RefObject<HTMLDivElement | null>;
|
|
/** Drop target connector for "change collection" / reorder. */
|
|
dropRef?: React.Ref<HTMLDivElement>;
|
|
/** Whether the row is an active drop target (visual highlight). */
|
|
isActiveDropTarget?: boolean;
|
|
|
|
/**
|
|
* Renders the content after the row (e.g. CollectionLinkChildren), given the
|
|
* inline new document input to show above the child documents.
|
|
*/
|
|
children: (newChild: React.ReactNode) => React.ReactNode;
|
|
};
|
|
|
|
function CollectionRow({
|
|
collection,
|
|
depth = 0,
|
|
to,
|
|
onClick,
|
|
onClickIntent,
|
|
isActiveOverride,
|
|
icon,
|
|
expanded,
|
|
onDisclosureClick,
|
|
onExpand,
|
|
canEdit,
|
|
labelText,
|
|
onTitleChange,
|
|
editableTitleRef,
|
|
onEditingChange,
|
|
contextAction,
|
|
menu,
|
|
menuOpen,
|
|
canCreateChild,
|
|
onCreateChild,
|
|
newChildDepth,
|
|
parentRef,
|
|
dropRef,
|
|
isActiveDropTarget,
|
|
children,
|
|
}: CollectionRowProps) {
|
|
const { t } = useTranslation();
|
|
const sidebarContext = useSidebarContext();
|
|
const [isEditing, setIsEditingState] = React.useState(false);
|
|
const setIsEditing = React.useCallback(
|
|
(editing: boolean) => {
|
|
setIsEditingState(editing);
|
|
onEditingChange?.(editing);
|
|
},
|
|
[onEditingChange]
|
|
);
|
|
const [isAddingNewChild, setIsAddingNewChild, closeAddingNewChild] =
|
|
useBoolean();
|
|
const newChildTitleRef = React.useRef<RefHandle>(null);
|
|
|
|
const handleAddChild = React.useCallback(
|
|
(ev: React.MouseEvent<HTMLButtonElement>) => {
|
|
ev.preventDefault();
|
|
setIsAddingNewChild();
|
|
onExpand?.();
|
|
},
|
|
[setIsAddingNewChild, onExpand]
|
|
);
|
|
|
|
const handleNewChildSubmit = React.useCallback(
|
|
async (value: string) => {
|
|
if (!onCreateChild) {
|
|
return;
|
|
}
|
|
try {
|
|
newChildTitleRef.current?.setIsEditing(false);
|
|
await onCreateChild(value);
|
|
closeAddingNewChild();
|
|
} catch (_err) {
|
|
newChildTitleRef.current?.setIsEditing(true);
|
|
}
|
|
},
|
|
[onCreateChild, closeAddingNewChild]
|
|
);
|
|
|
|
const defaultIsActive = React.useCallback(
|
|
(
|
|
_m: match | null,
|
|
location: Location<{ sidebarContext?: SidebarContextType }>
|
|
) => !!_m && location.state?.sidebarContext === sidebarContext,
|
|
[sidebarContext]
|
|
);
|
|
|
|
const labelElement = canEdit ? (
|
|
<EditableTitle
|
|
title={labelText ?? collection.displayName}
|
|
onSubmit={onTitleChange ?? (async () => undefined)}
|
|
isEditing={isEditing}
|
|
onEditing={setIsEditing}
|
|
canUpdate={canEdit}
|
|
maxLength={CollectionValidation.maxNameLength}
|
|
ref={editableTitleRef}
|
|
/>
|
|
) : (
|
|
collection.displayName
|
|
);
|
|
|
|
const iconElement = icon ?? (
|
|
<CollectionIcon collection={collection} expanded={expanded} />
|
|
);
|
|
|
|
const hasMenuContent = Boolean(menu) || canCreateChild;
|
|
const menuVisible = hasMenuContent && !isEditing;
|
|
const menuElement = menuVisible ? (
|
|
<Fade>
|
|
{canCreateChild && (
|
|
<Tooltip content={t("New doc")}>
|
|
<NudeButton
|
|
aria-label={t("New nested document")}
|
|
onClick={handleAddChild}
|
|
>
|
|
<PlusIcon />
|
|
</NudeButton>
|
|
</Tooltip>
|
|
)}
|
|
{menu}
|
|
</Fade>
|
|
) : undefined;
|
|
|
|
const mergedRef = React.useMemo(
|
|
() =>
|
|
mergeRefs<HTMLDivElement>(
|
|
[parentRef, dropRef].filter(Boolean) as React.Ref<HTMLDivElement>[]
|
|
),
|
|
[parentRef, dropRef]
|
|
);
|
|
|
|
const getNavigationItems = React.useCallback(
|
|
() => [
|
|
{
|
|
id: collection.id,
|
|
url: collection.path,
|
|
title: collection.displayName,
|
|
sidebarContext,
|
|
collapsedChildren: expanded ? undefined : collection.sortedDocuments,
|
|
},
|
|
],
|
|
[collection, sidebarContext, expanded]
|
|
);
|
|
useSidebarNavigation(parentRef, getNavigationItems);
|
|
|
|
const sidebarLinkElement = (
|
|
<SidebarLink
|
|
component={isEditing ? StaticLink : undefined}
|
|
depth={depth}
|
|
to={to}
|
|
onClick={onClick}
|
|
onClickIntent={onClickIntent}
|
|
contextAction={contextAction}
|
|
expanded={expanded}
|
|
onDisclosureClick={onDisclosureClick}
|
|
icon={iconElement}
|
|
isActive={isActiveOverride ?? defaultIsActive}
|
|
isActiveDrop={isActiveDropTarget}
|
|
label={labelElement}
|
|
ellipsis={!isEditing}
|
|
exact={false}
|
|
$showActions={menuOpen}
|
|
menu={menuElement}
|
|
/>
|
|
);
|
|
|
|
return (
|
|
<ActionContextProvider
|
|
value={{ activeModels: [collection], sidebarContext }}
|
|
>
|
|
<Relative ref={mergedRef}>
|
|
<DropToImport collectionId={collection.id}>
|
|
{sidebarLinkElement}
|
|
</DropToImport>
|
|
</Relative>
|
|
{children(
|
|
isAddingNewChild && onCreateChild ? (
|
|
<SidebarLink
|
|
isActive={() => true}
|
|
depth={newChildDepth ?? Math.max(depth + 1, 2)}
|
|
ellipsis={false}
|
|
label={
|
|
<EditableTitle
|
|
title=""
|
|
canUpdate
|
|
isEditing
|
|
placeholder={`${t("New doc")}…`}
|
|
onCancel={closeAddingNewChild}
|
|
onSubmit={handleNewChildSubmit}
|
|
maxLength={DocumentValidation.maxTitleLength}
|
|
ref={newChildTitleRef}
|
|
/>
|
|
}
|
|
/>
|
|
) : undefined
|
|
)}
|
|
</ActionContextProvider>
|
|
);
|
|
}
|
|
|
|
export default observer(CollectionRow);
|