OverviewTab.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import { toast } from 'sonner'
  4. import { Admonition } from 'ui-patterns'
  5. import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
  6. import { IntegrationOverviewTab } from '../Integration/IntegrationOverviewTab'
  7. import { IntegrationOverviewTabV2 } from '../Integration/IntegrationOverviewTabV2'
  8. import { useIsMarketplaceEnabled } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
  9. import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
  10. import NoPermission from '@/components/ui/NoPermission'
  11. import { useHooksEnableMutation } from '@/data/database/hooks-enable-mutation'
  12. import { useSchemasQuery } from '@/data/database/schemas-query'
  13. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  14. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  15. export const WebhooksOverviewTab = () => {
  16. const { ref: projectRef } = useParams()
  17. const { data: project } = useSelectedProjectQuery()
  18. const isMarketplaceEnabled = useIsMarketplaceEnabled()
  19. const {
  20. data: schemas,
  21. isSuccess: isSchemasLoaded,
  22. refetch,
  23. } = useSchemasQuery({
  24. projectRef: project?.ref,
  25. connectionString: project?.connectionString,
  26. })
  27. const isHooksEnabled = schemas?.some((schema) => schema.name === 'briven_functions')
  28. const { can: canReadWebhooks, isLoading: isLoadingPermissions } = useAsyncCheckPermissions(
  29. PermissionAction.TENANT_SQL_ADMIN_READ,
  30. 'triggers'
  31. )
  32. const { mutate: enableHooks, isPending: isEnablingHooks } = useHooksEnableMutation({
  33. onSuccess: async () => {
  34. await refetch()
  35. toast.success('Successfully enabled webhooks')
  36. },
  37. })
  38. const enableHooksForProject = async () => {
  39. if (!projectRef) return console.error('Project ref is required')
  40. enableHooks({ ref: projectRef })
  41. }
  42. if (!isSchemasLoaded || isLoadingPermissions) {
  43. return (
  44. <div className="p-10">
  45. <GenericSkeletonLoader />
  46. </div>
  47. )
  48. }
  49. if (!canReadWebhooks) {
  50. return (
  51. <div className="p-10">
  52. <NoPermission isFullPage resourceText="view database webhooks" />
  53. </div>
  54. )
  55. }
  56. if (isMarketplaceEnabled) {
  57. return <IntegrationOverviewTabV2 />
  58. } else {
  59. return (
  60. <IntegrationOverviewTab
  61. hideRequiredExtensionsSection
  62. actions={
  63. isSchemasLoaded && isHooksEnabled ? null : (
  64. <Admonition
  65. showIcon={false}
  66. type="default"
  67. title="Enable database webhooks on your project"
  68. >
  69. <p>
  70. Database Webhooks can be used to trigger serverless functions or send requests to an
  71. HTTP endpoint
  72. </p>
  73. <ButtonTooltip
  74. className="mt-2 w-fit"
  75. onClick={() => enableHooksForProject()}
  76. disabled={isEnablingHooks}
  77. tooltip={{
  78. content: {
  79. side: 'bottom',
  80. text: !canReadWebhooks
  81. ? 'You need additional permissions to enable webhooks'
  82. : undefined,
  83. },
  84. }}
  85. >
  86. Enable webhooks
  87. </ButtonTooltip>
  88. </Admonition>
  89. )
  90. }
  91. />
  92. )
  93. }
  94. }