Files
supabase/apps/studio/components/ui/UpgradePlanButton.tsx
T
Joshen Lim e803507a0d Add flag disabled UI handler for code redeem page (#42591)
## Context

Adds a UI state for when the feature flag for code redeem is disabled
<img width="1270" height="424" alt="image"
src="https://github.com/user-attachments/assets/90e97869-f1e5-456b-9170-4a0a576f1064"
/>

## Other changes involved

Mostly refactor + clean up here
- Pass `onClick` event to `OrganizationCard`, rather than declaring an
extra parent `div` to handle the click event
- Use `Admonition` instead of the `Alert` components within
`CreditCodeRedemption`
- Adjust element semantics in `CreditCodeRedemption` (Opting for `p`
instead of `h4` and `h1` to show credit related information)
- Use `UpgradePlanButton` component for upgrade CTA (Handles behaviour
depending if user has perms to update plan of the organization)
- Use `TimestampInfo` component for expires at
- Fix error handling when calling `redeemCode` (use `mutate` instead of
`mutateAsync` which requires a try catch)

## To Test

- [ ] Need to verify that the `TimestampInfo` component renders
correctly after redeeming a code

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Organization selection with modal-driven credit code redemption and
URL prefill.
* Upgrade button accepts an organization slug and integrates into the
upgrade flow.
* Organization cards support an optional click handler for custom
interactions.

* **UI/UX Improvements**
* Success/error messaging switched to admonition styling; expiry dates
shown with timestamps.
* Horizontal form layout, medium dialog sizing, compact balance/code
presentation, and invisible CAPTCHA with reset on success.
  * Feature gated by a flag with clearer loading/error states.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-09 11:47:43 +08:00

122 lines
4.0 KiB
TypeScript

import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useFlag, useParams } from 'common'
import { SupportLink } from 'components/interfaces/Support/SupportLink'
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
import { useIsFeatureEnabled } from 'hooks/misc/useIsFeatureEnabled'
import { useSelectedOrganizationQuery } from 'hooks/misc/useSelectedOrganization'
import Link from 'next/link'
import { PropsWithChildren } from 'react'
import { Button } from 'ui'
import { ButtonTooltip } from './ButtonTooltip'
import { RequestUpgradeToBillingOwners } from './RequestUpgradeToBillingOwners'
export const PLAN_REQUEST_EMPTY_PLACEHOLDER =
'<Specify which plan to upgrade to: Pro | Team | Enterprise>'
interface UpgradePlanButtonProps {
/** Stick to camel case for consistency */
source: string
variant?: 'default' | 'primary'
plan?: 'Pro' | 'Team' | 'Enterprise'
addon?: 'pitr' | 'customDomain' | 'spendCap' | 'computeSize'
/** Used in the default message template for request upgrade dialog, e.g: "Upgrade to ..." */
featureProposition?: string
disabled?: boolean
className?: string
slug?: string
}
/**
* If `billingAll` is enabled, links to upgrade paths (e.g organization settings, addons).
*
* Otherwise, links to support form instead
*/
export const UpgradePlanButton = ({
source,
variant = 'primary',
plan = 'Pro',
addon,
featureProposition,
disabled,
children,
className,
slug: slugParam,
}: PropsWithChildren<UpgradePlanButtonProps>) => {
const { ref } = useParams()
const { data: organization } = useSelectedOrganizationQuery()
const isFreePlan = organization?.plan?.id === 'free'
const slug = slugParam ?? organization?.slug ?? '_'
const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate')
const { billingAll } = useIsFeatureEnabled(['billing:all'])
const { can: canUpdateSubscription } = useAsyncCheckPermissions(
PermissionAction.BILLING_WRITE,
'stripe.subscriptions',
undefined,
{ organizationSlug: slug }
)
const subject = `Enquiry to upgrade ${!!plan ? `to ${plan} ` : ''}plan for organization`
const message = `Name: ${organization?.name}\nSlug: ${slug}\nRequested plan: ${plan ?? PLAN_REQUEST_EMPTY_PLACEHOLDER}`
const isRequestingToDisableSpendCap = addon === 'spendCap'
const isOnPaidPlanAndRequestingToPurchaseAddon = !isFreePlan && !!addon
// [Joshen] URL for button based on the "upgrade request" and the org's plan. Falls back to URL for opening subscription plan
const href = isRequestingToDisableSpendCap
? `/org/${slug ?? '_'}/billing?panel=costControl&source=${source}`
: isOnPaidPlanAndRequestingToPurchaseAddon
? addon === 'computeSize'
? `/project/${ref ?? '_'}/settings/compute-and-disk`
: `/project/${ref ?? '_'}/settings/addons?panel=${addon}&source=${source}`
: `/org/${slug ?? '_'}/billing?panel=subscriptionPlan&source=${source}`
const linkChildren = children || (!!addon ? 'Enable add-on' : `Upgrade to ${plan}`)
const link = billingAll ? (
<Link href={href}>{linkChildren}</Link>
) : (
<SupportLink queryParams={{ orgSlug: slug, category: 'Plan_upgrade', subject, message }}>
{linkChildren}
</SupportLink>
)
if (!canUpdateSubscription) {
return (
<RequestUpgradeToBillingOwners
plan={plan}
addon={addon}
featureProposition={featureProposition}
className={className}
>
{children}
</RequestUpgradeToBillingOwners>
)
}
if (projectUpdateDisabled) {
return (
<ButtonTooltip
disabled
type={variant}
className={className}
tooltip={{
content: {
side: 'bottom',
text: 'Plan changes are currently disabled, our engineers are working on a fix',
},
}}
>
{linkChildren}
</ButtonTooltip>
)
}
return (
<Button asChild type={variant} disabled={disabled} className={className}>
{link}
</Button>
)
}