Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

LayoutDocs.js

197 lines5.5 KB

1import * as React from 'react'
2import { MDXProvider } from '@mdx-js/react'
3import { Nav } from 'components/Nav'
4import { Sidebar } from 'components/Sidebar'
5import { SidebarCategory } from 'components/SidebarCategory'
6import { SidebarHeading } from 'components/SidebarHeading'
7import { SidebarMobile } from 'components/SidebarMobile'
8import { SidebarPost } from 'components/SidebarPost'
9import { Sticky } from 'components/Sticky'
10import { useIsMobile } from 'components/useIsMobile'
11import { findRouteByPath } from 'lib/docs/findRouteByPath'
12import { removeFromLast } from 'lib/docs/utils'
13import { getRouteContext } from 'lib/get-route-context'
14import { useRouter } from 'next/router'
15import { Toc } from './Toc'
16import s from './markdown.module.css'
17import { Footer } from './Footer'
18import { DocsPageFooter } from './DocsPageFooter'
19import { Seo } from './Seo'
20import MDXComponents from './MDXComponents'
21import Head from 'next/head'
22import { getManifest } from 'manifests/getManifest'
23​
24const getSlugAndTag = path => {
25 const parts = path.split('/')
26​
27 if (parts[2] === '1.5.8' || parts[2] === '2.1.4') {
28 return {
29 tag: parts[2],
30 slug: `/docs/${parts.slice(2).join('/')}`,
31 }
32 }
33​
34 return {
35 slug: path,
36 }
38​
39const addTagToSlug = (slug, tag) => {
40 return tag ? `/docs/${tag}/${slug.replace('/docs/', '')}` : slug
42​
43export const LayoutDocs = props => {
44 const router = useRouter()
45 const { slug, tag } = getSlugAndTag(router.asPath)
46 const { routes } = getManifest(tag)
47​
48 const _route = findRouteByPath(removeFromLast(slug, '#'), routes) // @ts-ignore
49​
50 const isMobile = useIsMobile()
51 const { route, prevRoute, nextRoute } = getRouteContext(_route, routes)
52 const title = route && `${route.title}`
53​
54 return (
55 <>
56 {tag && (
57 <Head>
58 <meta name="robots" content="noindex" />
59 </Head>
60 )}
61 <div>
62 {isMobile ? (
63 <>
64 <Nav />
65 <Sticky shadow>
66 <SidebarMobile>
67 <SidebarRoutes isMobile={true} routes={routes} />
68 </SidebarMobile>
69 </Sticky>
70 </>
71 ) : (
72 <Sticky>
73 <Nav />
74 </Sticky>
75 )}
76 <Seo
77 title={props.meta.title || title}
78 description={props.meta.description}
79 />
80 <div className="block">
81 <>
82 <div className="container mx-auto pb-12 pt-6 content">
83 <div className="flex relative">
84 {!isMobile && (
85 <Sidebar fixed>
86 <SidebarRoutes routes={routes} />
87 </Sidebar>
88 )}
89​
90 <div className={s['markdown'] + ' w-full docs'}>
91 <h1 id="_top">{props.meta.title}</h1>
92 <MDXProvider components={MDXComponents}>
93 {props.children}
94 </MDXProvider>
95 <DocsPageFooter
96 href={route?.path || ''}
97 route={route}
98 prevRoute={prevRoute}
99 nextRoute={nextRoute}
100 />
101 </div>
102 {props.meta.toc === false ? null : (
103 <div
104 className="hidden xl:block ml-10 flex-shrink-0"
105 style={{
106 width: 200,
107 }}
108 >
109 <div className="sticky top-24 overflow-y-auto">
110 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
111 On this page
112 </h4>
113 <Toc title={props.meta.title} />
114 </div>
115 </div>
116 )}
117 </div>
118 </div>
119 </>
120 </div>
121 </div>
122 <Footer />
123 <style jsx>{`
124 .docs {
125 min-width: calc(100% - 300px - 1rem - 200px);
126 }
127 `}</style>
128 </>
129 )
131​
132function getCategoryPath(routes) {
133 const route = routes.find(r => r.path)
134 return route && removeFromLast(route.path, '/')
136​
137function SidebarRoutes({ isMobile, routes: currentRoutes, level = 1 }) {
138 const { asPath } = useRouter()
139 let { slug, tag } = getSlugAndTag(asPath)
140 return currentRoutes.map(({ path, title, routes, heading, open }) => {
141 if (routes) {
142 const pathname = getCategoryPath(routes)
143 const selected = slug.startsWith(pathname)
144 const opened = selected || isMobile ? false : open
145​
146 if (heading) {
147 return (
148 <SidebarHeading key={'parent' + pathname} title={title}>
149 <SidebarRoutes
150 isMobile={isMobile}
151 routes={routes}
152 level={level + 1}
153 />
154 </SidebarHeading>
155 )
156 }
157​
158 return (
159 <SidebarCategory
160 key={pathname}
161 isMobile={isMobile}
162 level={level}
163 title={title}
164 selected={selected}
165 opened={opened}
166 >
167 <SidebarRoutes
168 isMobile={isMobile}
169 routes={routes}
170 level={level + 1}
171 />
172 </SidebarCategory>
173 )
174 }
175​
176 const pagePath = removeFromLast(path, '.')
177 const pathname = addTagToSlug(pagePath, tag)
178 const selected = slug === pagePath
179 const route = {
180 href: pagePath,
181 path,
182 title,
183 pathname,
184 selected,
185 }
186 return (
187 <SidebarPost
188 key={title}
189 isMobile={isMobile}
190 level={level}
191 route={route}
192 />
193 )
194 })
196​
197LayoutDocs.displayName = 'LayoutDocs'