Entity.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { useParams } from 'common'
  2. import { useEffect } from 'react'
  3. import LanguageSelector from '../LanguageSelector'
  4. import { DOCS_RESOURCE_CONTENT } from '../ProjectAPIDocs.constants'
  5. import ResourceContent from '../ResourceContent'
  6. import type { ContentProps } from './Content.types'
  7. import { tempRemovePostgrestText } from './Content.utils'
  8. import Table from '@/components/to-be-cleaned/Table'
  9. import { useProjectJsonSchemaQuery } from '@/data/docs/project-json-schema-query'
  10. import { useAppStateSnapshot } from '@/state/app-state'
  11. function getColumnType(type: string, format: string) {
  12. // json and jsonb both have type=undefined, so check format instead
  13. if (type === undefined && (format === 'jsonb' || format === 'json')) return 'json'
  14. switch (type) {
  15. case 'string':
  16. return 'string'
  17. case 'integer':
  18. return 'number'
  19. case 'json':
  20. return 'json'
  21. case 'number':
  22. return 'number'
  23. case 'boolean':
  24. return 'boolean'
  25. default:
  26. return type
  27. }
  28. }
  29. export const Entity = ({ language, apikey = '', endpoint = '' }: ContentProps) => {
  30. const { ref } = useParams()
  31. const snap = useAppStateSnapshot()
  32. const resource = snap.activeDocsSection[1]
  33. const { data: jsonSchema, refetch } = useProjectJsonSchemaQuery({ projectRef: ref })
  34. const definition = jsonSchema?.definitions?.[resource]
  35. const columns =
  36. definition?.properties !== undefined
  37. ? Object.entries(definition.properties).map(([id, val]: any) => ({
  38. ...val,
  39. id,
  40. required: (definition?.required ?? []).includes(id),
  41. }))
  42. : []
  43. useEffect(() => {
  44. if (resource !== undefined) refetch()
  45. }, [resource])
  46. if (resource === undefined) return null
  47. return (
  48. <div className="divide-y relative">
  49. <div className="flex items-center justify-between px-4 py-4 sticky top-0 bg-surface-100 z-10 border-b shadow-md">
  50. <div className="flex flex-col gap-y-1">
  51. <h2>{resource}</h2>
  52. <p className="text-sm text-foreground-light">
  53. {definition?.description ?? 'No description available'}
  54. </p>
  55. </div>
  56. <LanguageSelector />
  57. </div>
  58. <div className="space-y-2 px-4 py-4 border-t-0!">
  59. <p className="text-sm text-foreground-light">Columns</p>
  60. <Table
  61. head={[
  62. <Table.th key="name">Name</Table.th>,
  63. <Table.th key="format">Format</Table.th>,
  64. <Table.th key="type">Type</Table.th>,
  65. <Table.th key="description">Description</Table.th>,
  66. ]}
  67. body={columns.map((column) => {
  68. const formattedColumnType = getColumnType(column.type, column.format)
  69. return (
  70. <Table.tr key={column.id}>
  71. <Table.td title={column.id}>{column.id}</Table.td>
  72. <Table.td title={column.format}>
  73. <p className="truncate">{column.format}</p>
  74. </Table.td>
  75. <Table.td title={formattedColumnType}>{formattedColumnType}</Table.td>
  76. <Table.td title={column.description}>
  77. {tempRemovePostgrestText(column.description ?? '').trim()}
  78. </Table.td>
  79. </Table.tr>
  80. )
  81. })}
  82. />
  83. </div>
  84. <ResourceContent
  85. selectedLanguage={language}
  86. snippet={DOCS_RESOURCE_CONTENT.readRows}
  87. codeSnippets={DOCS_RESOURCE_CONTENT.readRows.code({
  88. resourceId: resource,
  89. endpoint,
  90. apikey,
  91. })}
  92. />
  93. <ResourceContent
  94. selectedLanguage={language}
  95. snippet={DOCS_RESOURCE_CONTENT.filtering}
  96. codeSnippets={DOCS_RESOURCE_CONTENT.filtering.code({
  97. resourceId: resource,
  98. endpoint,
  99. apikey,
  100. })}
  101. />
  102. <ResourceContent
  103. selectedLanguage={language}
  104. snippet={DOCS_RESOURCE_CONTENT.insertRows}
  105. codeSnippets={DOCS_RESOURCE_CONTENT.insertRows.code({
  106. resourceId: resource,
  107. endpoint,
  108. apikey,
  109. })}
  110. />
  111. <ResourceContent
  112. selectedLanguage={language}
  113. snippet={DOCS_RESOURCE_CONTENT.updateRows}
  114. codeSnippets={DOCS_RESOURCE_CONTENT.updateRows.code({
  115. resourceId: resource,
  116. endpoint,
  117. apikey,
  118. })}
  119. />
  120. <ResourceContent
  121. selectedLanguage={language}
  122. snippet={DOCS_RESOURCE_CONTENT.deleteRows}
  123. codeSnippets={DOCS_RESOURCE_CONTENT.deleteRows.code({
  124. resourceId: resource,
  125. endpoint,
  126. apikey,
  127. })}
  128. />
  129. <ResourceContent
  130. selectedLanguage={language}
  131. snippet={DOCS_RESOURCE_CONTENT.subscribeChanges}
  132. codeSnippets={DOCS_RESOURCE_CONTENT.subscribeChanges.code({ resourceId: resource })}
  133. />
  134. </div>
  135. )
  136. }