Files
Alaister YoungandAlaister Young 0f14ef4d67 [FE-5109] (part 5) refactor(studio): migrate remaining interfaces and router events off next/router (#54057)
Part 5 of a 6-PR stack migrating all `next/router` and
`next/compat/router` imports in `apps/studio` onto TanStack Router hooks
and removing the router shims.

| Part | PR | Scope |
| --- | --- | --- |
| 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`,
`data/prefetchers/**` |
| 2 | #54054 | `components/ui/**` |
| 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` |
| 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer,
SQL Editor, Storage, Table Editor |
| **5** | **#54057 ← this PR** | **Remaining `components/interfaces/**`
and `QueryInsightsTable` router events** |
| 6 | #54058 | Remove `next/router` & `next/compat/router` shims,
`_router-events.ts`, Vite aliases, and test mocks |

Migrates all remaining `apps/studio/components/interfaces/**`
directories off `next/router`, including the last production
`router.events` consumer (`QueryInsightsTable`) and the only default
`next/router` import (`DiscordCTACard`). After this PR, zero files under
`apps/studio/components/**` import `next/router` or
`next/compat/router`.

**Added:**

-
`apps/studio/components/interfaces/QueryInsights/QueryInsightsTable/QueryInsightsTable.test.tsx`:
verifies that pathname changes clear selected query state without
mutating destination or history search params, while same-pathname
search updates preserve selection

**Changed:**

- Replaced the sole production `router.events.on('routeChangeStart',
...)` consumer in `QueryInsightsTable.tsx` with a `pathname` effect that
clears `onCurrentSelectQuery` on pathname changes, and derived
`searchQuery` directly from `nuqs` (`urlSearch`) instead of mirroring it
in local state. Drops the legacy `setQueryStates({ search: null })` call
on leave, which ran after `history.push` on TanStack Router (targeting
the destination URL rather than the previous history entry, while
browser Back is expected to restore previous search params).
- Removed the `router: NextRouter` prop from `ReportWidgetRendererProps`
in `Reports/ReportWidget.tsx` and updated its callers (`ChartBlock`,
`ReportHeader`, `CreateReportModal`)
- Updated `LOG_ROUTES_WITH_REPLICA_SUPPORT` in
`Settings/Logs/Logs.constants.ts` from `[ref]` to `$ref` for TanStack
route patterns
- Replaced the default `import router from 'next/router'` in
`Support/DiscordCTACard.tsx` with `useNavigate`
- Replaced `useRouter` across `Account`, `Advisors`, `App`, `Auth`,
`ErrorHandling`, `Home`, `HomePageActions`, `Integrations`, `Linter`,
`LocalDropdown`, `Observability`, `Organization`, `OrganizationInvite`,
`Platform/Webhooks`, `Project`, `ProjectCreation`, `ProjectHome`,
`QueryPerformance`, `RedeemCredits`, `Settings`, `Sidebar`, `SignIn`,
`Support`, `UnifiedLogs`, and `UserDropdown`
- Updated affected tests (`Auth/Users/UserPanel`, `Home/ProjectList`,
`LocalDropdown`, `Organization/*`, `OrganizationInvite`,
`RedeemCredits`, `Settings/General/*`, `SignIn/SignInRecoveryCodeForm`,
`UnifiedLogs/*`)

**Simplified:**

- `LocalDropdown`, `UserDropdown` and `RedeemCredits` build the current
route from `location.href`, so the hash keeps its leading `#`
- `NewOrgForm` navigates to the `returnTo` path instead of an absolute
URL, which was being treated as external. It also uses a typed
`/new/$slug` navigate.
- Read search params from `useParams()` instead of `location.search`
casts (`CronJobPage`, `QueuePage`, `LogsPreviewer`,
`LinkSupportTicketPage`, `MarketplaceSidebar`, `PlanCards`,
`SignUpForm`)
- Dropped the dead `isReady`/`isRouterReady` constants and the
`navigate<AnyRouter, string>` casts where typed routes work
(`ForgotPasswordWizard`, `SignInPartner`, `ReportWidget`,
`ProjectUpgradeAlert`)
- Linter, `ReportHeader` and `LogsPreviewer` use `to: '.'` instead of a
separate pathname subscription. `CreateReportModal` builds the preserved
query params in `onSuccess` instead of a `useMemo`.
- Tests use the shared `createTestRouter` and `customRender` router.
`LocalDropdown` now tests that the hash is kept.

Addresses
[FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks)

## To test

- [x] Query Insights: select a query and confirm search/filter updates
keep selection while switching projects/routes clears it; navigating to
Logs keeps Logs' query params intact and pressing Back restores Query
Insights' `?search=` param cleanly
- [x] Auth / Sign-in flows: sign in, sign up, MFA, recovery code,
forgot/reset password, and partner sign-in preserve `returnTo` and
redirect as expected
- [x] Organization & Project flows: creating an org/project, accepting
an org invite, claiming a project, redeeming credits, and
deleting/pausing/restarting a project or branch navigate cleanly
- [ ] Logs & Reports: replica selector appears on supported log routes
(`LOG_ROUTES_WITH_REPLICA_SUPPORT`), saved log queries open, and custom
reports create/navigate properly
- [ ] Sidebar, LocalDropdown, UserDropdown, Integrations
(Cron/Queues/Marketplace), Advisors/Linter, and Unified Logs navigate
cleanly

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 14:53:27 +08:00

741 lines
27 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { Elements } from '@stripe/react-stripe-js'
import type { PaymentIntentResult, PaymentMethod, StripeElementsOptions } from '@stripe/stripe-js'
import { loadStripe } from '@stripe/stripe-js'
import { useQueryClient } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import { useDebounce } from '@uidotdev/usehooks'
import { LOCAL_STORAGE_KEYS } from 'common'
import { groupBy } from 'lodash'
import { HelpCircleIcon } from 'lucide-react'
import { useTheme } from 'next-themes'
import { parseAsBoolean, parseAsString, useQueryStates } from 'nuqs'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { SubmitHandler, useForm, useWatch } from 'react-hook-form'
import { toast } from 'sonner'
import {
Button,
Form,
FormControl,
FormField,
Input,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Switch,
} from 'ui'
import { CollapsibleCardSection } from 'ui-patterns/CollapsibleCardSection'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { z } from 'zod'
import { UpgradeExistingOrganizationCallout } from './UpgradeExistingOrganizationCallout'
import { ChargeBreakdown } from '@/components/interfaces/Billing/ChargeBreakdown'
import { getStripeElementsAppearanceOptions } from '@/components/interfaces/Billing/Payment/Payment.utils'
import { PaymentConfirmation } from '@/components/interfaces/Billing/Payment/PaymentConfirmation'
import {
NewPaymentMethodElement,
type PaymentMethodElementRef,
} from '@/components/interfaces/Billing/Payment/PaymentMethods/NewPaymentMethodElement'
import SpendCapModal from '@/components/interfaces/Billing/SpendCapModal'
import {
ORG_KIND_DEFAULT,
ORG_SIZE_DEFAULT,
onboardingSurveySchema,
type OrgKind,
type OrgSize,
} from '@/components/interfaces/OnboardingSurvey/OnboardingSurvey.constants'
import { OnboardingSurveyFields } from '@/components/interfaces/OnboardingSurvey/OnboardingSurveyFields'
import { markFollowUpPending } from '@/components/interfaces/OnboardingSurvey/OnboardingSurveyFollowUp.utils'
import { InlineLink } from '@/components/ui/InlineLink'
import Panel from '@/components/ui/Panel'
import { useOnboardingSurveyMutation } from '@/data/organizations/onboarding-survey-mutation'
import { useOrganizationCreateMutation } from '@/data/organizations/organization-create-mutation'
import { useOrganizationCreationPreview } from '@/data/organizations/organization-creation-preview'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import type { CustomerAddress, CustomerTaxId } from '@/data/organizations/types'
import { useProjectsInfiniteQuery } from '@/data/projects/projects-infinite-query'
import { SetupIntentResponse } from '@/data/stripe/setup-intent-mutation'
import { useConfirmPendingSubscriptionCreateMutation } from '@/data/subscriptions/org-subscription-confirm-pending-create'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
import { useLastVisitedOrganization } from '@/hooks/misc/useLastVisitedOrganization'
import { getLocalStorageQueryKey } from '@/hooks/misc/useLocalStorage'
import { PRICING_TIER_LABELS_ORG, STRIPE_PUBLIC_KEY } from '@/lib/constants'
import { validateReturnTo } from '@/lib/gotrue'
import { timeout } from '@/lib/helpers'
import { navigateToUrl } from '@/lib/navigation'
import { useProfile } from '@/lib/profile'
import {
classifyApiError,
classifyStripeError,
classifyValidationError,
} from '@/lib/telemetry/funnel-errors'
import { useTrack } from '@/lib/telemetry/track'
import { useTrackFunnelError } from '@/lib/telemetry/use-track-funnel-error'
const SURVEY_SUBMIT_MAX_WAIT_MS = 3_000
interface NewOrgFormProps {
onPaymentMethodReset: () => void
setupIntent?: SetupIntentResponse
onPlanSelected: (plan: string) => void
}
const plans = ['FREE', 'PRO', 'TEAM'] as const
const formSchema = onboardingSurveySchema.extend({
name: z.string().trim().min(1, 'Organization name is required'),
plan: z
.string()
.transform((val) => val.toUpperCase())
.pipe(z.enum(plans)),
spend_cap: z.boolean(),
})
type FormState = z.infer<typeof formSchema>
const stripePromise = loadStripe(STRIPE_PUBLIC_KEY)
const FORM_ID = 'new-org-form'
export const NewOrgForm = ({
onPaymentMethodReset,
setupIntent,
onPlanSelected,
}: NewOrgFormProps) => {
const navigate = useNavigate()
const user = useProfile()
const track = useTrack()
const { resolvedTheme } = useTheme()
const { lastVisitedOrganization } = useLastVisitedOrganization()
const isBillingEnabled = useIsFeatureEnabled('billing:all')
const { data: organizations, isSuccess } = useOrganizationsQuery()
const { data } = useProjectsInfiniteQuery({})
const projects = useMemo(() => data?.pages.flatMap((page) => page.projects) ?? [], [data?.pages])
const freeOrgs = (organizations || []).filter((it) => it.plan.id === 'free')
// [Joshen] JFYI because we're now using a paginated endpoint, there's a chance that not all projects will be
// factored in here (page limit is 100 results). This data is mainly used for the `hasFreeOrgWithProjects` check
// in onSubmit below, which isn't a critical functionality imo so am okay for now. But ideally perhaps this data can
// be computed on the API and returned in /profile or something (since this data is on the account level)
const projectsByOrg = useMemo(() => {
return groupBy(projects, 'organization_slug')
}, [projects])
const stripeOptionsPaymentMethod: StripeElementsOptions = useMemo(
() =>
({
clientSecret: setupIntent ? setupIntent.client_secret! : '',
appearance: getStripeElementsAppearanceOptions(resolvedTheme),
paymentMethodCreation: 'manual',
}) as const,
[setupIntent, resolvedTheme]
)
const [searchParams] = useQueryStates({
returnTo: parseAsString.withDefault(''),
auth_id: parseAsString.withDefault(''),
token: parseAsString.withDefault(''),
})
const [defaultValues] = useQueryStates({
name: parseAsString.withDefault(''),
kind: parseAsString.withDefault(ORG_KIND_DEFAULT),
plan: parseAsString.withDefault('FREE'),
size: parseAsString.withDefault(ORG_SIZE_DEFAULT),
spend_cap: parseAsBoolean.withDefault(true),
})
const form = useForm<FormState>({
resolver: zodResolver(formSchema),
defaultValues: {
plan: defaultValues.plan.toUpperCase() as (typeof plans)[number],
name: defaultValues.name,
kind: defaultValues.kind as OrgKind,
size: defaultValues.size as OrgSize,
spend_cap: defaultValues.spend_cap,
building: '',
can_code: '',
},
})
// What was submitted, fixed at submit: the survey step runs after creation
// (much later after a card confirmation), when the form values may have moved
// on. kind/size are left out of org creation: the survey endpoint saves them
// and keeps only the first response per user and org, so sending them at
// creation too would swallow the survey call.
const orgSubmission = useRef<FormState | null>(null)
useEffect(() => {
form.reset({
plan: defaultValues.plan.toUpperCase() as (typeof plans)[number],
name: defaultValues.name,
kind: defaultValues.kind as OrgKind,
size: defaultValues.size as OrgSize,
spend_cap: defaultValues.spend_cap,
building: form.getValues('building'),
can_code: form.getValues('can_code'),
})
}, [defaultValues, form])
useEffect(() => {
const currentName = form.getValues('name')
if (!currentName && isSuccess && organizations?.length === 0 && user.isSuccess) {
const prefilledOrgName = user.profile?.username ? user.profile.username + `'s Org` : 'My Org'
form.setValue('name', prefilledOrgName)
}
}, [isSuccess, form, organizations?.length, user.profile?.username, user.isSuccess])
const hasTrackedFormExposed = useRef(false)
useEffect(() => {
if (hasTrackedFormExposed.current) return
if (!user.isSuccess) return
hasTrackedFormExposed.current = true
track('organization_creation_form_exposed')
}, [user.isSuccess, track])
const [latestAddress, setLatestAddress] = useState<CustomerAddress>()
const [latestTaxId, setLatestTaxId] = useState<CustomerTaxId | null>()
const billingAddress = useDebounce(latestAddress, 1000)
const billingTaxId = useDebounce(latestTaxId, 1000)
const handleAddressChange = useCallback((address: CustomerAddress) => {
setLatestAddress({
...address,
line2: address.line2 || undefined,
})
}, [])
const handleAddressIncomplete = useCallback(() => {
setLatestAddress(undefined)
}, [])
const handleTaxIdChange = useCallback((taxId: CustomerTaxId | null) => {
setLatestTaxId(taxId)
}, [])
const selectedPlan = useWatch({ control: form.control, name: 'plan' })
const selectedSpendCap = useWatch({
control: form.control,
name: 'spend_cap',
})
useEffect(() => {
if (selectedPlan === 'FREE' || !setupIntent) {
setLatestAddress(undefined)
setLatestTaxId(null)
}
}, [selectedPlan, setupIntent])
const previewTier = useMemo(() => {
if (selectedPlan === 'FREE') return undefined
const dbTier = selectedPlan === 'PRO' && !selectedSpendCap ? 'PAYG' : selectedPlan
return ('tier_' + dbTier.toLowerCase()) as 'tier_pro' | 'tier_payg' | 'tier_team'
}, [selectedPlan, selectedSpendCap])
const {
data: creationPreview,
isFetching: creationPreviewIsFetching,
isSuccess: creationPreviewInitialized,
} = useOrganizationCreationPreview(
{
tier: previewTier,
address: billingAddress,
taxId: billingTaxId ?? undefined,
},
{ enabled: !!previewTier && !!billingAddress }
)
const [newOrgLoading, setNewOrgLoading] = useState(false)
const [paymentMethod, setPaymentMethod] = useState<PaymentMethod>()
const [paymentConfirmationLoading, setPaymentConfirmationLoading] = useState(false)
const [showSpendCapHelperModal, setShowSpendCapHelperModal] = useState(false)
const [paymentIntentSecret, setPaymentIntentSecret] = useState<string | null>(null)
const hasFreeOrgWithProjects = useMemo(
() => freeOrgs.some((it) => projectsByOrg[it.slug]?.length > 0),
[freeOrgs, projectsByOrg]
)
const trackFunnelError = useTrackFunnelError()
const { mutate: createOrganization } = useOrganizationCreateMutation({
onSuccess: async (org) => {
if ('pending_payment_intent_secret' in org && org.pending_payment_intent_secret) {
setPaymentIntentSecret(org.pending_payment_intent_secret)
} else {
await onOrganizationCreated(org as { slug: string })
}
},
onError: (data) => {
const toastId = toast.error(data.message, {
duration: 10_000,
dismissible: true,
position: 'bottom-center',
})
trackFunnelError('org_creation', classifyApiError('org_creation', data), 'toast', toastId)
setNewOrgLoading(false)
},
})
const { mutateAsync: submitOnboardingSurvey } = useOnboardingSurveyMutation()
const queryClient = useQueryClient()
const submitOrganizationSurvey = async (slug: string) => {
const submission = orgSubmission.current
if (!submission) return
const { building, can_code: canCode, kind } = submission
const size = kind === 'COMPANY' ? submission.size : undefined
// Kind and size always hold a value, so only count them as answered when changed
const hasKind = kind !== defaultValues.kind.toUpperCase()
const hasSize = size !== undefined && size !== defaultValues.size
const hasBuilding = building !== ''
const hasCanCode = canCode !== ''
const profileId = user.profile?.gotrue_id
const surveyProperties = {
surface: 'org_form' as const,
hasKind,
hasBuilding,
hasCanCode,
}
const groupOverrides = { organization: slug }
const markSurveyFollowUpPending = () => {
if (!profileId) return
markFollowUpPending({ profileId, orgSlug: slug, kind, size })
// A slow survey call can fail after the redirect to the new org, once the
// follow-up card has already read (and cached) this key as empty
queryClient.invalidateQueries({
queryKey: getLocalStorageQueryKey(
LOCAL_STORAGE_KEYS.ONBOARDING_SURVEY_FOLLOW_UP(profileId, slug)
),
})
}
// Nothing answered: send nothing yet so the creator's follow-up answers
// aren't dropped (the survey endpoint keeps one response per user and org).
// Kind/size ride along with the follow-up, since this is their only save path.
if (!hasKind && !hasSize && !hasBuilding && !hasCanCode) {
track(
'onboarding_survey_dismissed',
{ surface: 'org_form', reason: 'org_form_blank' },
groupOverrides
)
markSurveyFollowUpPending()
return
}
track('onboarding_survey_submit_button_clicked', surveyProperties, groupOverrides)
try {
await submitOnboardingSurvey({
slug,
kind,
size,
building: building || undefined,
can_code: canCode || undefined,
})
} catch {
track('onboarding_survey_submit_failed', surveyProperties, groupOverrides)
markSurveyFollowUpPending()
}
}
const { mutate: confirmPendingSubscriptionChange } = useConfirmPendingSubscriptionCreateMutation({
onSuccess: async (data) => {
if (data && 'slug' in data) {
await onOrganizationCreated({ slug: data.slug })
}
},
onError: (error) => {
const toastId = toast.error(error.message, {
dismissible: true,
duration: 10_000,
})
trackFunnelError('org_creation', classifyApiError('org_creation', error), 'toast', toastId)
},
})
const paymentIntentConfirmed = async (paymentIntentConfirmation: PaymentIntentResult) => {
// Reset payment intent secret to ensure another attempt works as expected
setPaymentIntentSecret('')
const submission = orgSubmission.current
if (paymentIntentConfirmation.paymentIntent?.status === 'succeeded' && submission) {
await confirmPendingSubscriptionChange({
payment_intent_id: paymentIntentConfirmation.paymentIntent.id,
name: submission.name,
})
} else {
// If the payment intent is not successful, we reset the payment method and show an error
const toastId = toast.error(
`Could not confirm payment. Please try again or use a different card.`,
{ duration: 10_000 }
)
trackFunnelError(
'org_creation',
classifyStripeError(paymentIntentConfirmation.error),
'toast',
toastId
)
resetPaymentMethod()
setNewOrgLoading(false)
}
}
const onOrganizationCreated = async (org: { slug: string }) => {
if (submittedTier.current) {
track(
'organization_creation_completed',
{ tier: submittedTier.current },
{ organization: org.slug }
)
}
await Promise.race([submitOrganizationSurvey(org.slug), timeout(SURVEY_SUBMIT_MAX_WAIT_MS)])
const prefilledProjectName = user.profile?.username
? user.profile.username + `'s Project`
: 'My Project'
if (searchParams.returnTo) {
const url = new URL(validateReturnTo(searchParams.returnTo, '/'), window.location.origin)
if (searchParams.auth_id) {
url.searchParams.set('auth_id', searchParams.auth_id)
}
if (searchParams.token) {
url.searchParams.set('token', searchParams.token)
}
navigateToUrl(navigate, `${url.pathname}${url.search}${url.hash}`)
} else {
navigate({
to: '/new/$slug',
params: { slug: org.slug },
search: { projectName: prefilledProjectName },
})
}
}
const stripeOptionsConfirm = useMemo(() => {
return {
clientSecret: paymentIntentSecret,
appearance: getStripeElementsAppearanceOptions(resolvedTheme),
} as StripeElementsOptions
}, [paymentIntentSecret, resolvedTheme])
const submittedTier = useRef<'tier_free' | 'tier_pro' | 'tier_payg' | 'tier_team' | null>(null)
async function createOrg(
formValues: FormState,
paymentMethodId?: string,
customerData?: {
address: CustomerAddress | null
billing_name: string | null
tax_id: CustomerTaxId | null
}
) {
const dbTier = formValues.plan === 'PRO' && !formValues.spend_cap ? 'PAYG' : formValues.plan
const tier = ('tier_' + dbTier.toLowerCase()) as
| 'tier_payg'
| 'tier_pro'
| 'tier_free'
| 'tier_team'
submittedTier.current = tier
createOrganization({
name: formValues.name,
tier,
payment_method: paymentMethodId,
billing_name: dbTier === 'FREE' ? undefined : customerData?.billing_name,
address: dbTier === 'FREE' ? null : customerData?.address,
tax_id: dbTier === 'FREE' ? undefined : (customerData?.tax_id ?? undefined),
})
}
const paymentRef = useRef<PaymentMethodElementRef | null>(null)
const onSubmit: SubmitHandler<z.infer<typeof formSchema>> = async (formValues) => {
setNewOrgLoading(true)
orgSubmission.current = formValues
if (formValues.plan === 'FREE') {
await createOrg(formValues)
return
}
const result = await paymentRef.current?.createPaymentMethod()
if (!result) {
setNewOrgLoading(false)
return
}
setPaymentMethod(result.paymentMethod)
createOrg(formValues, result.paymentMethod.id, {
address: result.address,
billing_name: result.customerName,
tax_id: result.taxId,
})
}
const resetPaymentMethod = () => {
setPaymentMethod(undefined)
return onPaymentMethodReset()
}
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit, (errors) =>
trackFunnelError('org_creation', classifyValidationError('org_creation', errors), 'form')
)}
id={FORM_ID}
>
<Panel
title={
<div key="panel-title">
<h3>Create a new organization</h3>
<p className="text-sm text-foreground-lighter text-balance">
Organizations are a way to group your projects. Each organization can be configured
with different team members and billing settings.
</p>
</div>
}
footer={
<div key="panel-footer" className="flex w-full items-center justify-between">
<Button
disabled={newOrgLoading || paymentConfirmationLoading}
onClick={() => {
if (!!lastVisitedOrganization) {
navigate({ to: '/org/$slug', params: { slug: lastVisitedOrganization } })
} else {
navigate({ to: '/organizations' })
}
}}
>
Cancel
</Button>
<Button
form={FORM_ID}
type="submit"
variant="primary"
loading={newOrgLoading}
disabled={newOrgLoading || creationPreviewIsFetching}
>
Create organization
</Button>
</div>
}
// Allow address dropdown in Stripe Elements to overflow the panel
noHideOverflow
// Prevent resulting rounded corners in footer being clipped by squared corners of bg
titleClasses="rounded-t-md"
footerClasses="rounded-b-md"
>
<div className="divide-y divide-border-muted">
<Panel.Content>
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItemLayout
label="Name"
layout="horizontal"
description="What's the name of your company or team? You can change this later."
>
<FormControl>
<Input
autoFocus
type="text"
placeholder="Organization name"
data-1p-ignore
data-lpignore="true"
data-form-type="other"
data-bwignore
{...field}
/>
</FormControl>
</FormItemLayout>
)}
/>
</Panel.Content>
{isBillingEnabled && (
<Panel.Content>
<FormField
control={form.control}
name="plan"
render={({ field }) => (
<FormItemLayout
label="Plan"
layout="horizontal"
description={
<>
Which plan fits your organization's needs best?{' '}
<InlineLink href="https://supabase.com/pricing">Learn more</InlineLink>.
</>
}
>
<FormControl>
<Select
value={field.value}
onValueChange={(value) => {
field.onChange(value)
onPlanSelected(value)
}}
>
<SelectTrigger className="w-full">
<SelectValue />
</SelectTrigger>
<SelectContent>
{Object.entries(PRICING_TIER_LABELS_ORG).map(([k, v]) => (
<SelectItem key={k} value={k} translate="no">
{v}
</SelectItem>
))}
</SelectContent>
</Select>
</FormControl>
</FormItemLayout>
)}
/>
</Panel.Content>
)}
{selectedPlan === 'PRO' && (
<>
<Panel.Content className="border-b border-panel-border-interior-light dark:border-panel-border-interior-dark">
<FormField
control={form.control}
name="spend_cap"
render={({ field }) => (
<FormItemLayout
label={
<div className="flex space-x-2 text-sm items-center">
<span>Spend Cap</span>
<HelpCircleIcon
size={16}
strokeWidth={1.5}
className="transition opacity-50 cursor-pointer hover:opacity-100"
onClick={() => setShowSpendCapHelperModal(true)}
/>
</div>
}
layout="horizontal"
description={
field.value
? `Usage is limited to the plan's quota.`
: `You pay for overages beyond the plan's quota.`
}
>
<FormControl>
<Switch checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
</FormItemLayout>
)}
/>
</Panel.Content>
<SpendCapModal
visible={showSpendCapHelperModal}
onHide={() => setShowSpendCapHelperModal(false)}
/>
</>
)}
{setupIntent && selectedPlan !== 'FREE' && (
<Panel.Content className="pt-5">
<Elements stripe={stripePromise} options={stripeOptionsPaymentMethod}>
<NewPaymentMethodElement
ref={paymentRef}
email={user.profile?.primary_email}
readOnly={newOrgLoading || paymentConfirmationLoading}
onAddressChange={handleAddressChange}
onAddressIncomplete={handleAddressIncomplete}
onTaxIdChange={handleTaxIdChange}
/>
</Elements>
{!!billingAddress && !creationPreviewInitialized && (
<div className="space-y-2 mt-4">
<ShimmeringLoader />
<ShimmeringLoader className="w-3/4" />
<ShimmeringLoader className="w-1/2" />
</div>
)}
{creationPreviewInitialized && !!billingAddress && (
<div className="mt-4">
<ChargeBreakdown
subtotal={creationPreview.plan_price}
subtotalLabel="Plan price"
total={creationPreview.total}
tax={
creationPreview.tax
? {
amount: creationPreview.tax.tax_amount,
percentage: creationPreview.tax.tax_rate_percentage,
}
: undefined
}
taxStatus={creationPreview.tax_status}
isFetching={creationPreviewIsFetching}
/>
</div>
)}
</Panel.Content>
)}
{hasFreeOrgWithProjects && form.getValues('plan') !== 'FREE' && (
<UpgradeExistingOrganizationCallout />
)}
<Panel.Content>
<CollapsibleCardSection
title="Help improve Supabase"
description="Optional questions to help improve the Supabase experience"
defaultOpen
>
<OnboardingSurveyFields control={form.control} />
</CollapsibleCardSection>
</Panel.Content>
</div>
</Panel>
{stripePromise && paymentIntentSecret && paymentMethod && (
<Elements stripe={stripePromise} options={stripeOptionsConfirm}>
<PaymentConfirmation
paymentIntentSecret={paymentIntentSecret}
onPaymentIntentConfirm={(paymentIntentConfirmation) =>
paymentIntentConfirmed(paymentIntentConfirmation)
}
onLoadingChange={(loading) => setPaymentConfirmationLoading(loading)}
onError={(err) => {
const toastId = toast.error(err.message, { duration: 10_000 })
trackFunnelError(
'org_creation',
{ errorCategory: 'payment', errorReason: 'payment_error' },
'toast',
toastId
)
setNewOrgLoading(false)
resetPaymentMethod()
}}
/>
</Elements>
)}
</form>
</Form>
)
}