ComputeBadgeWrapper.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import Link from 'next/link'
  2. import { useState } from 'react'
  3. import { Button, cn, HoverCard, HoverCardContent, HoverCardTrigger, Separator } from 'ui'
  4. import { ComputeBadge } from 'ui-patterns/ComputeBadge'
  5. import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
  6. import { getAvailableComputeOptions } from '@/components/interfaces/DiskManagement/DiskManagement.utils'
  7. import { ProjectDetail } from '@/data/projects/project-detail-query'
  8. import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
  9. import { useProjectAddonsQuery } from '@/data/subscriptions/project-addons-query'
  10. import { ResourceWarning } from '@/data/usage/resource-warnings-query'
  11. import { getCloudProviderArchitecture } from '@/lib/cloudprovider-utils'
  12. import { useTrack } from '@/lib/telemetry/track'
  13. export const ChevronsUpAnimated = () => (
  14. <svg
  15. width={10}
  16. height={10}
  17. viewBox="0 0 24 24"
  18. fill="none"
  19. stroke="currentColor"
  20. strokeWidth="2.5"
  21. strokeLinecap="round"
  22. strokeLinejoin="round"
  23. >
  24. <polyline
  25. points="17 18 12 13 7 18"
  26. className="animate-chevron-up"
  27. style={{ animationDelay: '0s' }}
  28. />
  29. <polyline
  30. points="17 11 12 6 7 11"
  31. className="animate-chevron-up"
  32. style={{ animationDelay: '0.3s' }}
  33. />
  34. </svg>
  35. )
  36. const Row = ({ label, stat }: { label: string; stat: React.ReactNode | string }) => {
  37. return (
  38. <div className="flex flex-row gap-2">
  39. <span className="text-sm text-foreground-light w-16">{label}</span>
  40. <span className="text-sm">{stat}</span>
  41. </div>
  42. )
  43. }
  44. interface ComputeBadgeWrapperProps {
  45. slug?: string
  46. projectRef?: string
  47. cloudProvider?: string
  48. computeSize?: ProjectDetail['infra_compute_size']
  49. resourceWarnings?: ResourceWarning
  50. badgeClassName?: string
  51. }
  52. export const ComputeBadgeWrapper = ({
  53. slug,
  54. projectRef,
  55. cloudProvider,
  56. computeSize,
  57. resourceWarnings,
  58. badgeClassName,
  59. }: ComputeBadgeWrapperProps) => {
  60. // handles the state of the hover card
  61. // once open it will fetch the addons
  62. const [open, setOpenState] = useState(false)
  63. // returns hardcoded values for infra
  64. const cpuArchitecture = getCloudProviderArchitecture(cloudProvider)
  65. // fetches addons
  66. const { data: addons, isPending: isLoadingAddons } = useProjectAddonsQuery(
  67. { projectRef },
  68. { enabled: open }
  69. )
  70. // Derive cores/memory from the same source as the badge (infra_compute_size) by looking up
  71. // the matching variant in available_addons. Sourcing from selected_addons can drift out of
  72. // sync with infra_compute_size and produce a card that contradicts its own badge.
  73. const computeOptions = getAvailableComputeOptions(addons?.available_addons ?? [], cloudProvider)
  74. const meta = computeOptions.find((variant) => variant.identifier === `ci_${computeSize}`)?.meta
  75. const highestComputeAvailable = computeOptions[computeOptions.length - 1]?.identifier
  76. const isHighestCompute = computeSize === highestComputeAvailable?.replace('ci_', '')
  77. const { data, isPending: isLoadingSubscriptions } = useOrgSubscriptionQuery(
  78. { orgSlug: slug },
  79. { enabled: open }
  80. )
  81. const isEligibleForFreeUpgrade = data?.plan.id !== 'free' && computeSize === 'nano'
  82. const isComputeNearExhaustion =
  83. !!resourceWarnings?.cpu_exhaustion ||
  84. !!resourceWarnings?.memory_and_swap_exhaustion ||
  85. !!resourceWarnings?.disk_space_exhaustion ||
  86. !!resourceWarnings?.disk_io_exhaustion
  87. const showUpgradeGlow = isEligibleForFreeUpgrade && isComputeNearExhaustion
  88. const track = useTrack()
  89. const isLoading = isLoadingAddons || isLoadingSubscriptions
  90. if (!computeSize) return null
  91. return (
  92. <HoverCard onOpenChange={() => setOpenState(!open)} openDelay={280}>
  93. <HoverCardTrigger asChild className="group" onClick={(e) => e.stopPropagation()}>
  94. <div className={cn('flex items-center', showUpgradeGlow && 'animate-badge-pulse')}>
  95. <div
  96. className={cn(
  97. 'flex',
  98. showUpgradeGlow && 'relative inline-flex overflow-hidden rounded-sm'
  99. )}
  100. >
  101. <ComputeBadge
  102. infraComputeSize={computeSize}
  103. icon={showUpgradeGlow && <ChevronsUpAnimated />}
  104. className={cn(
  105. showUpgradeGlow && 'text-brand-600 border-brand-500 bg-brand/10 gap-1',
  106. badgeClassName
  107. )}
  108. />
  109. {showUpgradeGlow && (
  110. <span className="animate-badge-shimmer pointer-events-none absolute inset-0 bg-linear-to-br from-transparent via-brand/20 to-transparent blur-md" />
  111. )}
  112. </div>
  113. </div>
  114. </HoverCardTrigger>
  115. <HoverCardContent
  116. side="bottom"
  117. align="start"
  118. className="p-0 overflow-hidden w-96"
  119. onClick={(e) => e.stopPropagation()}
  120. >
  121. <div className="p-2 px-5 text-xs text-foreground-lighter">Compute size</div>
  122. <Separator />
  123. <div className="p-3 px-5 flex flex-row gap-4">
  124. <div>
  125. <ComputeBadge infraComputeSize={computeSize} />
  126. </div>
  127. <div className="flex flex-col gap-4">
  128. {isLoading ? (
  129. <>
  130. <div className="flex flex-col gap-1">
  131. <ShimmeringLoader className="h-[20px] py-0 w-32" />
  132. <ShimmeringLoader className="h-[20px] py-0 w-32" />
  133. </div>
  134. </>
  135. ) : (
  136. <>
  137. <div className="flex flex-col gap-1">
  138. {computeSize === 'nano' ? (
  139. <>
  140. <Row label="CPU" stat="Shared" />
  141. <Row label="Memory" stat="Up to 0.5 GB" />
  142. </>
  143. ) : meta !== undefined ? (
  144. <>
  145. <Row
  146. label="CPU"
  147. stat={`${meta.cpu_cores ?? '?'}-core ${cpuArchitecture} ${meta.cpu_dedicated ? '(Dedicated)' : '(Shared)'}`}
  148. />
  149. <Row label="Memory" stat={`${meta.memory_gb ?? '-'} GB`} />
  150. </>
  151. ) : null}
  152. </div>
  153. </>
  154. )}
  155. </div>
  156. </div>
  157. {(!isHighestCompute || isEligibleForFreeUpgrade) && (
  158. <>
  159. <Separator />
  160. <div className="p-3 px-5 text-sm flex flex-col gap-2 bg-studio">
  161. <div className="flex flex-col gap-0">
  162. <p className="text-foreground">
  163. {isEligibleForFreeUpgrade
  164. ? 'Free upgrade to Micro available'
  165. : 'Unlock more compute'}
  166. </p>
  167. <p className="text-foreground-light">
  168. {isEligibleForFreeUpgrade
  169. ? 'Paid plans include a free upgrade to Micro compute.'
  170. : 'Scale your project up to 64 cores and 256 GB RAM.'}
  171. </p>
  172. </div>
  173. <div>
  174. <Button
  175. asChild
  176. type="default"
  177. htmlType="button"
  178. role="button"
  179. onClick={() => {
  180. track('compute_badge_upgrade_clicked', {
  181. computeSize: computeSize ?? 'unknown',
  182. planId: data?.plan.id ?? 'unknown',
  183. upgradeType: isEligibleForFreeUpgrade
  184. ? 'free_micro_upgrade'
  185. : 'compute_upgrade',
  186. })
  187. }}
  188. >
  189. <Link href={`/project/${projectRef}/settings/compute-and-disk`}>
  190. Upgrade compute
  191. </Link>
  192. </Button>
  193. </div>
  194. </div>
  195. </>
  196. )}
  197. </HoverCardContent>
  198. </HoverCard>
  199. )
  200. }