forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
87 lines (80 loc) · 2.07 KB
/
index.tsx
File metadata and controls
87 lines (80 loc) · 2.07 KB
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
import * as React from 'react'
import Doc from '../Doc'
import { Wrapper, List, Item, ListWrapper, TocList, TocItem } from './elements'
import { compile } from '../../utils'
import { TApi } from '../App'
type State = {
content: string
}
type Props = {
currentPath: string
apis: TApi[]
isLoading: boolean
}
class Api extends React.Component<Props, State> {
state = {
content: null,
}
componentDidMount() {
this.getContent()
}
componentDidUpdate(prevProps) {
if (this.props.currentPath !== prevProps.currentPath) {
this.getContent()
}
}
getContent() {
import('../../../api/' + this.getFileNameFromUrl() + '.md').then((module) =>
this.setState({ content: module })
)
}
getFileNameFromUrl() {
return this.props.currentPath.split('/').pop()
}
renderToc(children) {
return (
<TocList>
{children.map((child) => (
<TocItem key={child.id}>
<a href={`#${child.id}`}>{child.title}</a>
{child.children.length ? this.renderToc(child.children) : null}
</TocItem>
))}
</TocList>
)
}
render() {
if (!this.state.content) {
return (
<Wrapper>
<ListWrapper />
<Doc />
</Wrapper>
)
}
const compiled = compile(this.state.content)
const currentFileName = this.getFileNameFromUrl()
return (
<Wrapper>
<ListWrapper>
<List>
{this.props.apis.map((api) => {
const fileShortName = api.fileName.replace('.md', '')
const isSelected = currentFileName === fileShortName
return (
<Item key={api.fileName} selected={isSelected}>
<a href={`/api/${fileShortName}`}>{api.title}</a>
{isSelected && compiled.toc[0].children.length
? this.renderToc(compiled.toc[0].children)
: null}
</Item>
)
})}
</List>
</ListWrapper>
<Doc>{compiled.tree}</Doc>
</Wrapper>
)
}
}
export default Api