import Link from 'next/link' import { useState } from 'react' import { getAddons } from 'components/interfaces/Billing/Subscription/Subscription.utils' import { ProjectDetail } from 'data/projects/project-detail-query' import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query' import { useProjectAddonsQuery } from 'data/subscriptions/project-addons-query' import { ProjectAddonVariantMeta } from 'data/subscriptions/types' import { getCloudProviderArchitecture } from 'lib/cloudprovider-utils' import { INSTANCE_MICRO_SPECS } from 'lib/constants' import { Button, HoverCard, HoverCardContent, HoverCardTrigger, Separator } from 'ui' import { ComputeBadge } from 'ui-patterns/ComputeBadge' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' const Row = ({ label, stat }: { label: string; stat: React.ReactNode | string }) => { return (
{label} {stat}
) } interface ComputeBadgeWrapperProps { slug?: string projectRef?: string cloudProvider?: string computeSize?: ProjectDetail['infra_compute_size'] } export const ComputeBadgeWrapper = ({ slug, projectRef, cloudProvider, computeSize, }: ComputeBadgeWrapperProps) => { // handles the state of the hover card // once open it will fetch the addons const [open, setOpenState] = useState(false) // returns hardcoded values for infra const cpuArchitecture = getCloudProviderArchitecture(cloudProvider) // fetches addons const { data: addons, isPending: isLoadingAddons } = useProjectAddonsQuery( { projectRef }, { enabled: open } ) const selectedAddons = addons?.selected_addons ?? [] const { computeInstance } = getAddons(selectedAddons) const computeInstanceMeta = computeInstance?.variant?.meta const meta = ( computeInstanceMeta === undefined && computeSize === 'micro' ? INSTANCE_MICRO_SPECS : computeInstanceMeta ) as ProjectAddonVariantMeta const availableCompute = addons?.available_addons.find( (addon) => addon.name === 'Compute Instance' )?.variants const highestComputeAvailable = availableCompute?.[availableCompute.length - 1].identifier const isHighestCompute = computeSize === highestComputeAvailable?.replace('ci_', '') const { data, isPending: isLoadingSubscriptions } = useOrgSubscriptionQuery( { orgSlug: slug }, { enabled: open } ) const isEligibleForFreeUpgrade = data?.plan.id !== 'free' && computeSize === 'nano' const isLoading = isLoadingAddons || isLoadingSubscriptions if (!computeSize) return null return ( setOpenState(!open)} openDelay={280}> e.stopPropagation()}>
e.stopPropagation()} >
Compute size
{isLoading ? ( <>
) : ( <>
{meta !== undefined ? ( <> ) : ( <> {/* meta is only undefined for nano sized compute */} )}
)}
{(!isHighestCompute || isEligibleForFreeUpgrade) && ( <>

{isEligibleForFreeUpgrade ? 'Free upgrade to Micro available' : 'Unlock more compute'}

{isEligibleForFreeUpgrade ? 'Paid plans include a free upgrade to Micro compute.' : 'Scale your project up to 64 cores and 256 GB RAM.'}

)}
) }