DocsLayout.tsx73 lines · main
1import { useParams } from 'common'
2import { useRouter } from 'next/router'
3import { ReactElement } from 'react'
4
5import { ProjectLayout } from '../ProjectLayout'
6import { generateDocsMenu, getActivePage } from './DocsLayout.utils'
7import Error from '@/components/ui/Error'
8import { ProductMenu } from '@/components/ui/ProductMenu'
9import { useOpenAPISpecQuery } from '@/data/open-api/api-spec-query'
10import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
11import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
12import { withAuth } from '@/hooks/misc/withAuth'
13import { PROJECT_STATUS } from '@/lib/constants'
14
15function DocsLayout({ title, children }: { title: string; children: ReactElement }) {
16 const router = useRouter()
17 const { ref } = useParams()
18 const { data: selectedProject } = useSelectedProjectQuery()
19 const isPaused = selectedProject?.status === PROJECT_STATUS.INACTIVE
20
21 const {
22 data,
23 isPending: isLoading,
24 error,
25 } = useOpenAPISpecQuery({ projectRef: ref }, { enabled: !isPaused })
26
27 const hideMenu = router.pathname.endsWith('/graphiql')
28
29 const { projectAuthAll: authEnabled } = useIsFeatureEnabled(['project_auth:all'])
30
31 const getPage = () => {
32 if (router.pathname.endsWith('graphiql')) return 'graphiql'
33
34 const { page, rpc, resource } = router.query
35 return getActivePage({
36 page: page as string | undefined,
37 resource: resource as string | undefined,
38 rpc: rpc as string | undefined,
39 })
40 }
41
42 if (error) {
43 return (
44 <ProjectLayout product="API Docs">
45 <Error error={error} />
46 </ProjectLayout>
47 )
48 }
49
50 const projectRef = selectedProject?.ref ?? 'default'
51 const tableNames = (data?.tables ?? []).map((table: any) => table.name)
52 const functionNames = (data?.functions ?? []).map((fn: any) => fn.name)
53
54 return (
55 <ProjectLayout
56 isLoading={isLoading}
57 product="API Docs"
58 browserTitle={{ section: title || 'API Docs' }}
59 productMenu={
60 !hideMenu && (
61 <ProductMenu
62 page={getPage()}
63 menu={generateDocsMenu(projectRef, tableNames, functionNames, { authEnabled })}
64 />
65 )
66 }
67 >
68 {children}
69 </ProjectLayout>
70 )
71}
72
73export default withAuth(DocsLayout)