-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathserverSidebar.tsx
150 lines (141 loc) · 4.04 KB
/
serverSidebar.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
import {DocNode, getGuide, getPlatform, nodeForPath} from 'sentry-docs/docTree';
import {serverContext} from 'sentry-docs/serverContext';
import {PlatformGuide} from 'sentry-docs/types';
import {ApiSidebar} from './apiSidebar';
import {Node as PlatformSidebarNode, PlatformSidebar} from './platformSidebar';
import {NavNode, ProductSidebar} from './productSidebar';
import {Sidebar, SidebarNode} from './sidebar';
export function productSidebar(rootNode: DocNode) {
const productNode = nodeForPath(rootNode, 'product');
if (!productNode) {
return null;
}
const nodes: NavNode[] = [
{
context: {
draft: Boolean(productNode.frontmatter.draft),
title: productNode.frontmatter.title,
sidebar_order: productNode.frontmatter.sidebar_order,
sidebar_title: productNode.frontmatter.sidebar_title,
},
path: '/' + productNode.path + '/',
},
];
function addChildren(docNodes: DocNode[]) {
docNodes.forEach(n => {
nodes.push({
context: {
draft: Boolean(n.frontmatter.draft),
title: n.frontmatter.title,
sidebar_order: n.frontmatter.sidebar_order,
sidebar_title: n.frontmatter.sidebar_title,
},
path: '/' + n.path + '/',
});
addChildren(n.children);
});
}
addChildren(productNode.children);
const data = {
allSitePage: {
nodes,
},
};
return <ProductSidebar data={data} />;
}
export function ServerSidebar() {
const {path, rootNode} = serverContext();
if (!rootNode) {
return null;
}
let node = rootNode;
if (path.indexOf('contributing') === 0) {
const maybeNode = nodeForPath(rootNode, 'contributing');
if (maybeNode) {
node = maybeNode;
} else {
return null;
}
} else if (path.indexOf('product') === 0 || path.indexOf('platform-redirect') === 0) {
return productSidebar(rootNode);
} else if (path.indexOf('api') === 0) {
return <ApiSidebar />;
} else if (path.indexOf('platforms') === 0) {
if (path.length === 1) {
return productSidebar(rootNode);
}
const name = path[1];
const platformNode = nodeForPath(rootNode, ['platforms', name]);
if (!platformNode) {
return null;
}
const platform = getPlatform(rootNode, name);
let guide: PlatformGuide | undefined;
if (path.length >= 4 && path[2] === 'guides') {
guide = getGuide(rootNode, name, path[3]);
}
const nodes: PlatformSidebarNode[] = [
{
context: {
platform: {
name,
},
title: platformNode.frontmatter.title,
sidebar_order: platformNode.frontmatter.sidebar_order,
sidebar_title: platformNode.frontmatter.sidebar_title,
},
path: '/' + platformNode.path + '/',
},
];
// eslint-disable-next-line no-inner-declarations
function addChildren(docNodes: DocNode[]) {
docNodes.forEach(n => {
if (n.frontmatter.draft) {
return;
}
nodes.push({
context: {
platform: {
name,
},
title: n.frontmatter.title,
sidebar_order: n.frontmatter.sidebar_order,
sidebar_title: n.frontmatter.sidebar_title,
},
path: '/' + n.path + '/',
});
addChildren(n.children);
});
}
addChildren(platformNode.children);
return (
<PlatformSidebar
platform={{
name,
title: platform?.title || '',
}}
guide={
guide && {
name: guide.name,
title: guide.title || '',
}
}
data={{
allSitePage: {
nodes,
},
}}
/>
);
}
// Must not send full DocNodes to a client component, or the entire doc tree
// will be serialized.
const nodeToSidebarNode = (n: DocNode): SidebarNode => {
return {
path: n.path,
frontmatter: n.frontmatter,
children: n.children.map(nodeToSidebarNode),
};
};
return <Sidebar node={nodeToSidebarNode(node)} path={path} />;
}