import { Link } from '@tanstack/react-router' import { ArrowUpRightIcon, CableIcon, MemoryStickIcon, SettingsIcon, TriangleAlertIcon, type LucideIcon, } from 'lucide-react' import { useState } from 'react' import { Badge, Button, Card, CardContent } from 'ui' import { ConfirmationModal } from 'ui-patterns/Dialogs/ConfirmationModal' import { getMicroUpgradeRows, type UpgradedPlanId } from './PlanUpgradeSuccess.utils' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useOrgProjectsInfiniteQuery, type OrgProject, } from '@/data/projects/org-projects-infinite-query' import { useSetProjectStatus } from '@/data/projects/project-detail-query' import { useProjectAddonUpdateMutation } from '@/data/subscriptions/project-addon-update-mutation' import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2' import { DOCS_URL, INSTANCE_MICRO_SPECS, INSTANCE_NANO_SPECS, PROJECT_STATUS, } from '@/lib/constants' import { pluralize } from '@/lib/helpers' import { useTrack } from '@/lib/telemetry/track' /** How often to refresh project statuses while a project restarts onto Micro. */ const UPGRADE_POLL_INTERVAL_MS = 5000 const SpecChange = ({ icon: Icon, from, to, unit, }: { icon: LucideIcon from: number to: number unit: string }) => (
  • ) const ProjectRow = ({ project, organizationSlug, isUpgrading, onUpgrade, }: { project: OrgProject organizationSlug: string isUpgrading: boolean onUpgrade: () => void }) => { const { can: canUpdateCompute, isSuccess: isPermissionsLoaded } = useAsyncCheckPermissionsV2( FGA_PERMISSIONS.PROJECT.INFRA_DISK_CONFIG_WRITE, { organizationSlug, projectRef: project.ref } ) const isMissingPermission = isPermissionsLoaded && !canUpdateCompute return (
  • {project.name}
    {isUpgrading && ( <> )} {!isUpgrading && ( <> Nano Upgrade {project.name} to Micro )}
  • ) } interface MicroComputeUpgradeCardProps { /** The paid plan the organization just upgraded to. */ planId: UpgradedPlanId organizationSlug: string /** All projects in the organization. The card picks the ones that can move to Micro. */ projects: OrgProject[] } /** * Paid plans bill Nano compute at the Micro rate, so every Nano project left behind after an * upgrade pays for resources it doesn't get. Lists those projects and upgrades each one to * Micro in place, following it until it is back up. */ export const MicroComputeUpgradeCard = ({ planId, organizationSlug, projects, }: MicroComputeUpgradeCardProps) => { const track = useTrack() const { setProjectStatus } = useSetProjectStatus() const [upgradeRequestedRefs, setUpgradeRequestedRefs] = useState>( () => new Set() ) const [projectToUpgrade, setProjectToUpgrade] = useState() const rows = getMicroUpgradeRows(projects, upgradeRequestedRefs) const nanoProjectCount = rows.filter((row) => !row.isUpgrading).length const isAnyProjectUpgrading = rows.some((row) => row.isUpgrading) // Same query key as the list that feeds `projects`, so polling here refreshes the rows. useOrgProjectsInfiniteQuery( { slug: organizationSlug }, { enabled: isAnyProjectUpgrading, refetchInterval: isAnyProjectUpgrading ? UPGRADE_POLL_INTERVAL_MS : false, } ) const { mutate: updateAddon, isPending: isUpdatingAddon } = useProjectAddonUpdateMutation({ onSuccess: (_data, { projectRef }) => { if (!projectRef) return // The project reports RESIZING on its next status poll; show it straight away. setProjectStatus({ ref: projectRef, slug: organizationSlug, status: PROJECT_STATUS.RESIZING }) setUpgradeRequestedRefs((refs) => new Set(refs).add(projectRef)) setProjectToUpgrade(undefined) }, }) if (rows.length === 0) return null // `projectToUpgrade` is a snapshot from when the row was clicked. Only confirm against the // current rows, in case the project changed size or started upgrading from another session. const selectedRow = rows.find((row) => row.project.ref === projectToUpgrade?.ref) const projectToConfirm = selectedRow && !selectedRow.isUpgrading ? selectedRow.project : undefined const handleConfirmUpgrade = () => { if (!projectToConfirm) { setProjectToUpgrade(undefined) return } track( 'plan_upgraded_micro_compute_upgrade_submitted', { planId, nanoProjectCount }, // The screen sits on an org page, so name the project being upgraded. { project: projectToConfirm.ref } ) updateAddon({ projectRef: projectToConfirm.ref, type: 'compute_instance', variant: 'ci_micro' }) } return ( <>

    {rows.length} {pluralize(rows.length, 'project')} can upgrade to Micro Compute for no additional costs

    You're already billed at the Micro Compute rate. Upgrading just unlocks the extra capacity.

      {rows.map(({ project, isUpgrading }) => ( setProjectToUpgrade(project)} /> ))}
    Compute sizes
    setProjectToUpgrade(undefined)} onConfirm={handleConfirmUpgrade} /> ) }