import { Link } from '@tanstack/react-router' import { useFlag, useParams } from 'common' import { CopyIcon, CpuIcon, GlobeIcon, HardDriveIcon, HistoryIcon, LinkIcon, MessageSquareLockIcon, ScrollTextIcon, WorkflowIcon, } from 'lucide-react' import { Fragment, type ReactNode } from 'react' import { Button } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { PageContainer } from 'ui-patterns/PageContainer' import { PageSection, PageSectionContent, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { AddonCategory, AddonRow, type AddonRowProps } from './AddonRow' import { countActiveDatabases, countReadReplicas, formatMonthlyAddonPrice, getActiveCountLabel, getAdvancedDiskDetail, getCustomDomainDisabledReason, getIPv4DisabledReason, getLowestPaidPrice, getPitrAlertState, getPitrDisabledReason, isAdvancedDiskEnabled, isComputeAddonEnabled, LOG_DRAIN_PRICE_LABEL, PIPELINES_PRICE_LABEL, SMS_MFA_PRICE_LABEL, } from './Addons.utils' import CustomDomainSidePanel from './CustomDomainSidePanel' import IPv4SidePanel from './IPv4SidePanel' import { PitrAlert } from './PitrAlert' import PITRSidePanel from './PITRSidePanel' import { countSelectedAddons, getAddons, subscriptionHasHipaaAddon, } from '@/components/interfaces/Billing/Subscription/Subscription.utils' import { formatRoundedCurrency } from '@/components/interfaces/Organization/BillingSettings/Subscription/PlanChangeSummary.utils' import { getMonthlyComputePrice } from '@/components/interfaces/Project/ResumeProjectCost.utils' import { AlertError } from '@/components/ui/AlertError' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { HighAvailabilityDisabledSectionNotice } from '@/components/ui/HighAvailability/HighAvailabilityDisabledSectionNotice' import { InlineLink } from '@/components/ui/InlineLink' import { ResourceList } from '@/components/ui/Resource/ResourceList' import { HorizontalShimmerWithIcon } from '@/components/ui/Shimmers' import { UpgradeToPro } from '@/components/ui/UpgradeToPro' import { useAuthConfigQuery } from '@/data/auth/auth-config-query' import { useDiskAttributesQuery } from '@/data/config/disk-attributes-query' import { useProjectSettingsV2Query } from '@/data/config/project-settings-v2-query' import { useReadReplicasQuery } from '@/data/read-replicas/replicas-query' import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query' import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query' import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { useIsAwsCloudProvider, useIsHighAvailability, useIsOrioleDbInAws, useIsProjectActive, useSelectedProjectQuery, } from '@/hooks/misc/useSelectedProject' import { getComputeSizeLabel } from '@/lib/compute-labels' import { DOCS_URL } from '@/lib/constants' import { getDatabaseMajorVersion, getSemanticVersion } from '@/lib/helpers' import { useAddonsPagePanel } from '@/state/addons-page' const ADDON_GROUPS = ['Networking', 'Database', 'Observability', 'Auth'] as const const FREE_PLAN_REASON = 'Upgrade to Pro to configure add-ons' type AddonGroup = (typeof ADDON_GROUPS)[number] type AddonDestination = | '/project/$ref/settings/infrastructure' | '/project/$ref/database/pipelines' | '/project/$ref/settings/log-drains' | '/project/$ref/auth/mfa' type AddonAction = | { type: 'panel'; disabledReason?: string; onClick: () => void } | { type: 'link'; to: AddonDestination; hash?: string } type AddonEntry = Omit & { id: string group: AddonGroup action: AddonAction footer?: ReactNode } /** Maps a query to AddonRow loading/error props. Failed reads without cached data surface as errors, not inactive. */ const getReadState = (query: { isLoading: boolean; isError: boolean; data: unknown }) => ({ isLoading: query.isLoading, isError: query.isError && query.data === undefined, }) const AddonActionButton = ({ action, isFreePlan, projectRef, }: { action: AddonAction isFreePlan: boolean projectRef?: string }) => { if (action.type === 'panel') { const disabledReason = isFreePlan ? FREE_PLAN_REASON : action.disabledReason return ( Configure ) } if (isFreePlan) { return ( Configure ) } return ( ) } const AddonsLoadingSkeleton = () => ( {Array.from({ length: 3 }).map((_, index) => (
))}
) const AddonsBillingSidebar = ({ isFreePlan, orgSlug, }: { isFreePlan: boolean orgSlug?: string }) => ( ) export const Addons = () => { const { ref: projectRef } = useParams() const { setPanel } = useAddonsPagePanel() const isAws = useIsAwsCloudProvider() const isProjectActive = useIsProjectActive() const isOrioleDbInAws = useIsOrioleDbInAws() === true const isHighAvailability = useIsHighAvailability() const { projectSettingsCustomDomains, projectAddonsDedicatedIpv4Address, projectSettingsLogDrains, databaseReplication, infrastructureReadReplicas, } = useIsFeatureEnabled([ 'project_settings:custom_domains', 'project_addons:dedicated_ipv4_address', 'project_settings:log_drains', 'database:replication', 'infrastructure:read_replicas', ]) const { data: selectedOrg } = useSelectedOrganizationQuery() const { data: selectedProject } = useSelectedProjectQuery() const parentProjectRef = selectedProject?.parent_project_ref const isFreePlan = selectedOrg?.plan?.id === 'free' const { data: settings } = useProjectSettingsV2Query({ projectRef }) const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: selectedOrg?.slug }) const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate') const hasHipaaAddon = subscriptionHasHipaaAddon(subscription) && settings?.is_sensitive === true // Only projects of version greater than supabase-postgrest-14.1.0.44 can use PITR const hasSufficientPgVersion = // introduced as generatedSemantic version could be < 141044 even if actual version is indeed past it // eg. 15.1.1.2 leads to 15112 getDatabaseMajorVersion(selectedProject?.dbVersion ?? '') > 14 || getSemanticVersion(selectedProject?.dbVersion ?? '') >= 141044 const { data: addons, error, isPending: isLoading, isError, isSuccess, } = useProjectAddonsQuery({ projectRef }) const selectedAddons = addons?.selected_addons ?? [] const availableAddons = addons?.available_addons ?? [] const { pitr, customDomain, ipv4, computeInstance } = getAddons(selectedAddons) // These add-ons are configured on their own pages, so we only read their state here const replicasQuery = useReadReplicasQuery( { projectRef }, { enabled: infrastructureReadReplicas } ) const diskQuery = useDiskAttributesQuery({ projectRef }, { enabled: isAws }) const authConfigQuery = useAuthConfigQuery({ projectRef }) const advancedDiskEnabled = isAdvancedDiskEnabled(diskQuery.data?.attributes) const replicaCount = countReadReplicas(replicasQuery.data ?? [], projectRef) // Each database, including read replicas, gets its own IPv4 address and is billed for it const activeDatabaseCount = countActiveDatabases(replicasQuery.data ?? [], projectRef) // Log drains and pipelines are billed per instance, so the project's add-ons carry their counts const logDrainCount = countSelectedAddons(selectedAddons, 'log_drain') const pipelineCount = countSelectedAddons(selectedAddons, 'etl_pipeline') const computeSize = computeInstance?.variant.identifier const computeEnabled = isComputeAddonEnabled(computeSize) const computeDetail = computeEnabled && computeInstance ? `${formatRoundedCurrency(getMonthlyComputePrice(computeInstance.variant))} / month` : 'from $5.00 / month' const smsMfaEnabled = authConfigQuery.data?.MFA_PHONE_ENROLL_ENABLED === true || authConfigQuery.data?.MFA_PHONE_VERIFY_ENABLED === true const canUpdateIPv4 = settings?.db_ip_addr_config === 'ipv6' const ipv4Enabled = ipv4 !== undefined const pitrEnabled = pitr !== undefined const customDomainEnabled = customDomain !== undefined // HIPAA projects must have 28-day PITR. Once enabled it is locked; until then it can be enabled. const hasHipaaCompliantPitr = pitr?.variant.identifier === 'pitr_28' const isPitrLockedByHipaa = hasHipaaAddon && hasHipaaCompliantPitr const ipv4DisabledReason = getIPv4DisabledReason({ isAws, isProjectActive, projectUpdateDisabled, canUpdateIPv4, ipv4Enabled, isHighAvailability, }) const pitrDisabledReason = getPitrDisabledReason({ isProjectActive, projectUpdateDisabled, hasHipaaAddon: isPitrLockedByHipaa, sufficientPgVersion: hasSufficientPgVersion, isOrioleDbInAws, isHighAvailability, }) const customDomainDisabledReason = getCustomDomainDisabledReason({ isProjectActive, projectUpdateDisabled, isHighAvailability, }) const pitrAlertState = getPitrAlertState({ hasHipaaAddon, hasHipaaCompliantPitr, sufficientPgVersion: hasSufficientPgVersion, isOrioleDbInAws, }) const lowestIpv4Price = getLowestPaidPrice(availableAddons, 'ipv4') const lowestCustomDomainPrice = getLowestPaidPrice(availableAddons, 'custom_domain') const lowestPitrPrice = getLowestPaidPrice(availableAddons, 'pitr') const allEntries: Array = [ projectAddonsDedicatedIpv4Address && { id: 'ipv4', group: 'Networking', icon: GlobeIcon, title: 'Dedicated IPv4 address', description: 'Reserve a dedicated IPv4 address for your project.', docsUrl: `${DOCS_URL}/guides/platform/ipv4-address`, isEnabled: ipv4Enabled, status: ipv4Enabled ? 'Enabled' : undefined, detail: ipv4Enabled ? formatMonthlyAddonPrice(ipv4.variant.price * activeDatabaseCount) : formatMonthlyAddonPrice(lowestIpv4Price, 'from '), action: { type: 'panel', disabledReason: ipv4DisabledReason, onClick: () => setPanel('ipv4'), }, }, projectSettingsCustomDomains && { id: 'custom-domain', group: 'Networking', icon: LinkIcon, title: 'Custom domain', description: 'Serve your project on your own domain name.', docsUrl: `${DOCS_URL}/guides/platform/custom-domains`, isEnabled: customDomainEnabled, status: customDomainEnabled ? 'Enabled' : undefined, detail: customDomainEnabled ? formatMonthlyAddonPrice(customDomain.variant.price) : formatMonthlyAddonPrice(lowestCustomDomainPrice), action: { type: 'panel', disabledReason: customDomainDisabledReason, onClick: () => setPanel('customDomain'), }, }, { id: 'compute', group: 'Database', icon: CpuIcon, title: 'Compute size', description: 'Choose the CPU and memory available to your database.', docsUrl: `${DOCS_URL}/guides/platform/compute-and-disk`, isEnabled: computeEnabled, status: computeEnabled ? `${getComputeSizeLabel(computeSize) ?? 'Custom'} Compute` : undefined, detail: computeDetail, action: { type: 'link', to: '/project/$ref/settings/infrastructure', hash: 'compute', }, }, infrastructureReadReplicas && { id: 'read-replicas', group: 'Database', icon: CopyIcon, title: 'Read replicas', description: 'Deploy read-only copies of your database across regions.', docsUrl: `${DOCS_URL}/guides/platform/read-replicas`, isEnabled: replicaCount > 0, ...getReadState(replicasQuery), status: replicaCount > 0 ? getActiveCountLabel(replicaCount) : undefined, detail: 'Billed per replica', action: { type: 'link', to: '/project/$ref/settings/infrastructure', hash: 'read-replicas', }, }, { id: 'pitr', group: 'Database', icon: HistoryIcon, title: 'Point in time recovery', description: 'Restore your database to a specific moment in the past.', docsUrl: `${DOCS_URL}/guides/platform/backups#point-in-time-recovery`, isEnabled: pitrEnabled, status: pitrEnabled ? 'Enabled' : undefined, detail: pitrEnabled ? formatMonthlyAddonPrice(pitr.variant.price) : formatMonthlyAddonPrice(lowestPitrPrice, 'from '), action: { type: 'panel', disabledReason: pitrDisabledReason, onClick: () => setPanel('pitr'), }, footer: ( setPanel('pitr')} /> ), }, isAws && { id: 'advanced-disk', group: 'Database', icon: HardDriveIcon, title: 'Advanced Disk', description: 'Provision IOPS and throughput, or configure a high-performance disk.', docsUrl: `${DOCS_URL}/guides/platform/compute-and-disk#disk`, isEnabled: advancedDiskEnabled, ...getReadState(diskQuery), status: advancedDiskEnabled ? 'Enabled' : undefined, detail: getAdvancedDiskDetail(diskQuery.data?.attributes), action: { type: 'link', to: '/project/$ref/settings/infrastructure', hash: 'advanced', }, }, databaseReplication && { id: 'pipelines', group: 'Database', icon: WorkflowIcon, title: 'Pipelines', description: 'Replicate database changes to external destinations.', docsUrl: `${DOCS_URL}/guides/database/replication/pipelines`, isEnabled: pipelineCount > 0, status: pipelineCount > 0 ? getActiveCountLabel(pipelineCount) : undefined, detail: PIPELINES_PRICE_LABEL, action: { type: 'link', to: '/project/$ref/database/pipelines', }, }, projectSettingsLogDrains && { id: 'log-drains', group: 'Observability', icon: ScrollTextIcon, title: 'Log drains', description: 'Send your project logs to external destinations.', docsUrl: `${DOCS_URL}/guides/telemetry/log-drains`, isEnabled: logDrainCount > 0, status: logDrainCount > 0 ? getActiveCountLabel(logDrainCount) : undefined, detail: LOG_DRAIN_PRICE_LABEL, action: { type: 'link', to: '/project/$ref/settings/log-drains', }, }, { id: 'sms-mfa', group: 'Auth', icon: MessageSquareLockIcon, title: 'Advanced Multi-Factor Auth - SMS', description: 'Let users verify sign-ins with a code sent by SMS.', docsUrl: `${DOCS_URL}/guides/auth/auth-mfa/phone`, isEnabled: smsMfaEnabled, ...getReadState(authConfigQuery), status: smsMfaEnabled ? 'Enabled' : undefined, detail: SMS_MFA_PRICE_LABEL, action: { type: 'link', to: '/project/$ref/auth/mfa', hash: 'sms-mfa', }, }, ] const entries = allEntries.filter((entry): entry is AddonEntry => entry !== false) const activeEntries = entries.filter((entry) => entry.isEnabled) const availableGroups = ADDON_GROUPS.map((group) => ({ group, entries: entries.filter((entry) => entry.group === group && !entry.isEnabled), })).filter(({ entries: groupEntries }) => groupEntries.length > 0) const renderEntry = ({ id, group: _group, action, footer, ...rowProps }: AddonEntry) => ( } /> {footer} ) return (
{isFreePlan && ( )} {parentProjectRef && ( Updating add-ons here will only apply to this preview branch. To manage add-ons for your main branch, please visit the{' '} main branch . )}
{/* Always mounted so screen readers register the region before its text changes */}
{isSuccess && `Add-ons loaded. ${activeEntries.length} ${activeEntries.length === 1 ? 'add-on' : 'add-ons'} active.`}
{isLoading && } {isError && ( )} {isSuccess && activeEntries.length > 0 && ( Active {activeEntries.map(renderEntry)} )} {isSuccess && availableGroups.length > 0 && ( Available {availableGroups.map(({ group, entries: groupEntries }) => ( {groupEntries.map(renderEntry)} ))} )}
{!isHighAvailability && ( <> )}
) }