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