RpcContent.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { useParams } from 'common'
  2. import { DocSection } from './DocSection'
  3. import CodeSnippet from '@/components/interfaces/Docs/CodeSnippet'
  4. import Description from '@/components/interfaces/Docs/Description'
  5. import Param from '@/components/interfaces/Docs/Param'
  6. import Snippets from '@/components/interfaces/Docs/Snippets'
  7. import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query'
  8. import { ProjectJsonSchemaPaths } from '@/data/docs/project-json-schema-query'
  9. /**
  10. * TODO: need to support rpc with the same name and different params type
  11. */
  12. interface RpcContentProps {
  13. rpcId: string
  14. rpcs: { [key: string]: any }
  15. paths?: ProjectJsonSchemaPaths
  16. selectedLang: 'bash' | 'js'
  17. showApiKey: string
  18. refreshDocs: () => void
  19. }
  20. export const RpcContent = ({
  21. rpcId,
  22. rpcs,
  23. paths,
  24. selectedLang,
  25. showApiKey,
  26. refreshDocs,
  27. }: RpcContentProps) => {
  28. const { ref: projectRef } = useParams()
  29. const { data: settings } = useProjectSettingsV2Query({ projectRef })
  30. const protocol = settings?.app_config?.protocol ?? 'https'
  31. const hostEndpoint = settings?.app_config?.endpoint ?? ''
  32. const endpoint = `${protocol}://${hostEndpoint ?? ''}`
  33. const meta = rpcs[rpcId]
  34. const pathKey = `/rpc/${rpcId}`
  35. const path = paths && pathKey in paths ? paths[pathKey] : undefined
  36. const keyToShow = !!showApiKey ? showApiKey : 'BRIVEN_KEY'
  37. const { parameters, summary } = path?.post || {}
  38. const rpcParamsObject =
  39. parameters && parameters[0] && parameters[0].schema && (parameters[0].schema as any).properties
  40. ? (parameters[0].schema as any).properties
  41. : {}
  42. const rpcParams = Object.entries(rpcParamsObject)
  43. .map(([k, v]: any) => ({ name: k, ...v }))
  44. .filter((x) => !!x.name)
  45. if (!path) return null
  46. return (
  47. <div className="flex flex-col flex-1">
  48. <DocSection
  49. title={meta.id}
  50. content={
  51. <>
  52. <label className="font-mono text-xs uppercase text-foreground-lighter inline-block mb-2">
  53. Description
  54. </label>
  55. <Description content={summary ?? ''} metadata={{ rpc: rpcId }} onChange={refreshDocs} />
  56. </>
  57. }
  58. snippets={
  59. <CodeSnippet
  60. selectedLang={selectedLang}
  61. snippet={Snippets.rpcSingle({
  62. rpcName: rpcId,
  63. // @ts-ignore
  64. rpcCamelCase: meta.camelCase,
  65. rpcParams: rpcParams,
  66. apiKey: keyToShow,
  67. endpoint: endpoint,
  68. })}
  69. />
  70. }
  71. />
  72. {rpcParams.length > 0 && (
  73. <div className="flex flex-col flex-1">
  74. <DocSection title="Function Arguments" content={null} />
  75. {rpcParams.map((x, i) => {
  76. return (
  77. <DocSection
  78. key={i}
  79. title={null}
  80. content={
  81. <Param
  82. key={x.name}
  83. name={x.name}
  84. type={x.type}
  85. format={x.format}
  86. required={true}
  87. description={false}
  88. />
  89. }
  90. />
  91. )
  92. })}
  93. </div>
  94. )}
  95. </div>
  96. )
  97. }