Open History to see the last commit
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'
23import BytesForm from './BytesForm'
24β
25const getSlugAndTag = path => {
26 const parts = path.split('/')
27β
28 if (parts[2] === '1.5.8' || parts[2] === '2.1.4') {
29 return {
30 tag: parts[2],
31 slug: `/docs/${parts.slice(2).join('/')}`,
32 }
33 }
34β
35 return {
36 slug: path,
37 }
38}
39β
40const addTagToSlug = (slug, tag) => {
41 return tag ? `/docs/${tag}/${slug.replace('/docs/', '')}` : slug
42}
43β
44export const LayoutDocs = props => {
45 const router = useRouter()
46 const { slug, tag } = getSlugAndTag(router.asPath)
47 const { routes } = getManifest(tag)
48β
49 const _route = findRouteByPath(removeFromLast(slug, '#'), routes) // @ts-ignore
50β
51 const isMobile = useIsMobile()
52 const { route, prevRoute, nextRoute } = getRouteContext(_route, routes)
53 const title = route && `${route.title}`
54β
55 return (
56 <>
57 {tag && (
58 <Head>
59 <meta name="robots" content="noindex" />
60 </Head>
61 )}
62 <div>
63 {isMobile ? (
64 <>
65 <Nav />
66 <Sticky shadow>
67 <SidebarMobile>
68 <SidebarRoutes isMobile={true} routes={routes} />
69 </SidebarMobile>
70 </Sticky>
71 </>
72 ) : (
73 <Sticky>
74 <Nav />
75 </Sticky>
76 )}
77 <Seo
78 title={props.meta.title || title}
79 description={props.meta.description}
80 />
81 <div className="block">
82 <>
83 <div className="container mx-auto pb-12 pt-6 content">
84 <div className="flex relative">
85 {!isMobile && (
86 <Sidebar fixed>
87 <SidebarRoutes routes={routes} />
88 </Sidebar>
89 )}
90β
91 <div className={s['markdown'] + ' w-full docs'}>
92 <h1 id="_top">{props.meta.title}</h1>
93 <MDXProvider components={MDXComponents}>
94 {props.children}
95 </MDXProvider>
96 <DocsPageFooter
97 href={route?.path || ''}
98 route={route}
99 prevRoute={prevRoute}
100 nextRoute={nextRoute}
101 />
102 </div>
103 {props.meta.toc === false ? null : (
104 <div
105 className="hidden xl:block ml-10 flex-shrink-0"
106 style={{
107 width: 200,
108 }}
109 >
110 <div className="sticky top-24">
111 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
112 On this page
113 </h4>
114 <Toc title={props.meta.title} />
115 <div className="mt-12">
116 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
117 Want to skip the docs?
118 </h4>
119 <p className='text-sm'>
120 Fast track your learning and {' '}
121 <a
122 href="https://ui.dev/checkout/react-query?from=tanstack"
123 className="text-blue-600 font-semibold transition-colors duration-150 ease-out"
124 >
125 take the offical course βοΈ
126 </a>
127 </p>
128 </div>
129 <div className="mt-12 relative">
130 <h4 className="font-semibold uppercase text-sm mb-2 mt-2 text-gray-500">
131 Subscribe to Bytes
132 </h4>
133 <p className="mt-4 text-sm leading-6 mb-4">
134 The best JavaScript newsletter! Delivered every
135 Monday to over 76,000 devs.
136 </p>
137 <BytesForm />
139 </div>
140 </div>
141 </div>
142 )}
143 </div>
144 </div>
145 </>
146 </div>
147 </div>
148 <Footer />
149 <style jsx>{`
150 .docs {
151 min-width: calc(100% - 300px - 1rem - 200px);
152 }
153 `}</style>
154 </>
155 )
156}
157β
158function getCategoryPath(routes) {
159 const route = routes.find(r => r.path)
160 return route && removeFromLast(route.path, '/')
161}
162β
163function SidebarRoutes({ isMobile, routes: currentRoutes, level = 1 }) {
164 const { asPath } = useRouter()
165 let { slug, tag } = getSlugAndTag(asPath)
166 return currentRoutes.map(({ path, title, routes, heading, open }) => {
167 if (routes) {
168 const pathname = getCategoryPath(routes)
169 const selected = slug.startsWith(pathname)
170 const opened = selected || isMobile ? false : open
171β
172 if (heading) {
173 return (
174 <SidebarHeading key={'parent' + pathname} title={title}>
175 <SidebarRoutes
176 isMobile={isMobile}
177 routes={routes}
178 level={level + 1}
179 />
180 </SidebarHeading>
181 )
182 }
183β
184 return (
185 <SidebarCategory
186 key={pathname}
187 isMobile={isMobile}
188 level={level}
189 title={title}
190 selected={selected}
191 opened={opened}
192 >
193 <SidebarRoutes
194 isMobile={isMobile}
195 routes={routes}
196 level={level + 1}
197 />
198 </SidebarCategory>
199 )
200 }
201β
202 const pagePath = removeFromLast(path, '.')
203 const pathname = addTagToSlug(pagePath, tag)
204 const selected = slug === pagePath
205 const route = {
206 href: pagePath,
207 path,
208 title,
209 pathname,
210 selected,
211 }
212 return (
213 <SidebarPost
214 key={title}
215 isMobile={isMobile}
216 level={level}
217 route={route}
218 />
219 )
220 })
221}
222β
223LayoutDocs.displayName = 'LayoutDocs'