PublicationsTableItem.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { PGPublication, PGTable } from '@supabase/pg-meta'
  2. import { PermissionAction } from '@supabase/shared-types/out/constants'
  3. import { useState } from 'react'
  4. import { toast } from 'sonner'
  5. import { Badge, Switch, TableCell, TableRow, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
  6. import { useDatabasePublicationUpdateMutation } from '@/data/database-publications/database-publications-update-mutation'
  7. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  8. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  9. import { useProtectedSchemas } from '@/hooks/useProtectedSchemas'
  10. interface PublicationsTableItemProps {
  11. table: PGTable
  12. selectedPublication: PGPublication
  13. }
  14. export const PublicationsTableItem = ({
  15. table,
  16. selectedPublication,
  17. }: PublicationsTableItemProps) => {
  18. const { data: project } = useSelectedProjectQuery()
  19. const { data: protectedSchemas } = useProtectedSchemas()
  20. const enabledForAllTables = selectedPublication.tables == null
  21. const isProtected = protectedSchemas.map((x) => x.name).includes(table.schema)
  22. const [checked, setChecked] = useState(
  23. selectedPublication.tables?.find((x: any) => x.id == table.id) != undefined
  24. )
  25. const { can: canUpdatePublications } = useAsyncCheckPermissions(
  26. PermissionAction.TENANT_SQL_ADMIN_WRITE,
  27. 'publications'
  28. )
  29. const { mutate: updatePublications, isPending } = useDatabasePublicationUpdateMutation()
  30. const toggleReplicationForTable = async (table: PGTable, publication: PGPublication) => {
  31. if (project === undefined) return console.error('Project is required')
  32. const originalChecked = checked
  33. setChecked(!checked)
  34. const publicationTables = publication?.tables ?? []
  35. const exists = publicationTables.some((x: any) => x.id == table.id)
  36. const tables = !exists
  37. ? [`${table.schema}.${table.name}`].concat(
  38. publicationTables.map((t: any) => `${t.schema}.${t.name}`)
  39. )
  40. : publicationTables
  41. .filter((x: any) => x.id != table.id)
  42. .map((x: any) => `${x.schema}.${x.name}`)
  43. updatePublications(
  44. {
  45. projectRef: project?.ref,
  46. connectionString: project?.connectionString,
  47. id: publication.id,
  48. tables,
  49. },
  50. {
  51. onSuccess: () => {
  52. toast.success(
  53. `Successfully ${checked ? 'disabled' : 'enabled'} replication for ${table.name}`
  54. )
  55. },
  56. onError: (error) => {
  57. toast.error(`Failed to toggle replication for ${table.name}: ${error.message}`)
  58. setChecked(originalChecked)
  59. },
  60. }
  61. )
  62. }
  63. return (
  64. <TableRow key={table.id}>
  65. <TableCell className="py-3 whitespace-nowrap">{table.name}</TableCell>
  66. <TableCell className="py-3 whitespace-nowrap text-foreground-light">{table.schema}</TableCell>
  67. <TableCell className="py-3 whitespace-nowrap hidden lg:table-cell max-w-sm truncate text-foreground-light">
  68. {table.comment}
  69. </TableCell>
  70. <TableCell className="py-3">
  71. <div className="flex justify-end gap-2">
  72. {enabledForAllTables ? (
  73. <Badge>
  74. <span>Enabled</span>
  75. <span className="hidden lg:inline-block">&nbsp;for all tables</span>
  76. </Badge>
  77. ) : (
  78. <Tooltip>
  79. <TooltipTrigger>
  80. <Switch
  81. size="small"
  82. disabled={!canUpdatePublications || isPending || isProtected}
  83. checked={checked}
  84. onClick={() => toggleReplicationForTable(table, selectedPublication)}
  85. />
  86. </TooltipTrigger>
  87. {isProtected && (
  88. <TooltipContent side="bottom" className="w-64 text-center">
  89. This table belongs to a protected schema, and its publication cannot be toggled
  90. </TooltipContent>
  91. )}
  92. </Tooltip>
  93. )}
  94. </div>
  95. </TableCell>
  96. </TableRow>
  97. )
  98. }