Files
supabase/apps/studio/components/interfaces/RedeemCredits/RedeemCredits.tsx
Danny WhiteandJoshen Lim e65da2c3e7 feat(studio): move redeem credits to connect interstitial (#45909)
## What kind of change does this PR introduce?

Feature. Part of DEPR-556.

## What is the current behavior?

The `/redeem` credit redemption page uses the legacy redeem credits
layout and scaffolded two-column UI. It is visually separate from the
newer connect interstitial surfaces.

## What is the new behavior?

`/redeem` now uses the shared connect interstitial layout with a
Supabase logo, signed-in account row, organisation selector, and
full-width redeem action.

The existing `CreditCodeRedemption` modal still owns the actual code
entry, hCaptcha, permission checks, mutation, and success/error
handling. Creating a new organisation links to `/new` with a return URL,
and the returned organisation is preselected when present.

Temporary mock states are available on non-production and Vercel preview
environments:

- `/redeem?mock=loading`
- `/redeem?mock=ready`
- `/redeem?mock=redeeming`
- `/redeem?mock=redeemed`
- `/redeem?mock=already-redeemed`
- `/redeem?mock=invalid`
- `/redeem?mock=wrong-account`
- `/redeem?mock=error`

| Before | After |
| --- | --- |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-66AF2C81-89A0-4F95-A591-3550AAD7112A"
src="https://github.com/user-attachments/assets/1e0dd7af-3168-4566-a7d9-0b889fe69d4e"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-7F9C59DD-73EC-4717-B2B7-3E6BB325273C"
src="https://github.com/user-attachments/assets/60e44213-d2ab-4b5c-b03e-38a3425dbd27"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-A77F2F93-98C3-42E9-B028-049E1AAC0CA3"
src="https://github.com/user-attachments/assets/25087b51-94cb-40c7-9dde-1fc957b7dd8e"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-4C4B7818-16A5-430E-B54F-E7FF34229CA0"
src="https://github.com/user-attachments/assets/4da11122-b547-4e1e-a953-8d2e4c478431"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-A77F2F93-98C3-42E9-B028-049E1AAC0CA3"
src="https://github.com/user-attachments/assets/25087b51-94cb-40c7-9dde-1fc957b7dd8e"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-4C4B7818-16A5-430E-B54F-E7FF34229CA0"
src="https://github.com/user-attachments/assets/4da11122-b547-4e1e-a953-8d2e4c478431"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-49FC982C-2605-41B3-8216-B8F270825247"
src="https://github.com/user-attachments/assets/8e5fc351-6e11-4fef-9d55-1a60fa34a89a"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-6AF18EF8-1699-4341-A900-34E3AEB59703"
src="https://github.com/user-attachments/assets/7adf3d15-73a7-4ef1-9d25-bfcb07b1b15c"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-4F8BBF66-6AF6-403F-B949-079F8E47200B"
src="https://github.com/user-attachments/assets/b05461de-1fd3-4251-9a22-9d1a4dea2b20"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-320E56E7-A291-426B-92B4-5CD4518C85B6"
src="https://github.com/user-attachments/assets/aa172303-0219-425d-9c8b-07044a9f5cac"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-7E55274C-E39E-443B-B636-63B597E11494"
src="https://github.com/user-attachments/assets/4a09d19e-a61c-4c1d-a0a2-e535e53aeee7"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-4D5A1985-D36B-48E1-8108-E95C10C52A12"
src="https://github.com/user-attachments/assets/e3baffe9-e0ac-4dff-8f4c-1b01d34fbfe4"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-B1296556-9DDF-48ED-A0DA-4BEFF5D20FBA"
src="https://github.com/user-attachments/assets/27ed8201-960b-45e3-a32f-115045d0443e"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-193C4266-29BC-4B03-A746-4C2CE3BD29EB"
src="https://github.com/user-attachments/assets/230fb7a0-4049-46b7-84d4-eb549099863b"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-751A900D-A8CF-425C-A745-1511B6DC81E3"
src="https://github.com/user-attachments/assets/55ab5719-a77c-486b-b2d0-5926a524a7b4"
/> |
| <img width="1024" height="759" alt="Redeem Credits
Supabase-29305C15-19B7-4015-9041-62B79E0C95A8"
src="https://github.com/user-attachments/assets/a1b2ba08-2d1a-4a95-a6ac-4d9e3faf9327"
/> | <img width="1024" height="759" alt="Redeem Credits
Supabase-A1628FDB-CFBE-47E7-83D7-3D3D426C3C1E"
src="https://github.com/user-attachments/assets/7fbbf75e-1f8e-478f-83ec-2ab06dff0545"
/> |


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

## Release Notes

* **New Features**
  * Added organization selector to the credits redemption workflow.
* New "Create Organization" card for quick access to organization
creation.

* **Refactor**
* Streamlined the credits redemption page for improved user experience.
  * Enhanced organization card display with customizable descriptions.

* **Style**
  * Removed "Coming soon" beta indicators from action cards.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/45909?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-05-18 19:02:51 +10:00

207 lines
6.8 KiB
TypeScript

import { FeatureFlagContext } from 'common'
import { useRouter } from 'next/router'
import { ReactNode, useContext, useEffect, useRef, useState } from 'react'
import { Button, Card, CardContent } from 'ui'
import { Admonition, ShimmeringLoader } from 'ui-patterns'
import { OrganizationSelector } from '../Connect/OrganizationSelector'
import { CreditCodeRedemption } from '@/components/interfaces/Organization/BillingSettings/CreditCodeRedemption'
import {
InterstitialAccountRow,
InterstitialLayout,
SupabaseLogo,
} from '@/components/layouts/InterstitialLayout'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { useProfile } from '@/lib/profile'
import { EMPTY_ARR } from '@/lib/void'
const RETURN_TO_SELECTED_ORG_PARAM = 'selected_org'
const RedeemCreditsInterstitial = ({
title,
description,
children,
}: {
title: ReactNode
description?: ReactNode
children: ReactNode
}) => (
<InterstitialLayout logo={<SupabaseLogo />} title={title} description={description}>
<div className="px-6 pb-6">{children}</div>
</InterstitialLayout>
)
export const RedeemCreditsScreen = () => {
const router = useRouter()
const { profile, isLoading: isLoadingProfile } = useProfile()
const { hasLoaded } = useContext(FeatureFlagContext)
const [selectedOrgSlug, setSelectedOrgSlug] = useState<string | null>(null)
const [redemptionModalOrgSlug, setRedemptionModalOrgSlug] = useState<string | null>(null)
const appliedReturnSelectedOrgRef = useRef<string | null>(null)
const {
data: organizationOptions = EMPTY_ARR,
error: organizationsError,
isLoading: isLoadingOrganizations,
isError: isOrganizationsError,
} = useOrganizationsQuery()
const returnSelectedOrgSlug =
router.isReady && typeof router.query[RETURN_TO_SELECTED_ORG_PARAM] === 'string'
? router.query[RETURN_TO_SELECTED_ORG_PARAM]
: null
const displayName = profile?.primary_email ?? profile?.username
const isLoading = isLoadingProfile || isLoadingOrganizations || !hasLoaded
useEffect(() => {
if (!returnSelectedOrgSlug) return
if (appliedReturnSelectedOrgRef.current === returnSelectedOrgSlug) return
const hasReturnedOrganization = (organizationOptions ?? []).some(
(organization) => organization.slug === returnSelectedOrgSlug
)
if (hasReturnedOrganization) {
setSelectedOrgSlug(returnSelectedOrgSlug)
appliedReturnSelectedOrgRef.current = returnSelectedOrgSlug
}
}, [organizationOptions, returnSelectedOrgSlug])
if (isLoading) {
return (
<RedeemCreditsInterstitial
title={<ShimmeringLoader className="mx-auto h-7 w-32 max-w-full py-0" />}
description={<ShimmeringLoader className="mx-auto h-4 w-56 max-w-full py-0" />}
>
<ConnectLoadingCards />
</RedeemCreditsInterstitial>
)
}
if (isOrganizationsError) {
return (
<RedeemCreditsInterstitial
title="Unable to load credit redemption"
description="Please try again before redeeming this code"
>
<div className="flex flex-col gap-3">
<Admonition
type="warning"
description={
<>
We could not load your organizations.
{organizationsError && (
<span className="mt-1 block text-foreground-lighter">
Error: {organizationsError.message}
</span>
)}
</>
}
/>
</div>
</RedeemCreditsInterstitial>
)
}
const createOrganizationParams = {
returnTo: router.asPath || '/redeem',
returnToOrgParam: RETURN_TO_SELECTED_ORG_PARAM,
}
const openRedemption = () => {
if (!selectedOrgSlug) return
setRedemptionModalOrgSlug(selectedOrgSlug)
}
return (
<>
<RedeemCreditsInterstitial
title="Redeem credits"
description="Choose an organization to redeem this code"
>
<div className="flex flex-col gap-5">
<InterstitialAccountRow displayName={displayName} />
<OrganizationSelector
organizations={organizationOptions}
selectedSlug={selectedOrgSlug}
onSelect={setSelectedOrgSlug}
getOrganizationDescription={(organization) => `${organization.plan.name} Plan`}
createLabel={
organizationOptions.length === 0
? 'Create your first organization'
: 'Create new organization'
}
createHrefParams={createOrganizationParams}
/>
{organizationOptions.length === 0 && (
<Admonition
type="warning"
description="Create an organization before redeeming this credit code."
/>
)}
<div className="flex flex-col gap-2">
<Button
block
type="primary"
disabled={!selectedOrgSlug || organizationOptions.length === 0}
onClick={openRedemption}
>
Redeem credits
</Button>
<p className="text-center text-xs text-foreground-lighter text-balance">
Credits apply to one organization and are used toward future invoices before your
payment method is charged.
</p>
</div>
</div>
</RedeemCreditsInterstitial>
{redemptionModalOrgSlug && (
<CreditCodeRedemption
modalVisible
slug={redemptionModalOrgSlug}
onClose={() => setRedemptionModalOrgSlug(null)}
/>
)}
</>
)
}
const ConnectLoadingCards = () => (
<div className="flex flex-col gap-5">
<Card className="shadow-none">
<CardContent className="flex items-center gap-3 border-none px-4 py-3">
<ShimmeringLoader className="size-8 flex-shrink-0 rounded-full py-0" />
<div className="min-w-0 flex-1 space-y-2">
<ShimmeringLoader className="h-3 w-20 py-0" />
<ShimmeringLoader className="h-4 w-40 max-w-full py-0" />
</div>
</CardContent>
</Card>
<section className="space-y-2" aria-label="Organizations">
<ShimmeringLoader className="h-3 w-24 py-0" />
{Array.from({ length: 3 }).map((_, index) => (
<Card key={index} className="shadow-none">
<CardContent className="flex items-center gap-3 border-none px-4 py-3">
<ShimmeringLoader className="size-9 flex-shrink-0 rounded-lg py-0" />
<div className="min-w-0 flex-1 space-y-2">
<ShimmeringLoader className="h-4 w-32 py-0" />
<ShimmeringLoader className="h-3 w-20 py-0" />
</div>
</CardContent>
</Card>
))}
</section>
<div className="flex flex-col gap-2">
<ShimmeringLoader className="h-10 w-full py-0" />
<ShimmeringLoader className="h-10 w-full py-0" />
</div>
</div>
)