ProjectAPIDocs.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import { useState } from 'react'
  4. import { Button, SidePanel } from 'ui'
  5. import { Bucket } from './Content/Bucket'
  6. import { EdgeFunction } from './Content/EdgeFunction'
  7. import { EdgeFunctions } from './Content/EdgeFunctions'
  8. import { Entities } from './Content/Entities'
  9. import { Entity } from './Content/Entity'
  10. import { Introduction } from './Content/Introduction'
  11. import { Realtime } from './Content/Realtime'
  12. import { RPC } from './Content/RPC'
  13. import { Storage } from './Content/Storage'
  14. import { StoredProcedures } from './Content/StoredProcedures'
  15. import { UserManagement } from './Content/UserManagement'
  16. import { FirstLevelNav } from './FirstLevelNav'
  17. import LanguageSelector from './LanguageSelector'
  18. import { SecondLevelNav } from './SecondLevelNav'
  19. import { getKeys, useAPIKeysQuery } from '@/data/api-keys/api-keys-query'
  20. import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
  21. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  22. import { useAppStateSnapshot } from '@/state/app-state'
  23. /**
  24. * [Joshen] Reminder: when we choose to release this as a main feature
  25. * Ensure that UX is better than the existing, and make sure we do the
  26. * necessary communications around releasing this.
  27. *
  28. * Problems:
  29. * - Needs URL support
  30. * - Language selector is not clear, users are missing the bash language option
  31. * - GraphiQL needs a better home, cannot be placed under Database as its "API"
  32. */
  33. export const ProjectAPIDocs = () => {
  34. const { ref } = useParams()
  35. const snap = useAppStateSnapshot()
  36. const isIntroduction =
  37. snap.activeDocsSection.length === 1 && snap.activeDocsSection[0] === 'introduction'
  38. const isEntityDocs =
  39. snap.activeDocsSection.length === 2 && snap.activeDocsSection[0] === 'entities'
  40. const [showKeys, setShowKeys] = useState(false)
  41. const language = snap.docsLanguage
  42. const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
  43. const { data: apiKeys } = useAPIKeysQuery(
  44. { projectRef: ref },
  45. { enabled: snap.showProjectApiDocs && canReadAPIKeys }
  46. )
  47. const { data: endpoint } = useProjectApiUrl(
  48. { projectRef: ref },
  49. { enabled: snap.showProjectApiDocs }
  50. )
  51. const { anonKey } = getKeys(apiKeys)
  52. const apikey = showKeys
  53. ? (anonKey?.api_key ?? 'BRIVEN_CLIENT_ANON_KEY')
  54. : 'BRIVEN_CLIENT_ANON_KEY'
  55. return (
  56. <SidePanel
  57. hideFooter
  58. size="xxlarge"
  59. className="max-w-5xl"
  60. visible={snap.showProjectApiDocs}
  61. onCancel={() => snap.setShowProjectApiDocs(false)}
  62. >
  63. <div className="flex items-start h-full">
  64. <div className="w-72 border-r h-full">
  65. <div className="border-b px-4 py-2 flex items-center justify-between">
  66. <h4>API Docs</h4>
  67. <div className="flex items-center space-x-1">
  68. {!isEntityDocs && <LanguageSelector simplifiedVersion />}
  69. {isIntroduction && (
  70. <Button type="default" onClick={() => setShowKeys(!showKeys)}>
  71. {showKeys ? 'Hide keys' : 'Show keys'}
  72. </Button>
  73. )}
  74. </div>
  75. </div>
  76. {snap.activeDocsSection.length === 1 ? <FirstLevelNav /> : <SecondLevelNav />}
  77. </div>
  78. <div className="flex-1 divide-y space-y-4 max-h-screen overflow-auto">
  79. {snap.activeDocsSection[0] === 'introduction' && (
  80. <Introduction
  81. showKeys={showKeys}
  82. language={language}
  83. apikey={apikey}
  84. endpoint={endpoint}
  85. />
  86. )}
  87. {snap.activeDocsSection[0] === 'user-management' && (
  88. <UserManagement language={language} apikey={apikey} endpoint={endpoint} />
  89. )}
  90. {snap.activeDocsSection[0] === 'realtime' && <Realtime language={language} />}
  91. {snap.activeDocsSection[0] === 'storage' && (
  92. <>
  93. {snap.activeDocsSection[1] !== undefined ? (
  94. <Bucket language={language} apikey={apikey} endpoint={endpoint} />
  95. ) : (
  96. <Storage language={language} />
  97. )}
  98. </>
  99. )}
  100. {snap.activeDocsSection[0] === 'edge-functions' && (
  101. <>
  102. {snap.activeDocsSection[1] !== undefined ? (
  103. <EdgeFunction language={language} apikey={apikey} endpoint={endpoint} />
  104. ) : (
  105. <EdgeFunctions language={language} />
  106. )}
  107. </>
  108. )}
  109. {snap.activeDocsSection[0] === 'entities' && (
  110. <>
  111. {snap.activeDocsSection[1] !== undefined ? (
  112. <Entity language={language} apikey={apikey} endpoint={endpoint} />
  113. ) : (
  114. <Entities language={language} />
  115. )}
  116. </>
  117. )}
  118. {snap.activeDocsSection[0] === 'stored-procedures' && (
  119. <>
  120. {snap.activeDocsSection[1] !== undefined ? (
  121. <RPC language={language} />
  122. ) : (
  123. <StoredProcedures language={language} />
  124. )}
  125. </>
  126. )}
  127. </div>
  128. </div>
  129. </SidePanel>
  130. )
  131. }