-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
Copy pathSidebarRouteTree.tsx
197 lines (184 loc) · 5.58 KB
/
SidebarRouteTree.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*/
import {
useRef,
useEffect,
Fragment,
useState,
useCallback,
useMemo,
} from 'react';
import cn from 'classnames';
import {useRouter} from 'next/router';
import {SidebarButton} from './SidebarButton';
import {SidebarLink} from './SidebarLink';
import {useCollapse} from 'react-collapsed';
import usePendingRoute from 'hooks/usePendingRoute';
import type {RouteItem} from 'components/Layout/getRouteMeta';
import {siteConfig} from 'siteConfig';
interface SidebarRouteTreeProps {
isForceExpanded: boolean;
breadcrumbs: RouteItem[];
routeTree: RouteItem;
level?: number;
}
/**
* CollapseWrapper Component:
* Handles smooth expanding and collapsing of sidebar items.
*/
const CollapseWrapper = ({
isExpanded,
duration,
children,
}: {
isExpanded: boolean;
duration: number;
children: any;
}) => {
const ref = useRef<HTMLDivElement | null>(null);
const timeoutRef = useRef<number | null>(null);
const {getCollapseProps} = useCollapse({isExpanded, duration});
useEffect(() => {
if (typeof window !== 'undefined') {
ref.current && (ref.current.style.pointerEvents = 'none');
timeoutRef.current = window.setTimeout(() => {
ref.current && (ref.current.style.pointerEvents = '');
}, duration + 100);
}
}, [isExpanded, duration]);
return (
<div
ref={ref}
className={cn(isExpanded ? 'opacity-100' : 'opacity-50')}
style={{transition: `opacity ${duration}ms ease-in-out`}}>
<div {...getCollapseProps()}>{children}</div>
</div>
);
};
/**
* SidebarRouteTree Component:
* Dynamically generates the sidebar menu with collapsible sections.
*/
export function SidebarRouteTree({
isForceExpanded,
breadcrumbs,
routeTree,
level = 0,
}: SidebarRouteTreeProps) {
const router = useRouter();
const slug = router.asPath.split(/[?#]/)[0]; // Extract current route path
const pendingRoute = usePendingRoute();
// Memoize the current route list for performance optimization
const currentRoutes = useMemo(
() => routeTree.routes as RouteItem[],
[routeTree.routes]
);
// State to track expanded items
const [expandedItem, setExpandedItem] = useState<string | null>(null);
/**
* Toggle function to handle sidebar dropdowns.
* Closes the currently expanded item if clicked again.
* Ensures only one section is open at a time.
*/
const handleToggle = useCallback((path: string) => {
setExpandedItem((prev) => (prev === path ? null : path));
}, []);
return (
<ul>
{currentRoutes.map(
(
{
path,
title,
routes,
version,
heading,
hasSectionHeader,
sectionHeader,
},
index
) => {
const selected = slug === path;
let listItem = null;
if (!path || heading) {
// Render nested sidebar sections
listItem = (
<SidebarRouteTree
level={level + 1}
isForceExpanded={isForceExpanded}
routeTree={{title, routes}}
breadcrumbs={[]}
/>
);
} else if (routes) {
// Handle collapsible sidebar sections
const isBreadcrumb =
breadcrumbs.length > 1 &&
breadcrumbs[breadcrumbs.length - 1].path === path;
const isExpanded = expandedItem === path;
listItem = (
<li key={`${title}-${path}-${level}-heading`}>
<SidebarButton
key={`${title}-${path}-${level}-link`}
title={title}
heading={false}
level={level}
onClick={() => handleToggle(path)}
isExpanded={isExpanded}
isBreadcrumb={isBreadcrumb}
/>
<CollapseWrapper duration={250} isExpanded={isExpanded}>
<SidebarRouteTree
isForceExpanded={isForceExpanded}
routeTree={{title, routes}}
breadcrumbs={breadcrumbs}
level={level + 1}
/>
</CollapseWrapper>
</li>
);
} else {
// Render individual sidebar links
listItem = (
<li key={`${title}-${path}-${level}-link`}>
<SidebarLink
isPending={pendingRoute === path}
href={path}
selected={selected}
level={level}
title={title}
version={version}
/>
</li>
);
}
// Render section headers if applicable
if (hasSectionHeader) {
let sectionHeaderText = sectionHeader
? sectionHeader.replace('{{version}}', siteConfig.version)
: '';
return (
<Fragment key={`${sectionHeaderText}-${level}-separator`}>
{index !== 0 && (
<li
role="separator"
className="mt-4 mb-2 ms-5 border-b border-border dark:border-border-dark"
/>
)}
<h3
className={cn(
'mb-1 text-sm font-bold ms-5 text-tertiary dark:text-tertiary-dark',
index !== 0 && 'mt-2'
)}>
{sectionHeaderText}
</h3>
</Fragment>
);
}
return listItem;
}
)}
</ul>
);
}