DocsLayout.tsx 2.2 KB

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