useCheckEligibilityDeployReplica.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { useParams } from 'common'
  2. import { useMemo } from 'react'
  3. import { useOverdueInvoicesQuery } from '@/data/invoices/invoices-overdue-query'
  4. import {
  5. getMaxReplicas,
  6. READ_REPLICA_COMPUTE_CAPS,
  7. useReadReplicasQuery,
  8. } from '@/data/read-replicas/replicas-query'
  9. import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query'
  10. import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
  11. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  12. import { useIsAwsK8sCloudProvider, useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  13. export const useCheckEligibilityDeployReplica = () => {
  14. const { ref: projectRef } = useParams()
  15. const isAwsK8s = useIsAwsK8sCloudProvider()
  16. const { data: project } = useSelectedProjectQuery()
  17. const { data: org } = useSelectedOrganizationQuery()
  18. const { hasAccess: hasReadReplicaAccess } = useCheckEntitlements('instances.read_replicas')
  19. const isAWSProvider = project?.cloud_provider === 'AWS'
  20. const isWalgEnabled = project?.is_physical_backups_enabled
  21. const isNotOnHigherPlan = useMemo(
  22. () => !['team', 'enterprise', 'platform'].includes(org?.plan.id ?? ''),
  23. [org]
  24. )
  25. const isProWithSpendCapEnabled = org?.plan.id === 'pro' && !org.usage_billing_enabled
  26. const { data: allOverdueInvoices } = useOverdueInvoicesQuery({
  27. enabled: isNotOnHigherPlan,
  28. })
  29. const overdueInvoices = (allOverdueInvoices ?? []).filter(
  30. (x) => x.organization_id === project?.organization_id
  31. )
  32. const hasOverdueInvoices = overdueInvoices.length > 0 && isNotOnHigherPlan
  33. const { data: databases = [] } = useReadReplicasQuery({ projectRef })
  34. const { data: addons } = useProjectAddonsQuery({ projectRef })
  35. // Will be following the primary's compute size for the time being
  36. const currentComputeAddon = addons?.selected_addons.find(
  37. (addon) => addon.type === 'compute_instance'
  38. )?.variant.identifier
  39. const isBelowSmallCompute =
  40. currentComputeAddon === undefined || READ_REPLICA_COMPUTE_CAPS[currentComputeAddon] === 0
  41. const maxNumberOfReplicas = getMaxReplicas(currentComputeAddon)
  42. const isReachedMaxReplicas =
  43. (databases ?? []).filter((db) => db.identifier !== projectRef).length >= maxNumberOfReplicas
  44. const currentPgVersion = Number(
  45. (project?.dbVersion ?? '').split('briven-postgres-')[1]?.split('.')[0]
  46. )
  47. const canDeployReplica =
  48. !isReachedMaxReplicas &&
  49. currentPgVersion >= 15 &&
  50. isAWSProvider &&
  51. hasReadReplicaAccess &&
  52. isWalgEnabled &&
  53. !hasOverdueInvoices &&
  54. !isAwsK8s &&
  55. !isProWithSpendCapEnabled &&
  56. !isBelowSmallCompute
  57. return {
  58. can: canDeployReplica,
  59. hasOverdueInvoices,
  60. isAWSProvider,
  61. isAwsK8s,
  62. isPgVersionBelow15: currentPgVersion < 15,
  63. isBelowSmallCompute,
  64. isWalgNotEnabled: !isWalgEnabled,
  65. isProWithSpendCapEnabled,
  66. isReachedMaxReplicas,
  67. maxNumberOfReplicas,
  68. }
  69. }