import type {
NormalizedSidebarGroup,
SidebarDivider as SidebarDividerType,
SidebarItem as SidebarItemType,
SidebarSectionHeader as SidebarSectionHeaderType,
} from '@rspress/core';
import { useActiveMatcher } from '@rspress/core/runtime';
import {
IconArrowRight,
SvgWrapper,
useLinkNavigate,
} from '@rspress/core/theme';
import clsx from 'clsx';
import type React from 'react';
import { SidebarDivider } from './SidebarDivider';
import './SidebarGroup.scss';
import { SidebarItem as SidebarItemComp, SidebarItemRaw } from './SidebarItem';
import { SidebarSectionHeader } from './SidebarSectionHeader';
import {
isSidebarDivider,
isSidebarGroup,
isSidebarSectionHeader,
} from './utils';
const CollapsibleIcon = ({ collapsed }: { collapsed: boolean }) => (
);
export interface SidebarGroupProps {
id: string;
item: NormalizedSidebarGroup;
depth: number;
className?: string;
setSidebarData: React.Dispatch<
React.SetStateAction<
(
| NormalizedSidebarGroup
| SidebarItemType
| SidebarDividerType
| SidebarSectionHeaderType
)[]
>
>;
}
export function SidebarGroup(props: SidebarGroupProps) {
const activeMatcher = useActiveMatcher();
const { item, depth, id, setSidebarData, className } = props;
const navigate = useLinkNavigate();
const active = item.link && activeMatcher(item.link);
const { collapsed = false, collapsible = true } =
item as NormalizedSidebarGroup;
const toggleCollapse = (): void => {
// update collapsed state
setSidebarData(sidebarData => {
const newSidebarData = [...sidebarData];
const indexes = id.split('-').map(Number);
const initialIndex = indexes.shift()!;
const root = newSidebarData[initialIndex];
let current = root;
for (const index of indexes) {
current = (current as NormalizedSidebarGroup).items[index];
}
if ('items' in current) {
current.collapsed = !current.collapsed;
}
return newSidebarData;
});
};
return (
<>
{
if (!active && item.link && !collapsed) {
navigate(item.link);
return;
}
if (item.link) {
e.stopPropagation();
navigate(item.link).then(() => {
collapsible && toggleCollapse();
});
return;
}
e.stopPropagation();
collapsible && toggleCollapse();
}}
right={collapsible && }
/>
{item.items?.map((item, index) =>
isSidebarGroup(item) ? (
) : isSidebarDivider(item) ? (
) : isSidebarSectionHeader(item) ? (
) : (
),
)}
>
);
}