CostControl.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useFlag, useParams } from 'common'
  3. import { ExternalLink } from 'lucide-react'
  4. import { useTheme } from 'next-themes'
  5. import Image from 'next/image'
  6. import Link from 'next/link'
  7. import { Alert, AlertTitle, Button } from 'ui'
  8. import { Admonition } from 'ui-patterns/admonition'
  9. import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
  10. import { ProjectUpdateDisabledTooltip } from '../ProjectUpdateDisabledTooltip'
  11. import SpendCapSidePanel from './SpendCapSidePanel'
  12. import {
  13. ScaffoldSection,
  14. ScaffoldSectionContent,
  15. ScaffoldSectionDetail,
  16. } from '@/components/layouts/Scaffold'
  17. import AlertError from '@/components/ui/AlertError'
  18. import { InlineLink } from '@/components/ui/InlineLink'
  19. import NoPermission from '@/components/ui/NoPermission'
  20. import PartnerIcon from '@/components/ui/PartnerIcon'
  21. import { PARTNER_TO_NAME } from '@/components/ui/PartnerManagedResource'
  22. import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
  23. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  24. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  25. import { BASE_PATH, DOCS_URL } from '@/lib/constants'
  26. import { MANAGED_BY } from '@/lib/constants/infrastructure'
  27. import { useOrgSettingsPageStateSnapshot } from '@/state/organization-settings'
  28. export interface CostControlProps {}
  29. const CostControl = ({}: CostControlProps) => {
  30. const { slug } = useParams()
  31. const { resolvedTheme } = useTheme()
  32. const { data: selectedOrganization } = useSelectedOrganizationQuery()
  33. const { isSuccess: isPermissionsLoaded, can: canReadSubscriptions } = useAsyncCheckPermissions(
  34. PermissionAction.BILLING_READ,
  35. 'stripe.subscriptions'
  36. )
  37. const snap = useOrgSettingsPageStateSnapshot()
  38. const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate')
  39. const {
  40. data: subscription,
  41. error,
  42. isPending: isLoading,
  43. isSuccess,
  44. isError,
  45. } = useOrgSubscriptionQuery({ orgSlug: slug }, { enabled: canReadSubscriptions })
  46. const currentPlan = subscription?.plan
  47. const isUsageBillingEnabled = subscription?.usage_billing_enabled ?? false
  48. const canChangeTier =
  49. !projectUpdateDisabled && !['team', 'enterprise', 'platform'].includes(currentPlan?.id || '')
  50. const costControlDisabled = selectedOrganization?.managed_by === MANAGED_BY.AWS_MARKETPLACE
  51. return (
  52. <>
  53. <ScaffoldSection>
  54. <ScaffoldSectionDetail>
  55. <div className="sticky space-y-6 top-12">
  56. <div className="space-y-2">
  57. <p className="text-foreground text-base m-0">Cost Control</p>
  58. <p className="text-sm text-foreground-light m-0">
  59. Allow scaling beyond your plan's{' '}
  60. <InlineLink href={`/org/${slug}/usage`}>included quota</InlineLink>.
  61. </p>
  62. </div>
  63. <div className="space-y-2">
  64. <p className="text-sm text-foreground-light m-0">More information</p>
  65. <div>
  66. <Link
  67. href={`${DOCS_URL}/guides/platform/cost-control#spend-cap`}
  68. target="_blank"
  69. rel="noreferrer"
  70. >
  71. <div className="flex items-center space-x-2 opacity-50 hover:opacity-100 transition">
  72. <p className="text-sm m-0">Spend cap</p>
  73. <ExternalLink size={16} strokeWidth={1.5} />
  74. </div>
  75. </Link>
  76. </div>
  77. </div>
  78. </div>
  79. </ScaffoldSectionDetail>
  80. <ScaffoldSectionContent>
  81. {isPermissionsLoaded && !canReadSubscriptions ? (
  82. <NoPermission resourceText="update this organization's cost control" />
  83. ) : (
  84. <>
  85. {isLoading && (
  86. <div className="space-y-2">
  87. <ShimmeringLoader />
  88. <ShimmeringLoader className="w-3/4" />
  89. <ShimmeringLoader className="w-1/2" />
  90. </div>
  91. )}
  92. {isError && <AlertError subject="Failed to retrieve subscription" error={error} />}
  93. {isSuccess && costControlDisabled && (
  94. <Alert className="flex flex-col items-center gap-y-2 border-0 rounded-none">
  95. <PartnerIcon
  96. organization={{ managed_by: selectedOrganization?.managed_by }}
  97. showTooltip={false}
  98. size="large"
  99. />
  100. <AlertTitle className="text-sm">
  101. The Spend Cap is not available for organizations managed by{' '}
  102. {PARTNER_TO_NAME[selectedOrganization?.managed_by]}.
  103. </AlertTitle>
  104. </Alert>
  105. )}
  106. {isSuccess && !costControlDisabled && (
  107. <div className="space-y-6">
  108. {['team', 'enterprise', 'platform'].includes(currentPlan?.id || '') ? (
  109. <>
  110. <Admonition
  111. type="default"
  112. layout="horizontal"
  113. title={`You will be charged for any additional usage on the ${
  114. currentPlan?.name || ''
  115. } plan`}
  116. description={
  117. <>
  118. {currentPlan?.name || ''} plan requires you to have spend cap off at all
  119. times. Your projects will never become unresponsive. Only when your{' '}
  120. <Link
  121. href={`/org/${slug}/usage`}
  122. className="text-green-900 transition hover:text-green-1000"
  123. >
  124. included usage
  125. </Link>{' '}
  126. is exceeded will you be charged for any additional usage.
  127. </>
  128. }
  129. />
  130. </>
  131. ) : (
  132. <p className="text-sm text-foreground-light">
  133. If you need to go beyond the included quota, simply switch off your spend cap
  134. to pay for additional usage.
  135. </p>
  136. )}
  137. <div className="flex flex-col md:flex-row gap-6">
  138. <div>
  139. <div className="rounded-md bg-surface-200 w-[160px] h-[96px] shadow-sm">
  140. <Image
  141. alt="Spend Cap"
  142. width={160}
  143. height={96}
  144. src={
  145. isUsageBillingEnabled
  146. ? `${BASE_PATH}/img/spend-cap-off${
  147. resolvedTheme?.includes('dark') ? '' : '--light'
  148. }.png`
  149. : `${BASE_PATH}/img/spend-cap-on${
  150. resolvedTheme?.includes('dark') ? '' : '--light'
  151. }.png`
  152. }
  153. />
  154. </div>
  155. </div>
  156. <div>
  157. <p className="mb-1">
  158. Spend cap is {isUsageBillingEnabled ? 'disabled' : 'enabled'}
  159. </p>
  160. <p className="text-sm text-foreground-light">
  161. {isUsageBillingEnabled ? (
  162. <span>You will be charged for usage beyond the included quota.</span>
  163. ) : (
  164. <span>
  165. You won't be charged any extra for usage. However, your projects could
  166. become unresponsive or enter read only mode if you exceed the included
  167. quota.
  168. </span>
  169. )}
  170. </p>
  171. <ProjectUpdateDisabledTooltip projectUpdateDisabled={projectUpdateDisabled}>
  172. <Button
  173. type="default"
  174. className="mt-4 pointer-events-auto"
  175. disabled={!canChangeTier}
  176. onClick={() => snap.setPanelKey('costControl')}
  177. >
  178. Change spend cap
  179. </Button>
  180. </ProjectUpdateDisabledTooltip>
  181. </div>
  182. </div>
  183. </div>
  184. )}
  185. </>
  186. )}
  187. </ScaffoldSectionContent>
  188. </ScaffoldSection>
  189. <SpendCapSidePanel />
  190. </>
  191. )
  192. }
  193. export default CostControl