-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdynamicNav.tsx
189 lines (171 loc) · 4.49 KB
/
dynamicNav.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
import {Fragment} from 'react';
import {serverContext} from 'sentry-docs/serverContext';
import {sortPages} from 'sentry-docs/utils';
import {SidebarLink} from './sidebarLink';
import {SmartLink} from './smartLink';
type Node = {
[key: string]: any;
context: {
[key: string]: any;
sidebar_order?: number;
sidebar_title?: string;
title?: string;
};
path: string;
};
type Entity<T> = {
children: T[];
name: string;
node: Node | null;
};
export interface EntityTree extends Entity<EntityTree> {}
export const toTree = (nodeList: Node[]): EntityTree[] => {
const result: EntityTree[] = [];
const level = {result};
nodeList
.sort((a, b) => a.path.localeCompare(b.path))
.forEach(node => {
let curPath = '';
node.path.split('/').reduce((r, name: string) => {
curPath += `${name}/`;
if (!r[name]) {
r[name] = {result: []};
r.result.push({
name,
children: r[name].result,
node: curPath === node.path ? node : null,
});
}
return r[name];
}, level);
});
result.length; // result[0] is undefined without this. wat
return result[0].children;
};
export const renderChildren = (
children: EntityTree[],
exclude: string[],
path: string,
showDepth: number = 0,
depth: number = 0
): React.ReactNode[] => {
return sortPages(
children.filter(
({name, node}) =>
node && !!node.context.title && name !== '' && exclude.indexOf(node.path) === -1
),
({node}) => node!
).map(({node, children: nodeChildren}) => {
// will not be null because of the filter above
if (!node) {
return null;
}
return (
<SidebarLink
to={node.path}
key={node.path}
title={node.context.sidebar_title || node.context.title!}
collapsed={depth >= showDepth}
path={path}
>
{renderChildren(nodeChildren, exclude, path, showDepth, depth + 1)}
</SidebarLink>
);
});
};
type ChildrenProps = {
path: string;
tree: EntityTree[];
exclude?: string[];
showDepth?: number;
};
export function Children({tree, path, exclude = [], showDepth = 0}: ChildrenProps) {
return <Fragment>{renderChildren(tree, exclude, path, showDepth)}</Fragment>;
}
type Props = {
root: string;
tree: EntityTree[];
collapse?: boolean;
exclude?: string[];
noHeadingLink?: boolean;
prependLinks?: [string, string][];
showDepth?: number;
suppressMissing?: boolean;
title?: string;
};
export function DynamicNav({
root,
title,
tree,
collapse = false,
exclude = [],
showDepth = 0,
prependLinks = [],
suppressMissing = false,
noHeadingLink = false,
}: Props) {
if (root.startsWith('/')) {
root = root.substring(1);
}
let entity: EntityTree | undefined;
let currentTree = tree;
const rootBits = root.split('/');
rootBits.forEach(bit => {
entity = currentTree.find(n => n.name === bit);
if (!entity) {
if (!suppressMissing) {
// eslint-disable-next-line no-console
console.warn(`Could not find entity at ${root} (specifically at ${bit})`);
}
return;
}
currentTree = entity.children;
});
if (!entity) {
return null;
}
if (!title && entity.node) {
title = entity.node.context.sidebar_title || entity.node.context.title || '';
}
const parentNode = entity.children
? entity.children.find((n: EntityTree) => n.name === '')
: null;
const {path} = serverContext();
const isActive = path.join('/').indexOf(root) === 0;
const linkPath = `/${path.join('/')}/`;
const headerClassName = 'sidebar-title d-flex align-items-center';
const header =
parentNode && !noHeadingLink ? (
<SmartLink
to={`/${root}/`}
className={headerClassName}
activeClassName="active"
data-sidebar-link
>
<h6>{title}</h6>
</SmartLink>
) : (
<div className={headerClassName} data-sidebar-link>
<h6>{title}</h6>
</div>
);
return (
<li className="mb-3" data-sidebar-branch>
{header}
{(!collapse || isActive) && entity.children && (
<ul className="list-unstyled" data-sidebar-tree>
{prependLinks &&
prependLinks.map(link => (
<SidebarLink to={link[0]} key={link[0]} title={link[1]} path={linkPath} />
))}
<Children
tree={entity.children}
exclude={exclude}
showDepth={showDepth}
path={linkPath}
/>
</ul>
)}
</li>
);
}