Introduction.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // @ts-nocheck
  2. import { PermissionAction } from '@supabase/shared-types/out/constants'
  3. import { useParams } from 'common'
  4. import { Copy } from 'lucide-react'
  5. import { useEffect, useState } from 'react'
  6. import { Button, copyToClipboard } from 'ui'
  7. import { Input } from 'ui-patterns/DataInputs/Input'
  8. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  9. import ContentSnippet from '../ContentSnippet'
  10. import { DOCS_CONTENT } from '../ProjectAPIDocs.constants'
  11. import type { ContentProps } from './Content.types'
  12. import { getKeys, useAPIKeysQuery } from '@/data/api-keys/api-keys-query'
  13. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  14. import { useSendEventMutation } from '@/data/telemetry/send-event-mutation'
  15. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  16. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  17. export const Introduction = ({ showKeys, language, apikey, endpoint }: ContentProps) => {
  18. const { ref } = useParams()
  19. const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
  20. const { data: apiKeys } = useAPIKeysQuery({ projectRef: ref }, { enabled: canReadAPIKeys })
  21. useProjectSettingsV2Query({ projectRef: ref })
  22. const { data: org } = useSelectedOrganizationQuery()
  23. const { mutate: sendEvent } = useSendEventMutation()
  24. const [copied, setCopied] = useState<'anon' | 'service'>()
  25. useEffect(() => {
  26. if (copied !== undefined) setTimeout(() => setCopied(undefined), 2000)
  27. }, [copied])
  28. const { anonKey, serviceKey } = getKeys(apiKeys)
  29. const anonApiKey = anonKey?.api_key
  30. const serviceApiKey = serviceKey?.api_key ?? 'BRIVEN_CLIENT_SERVICE_KEY'
  31. return (
  32. <>
  33. <ContentSnippet
  34. selectedLanguage={language}
  35. apikey={apikey}
  36. endpoint={endpoint}
  37. snippet={DOCS_CONTENT.init}
  38. >
  39. <div className="px-4 space-y-6">
  40. <div className="flex flex-col space-x-4 mt-8">
  41. <FormItemLayout isReactForm={false} layout="horizontal" label="Project URL">
  42. <Input disabled readOnly copy size="small" value={endpoint} className="w-full" />
  43. </FormItemLayout>
  44. </div>
  45. <div className="flex flex-col space-x-4">
  46. <FormItemLayout
  47. isReactForm={false}
  48. layout="horizontal"
  49. label="Client API key"
  50. description="This key is safe to use in a browser if you have enabled Row Level Security (RLS) for your tables and configured policies."
  51. >
  52. <Input
  53. disabled
  54. readOnly
  55. size="small"
  56. value={showKeys ? apikey : 'Reveal API keys via dropdown in the header'}
  57. actions={[
  58. <Button
  59. key="copy"
  60. type="default"
  61. icon={<Copy />}
  62. onClick={() => {
  63. setCopied('anon')
  64. copyToClipboard(anonApiKey ?? 'BRIVEN_CLIENT_ANON_KEY')
  65. sendEvent({
  66. action: 'api_docs_code_copy_button_clicked',
  67. properties: {
  68. title: 'Client API key',
  69. selectedLanguage: language,
  70. },
  71. groups: {
  72. project: ref ?? 'Unknown',
  73. organization: org?.slug ?? 'Unknown',
  74. },
  75. })
  76. }}
  77. >
  78. {copied === 'anon' ? 'Copied' : 'Copy'}
  79. </Button>,
  80. ]}
  81. />
  82. </FormItemLayout>
  83. </div>
  84. <div className="flex flex-col space-x-4">
  85. <FormItemLayout
  86. isReactForm={false}
  87. layout="horizontal"
  88. label="Service key"
  89. description={
  90. <p>
  91. This key has the ability to bypass Row Level Security.{' '}
  92. <span className="text-amber-900">Never share it publicly.</span>
  93. </p>
  94. }
  95. >
  96. <Input
  97. disabled
  98. readOnly
  99. size="small"
  100. value={
  101. showKeys
  102. ? (serviceApiKey ?? 'BRIVEN_CLIENT_SERVICE_KEY')
  103. : 'Reveal API keys via dropdown in the header'
  104. }
  105. actions={[
  106. <Button
  107. key="copy"
  108. type="default"
  109. icon={<Copy />}
  110. onClick={() => {
  111. setCopied('service')
  112. copyToClipboard(serviceApiKey)
  113. sendEvent({
  114. action: 'api_docs_code_copy_button_clicked',
  115. properties: {
  116. title: 'Service key',
  117. selectedLanguage: language,
  118. },
  119. groups: {
  120. project: ref ?? 'Unknown',
  121. organization: org?.slug ?? 'Unknown',
  122. },
  123. })
  124. }}
  125. >
  126. {copied === 'service' ? 'Copied' : 'Copy'}
  127. </Button>,
  128. ]}
  129. />
  130. </FormItemLayout>
  131. </div>
  132. </div>
  133. </ContentSnippet>
  134. <ContentSnippet
  135. selectedLanguage={language}
  136. apikey={apikey}
  137. endpoint={endpoint}
  138. snippet={DOCS_CONTENT.clientApiKeys}
  139. />
  140. <ContentSnippet
  141. selectedLanguage={language}
  142. apikey={showKeys ? serviceApiKey : 'BRIVEN_CLIENT_SERVICE_KEY'}
  143. endpoint={endpoint}
  144. snippet={DOCS_CONTENT.serviceApiKeys}
  145. />
  146. </>
  147. )
  148. }