forked from chakra-ui/chakra-ui-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.tsx
88 lines (79 loc) · 2.67 KB
/
changelog.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
import PageContainer from 'components/page-container'
import Sidebar from 'components/sidebar/sidebar'
import componentsSidebar from 'configs/components.sidebar.json'
import gettingStartedSidebar from 'configs/getting-started.sidebar.json'
import hooksSidebar from 'configs/hooks.sidebar.json'
import styledSystemSidebar from 'configs/styled-system.sidebar.json'
import tutorialSidebar from 'configs/tutorial.sidebar.json'
import communitySidebar from 'configs/community.sidebar.json'
import figmaSidebar from 'configs/figma.sidebar.json'
import semverRSort from 'semver/functions/rsort'
import { ReactNode } from 'react'
import { RouteItem } from 'utils/get-route-context'
import { Frontmatter } from 'src/types/frontmatter'
import { List, ListItem } from '@chakra-ui/react'
import SidebarLink from 'components/sidebar/sidebar-link'
import { allChangelogs } from 'contentlayer/generated'
import TocNav from 'components/toc-nav'
import { t } from 'utils/i18n'
export function getRoutes(slug: string): RouteItem[] {
// for home page, use docs sidebar
if (slug === '/') {
return gettingStartedSidebar.routes as RouteItem[]
}
const configMap = {
'/getting-started': gettingStartedSidebar,
'/docs/styled-system': styledSystemSidebar,
'/docs/hooks': hooksSidebar,
'/docs/components': componentsSidebar,
'/tutorial': tutorialSidebar,
'/community': communitySidebar,
'/figma': figmaSidebar,
}
const [, sidebar] =
Object.entries(configMap).find(([path]) => slug.startsWith(path)) ?? []
const routes = sidebar?.routes ?? []
return routes as RouteItem[]
}
export function getVersions(): RouteItem[] {
return semverRSort(
allChangelogs
.filter(({ version }) => version.startsWith('2.'))
.map(({ version }) => version),
).map((version) => ({
title: `v${version}`,
path: `/changelog/${version}`,
}))
}
interface MDXLayoutProps {
frontmatter: Frontmatter
children: ReactNode
hideToc?: boolean
maxWidth?: string
}
export default function MDXLayout(props: MDXLayoutProps) {
const { frontmatter, children, maxWidth } = props
const routes = getRoutes(frontmatter.slug)
const versions = getVersions()
return (
<PageContainer
hideToc={true}
maxWidth={maxWidth}
frontmatter={frontmatter}
leftSidebar={<Sidebar routes={routes} />}
rightSidebar={
<TocNav title={t('component.table-of-content.versions')}>
<List mt={2}>
{versions.map(({ title, path }) => (
<ListItem key={path}>
<SidebarLink href={path}>{title}</SidebarLink>
</ListItem>
))}
</List>
</TocNav>
}
>
{children}
</PageContainer>
)
}