Files
supabase/apps/studio/components/interfaces/Organization/BillingSettings/Subscription/CancellationFlow.tsx
Pamela Chia c6a3a23dfb feat(studio): track free project limit blocks on downgrade and transfer (#53460)
Studio blocks a downgrade to the Free Plan and a project transfer when a
member would exceed their free project limit, and neither block is
tracked. This adds one event for each so we can count how often people
hit them. No UI change.

**Changed:**
- **Downgrade blocked**: `studio_billing_downgrade_blocked_modal_opened`
fires when confirming a downgrade opens the "members have exceeded their
free project limits" modal. It sits in `CancellationFlow`, so it covers
both the Cancel subscription button and the plan side panel.
- **Transfer blocked**: `project_transfer_preview_failed` fires when the
transfer preview for the selected target organization comes back with
members over the limit, once per preview. A refetch that returns the
same data does not fire it again.

**Note:** the transfer event fires from an effect on the preview data.
The notice appears when that data arrives, so there is no click handler
to attach it to.

## To test
Needs an organization on a paid plan with a running project, where an
owner or admin already has the maximum number of free projects in other
organizations.

Tested on the PR preview:
- [ ] Go to the org billing page, click "Cancel subscription" and
confirm the downgrade, expect the members-exceed-limit modal and one
telemetry request with action
`studio_billing_downgrade_blocked_modal_opened` carrying `currentPlan`
and `membersExceedingLimitCount`
- [ ] Open the plan side panel, pick the Free Plan and confirm, expect
the same modal and one more
`studio_billing_downgrade_blocked_modal_opened` request
- [ ] In project settings > General, click "Transfer project" and select
a Free Plan target organization, expect the "Project cannot be
transferred" notice listing members and one telemetry request with
action `project_transfer_preview_failed` carrying `sourcePlan`,
`targetPlan` and `membersExceedingLimitCount`
- [ ] Edge case: with the notice showing, switch to another browser tab
and back, expect no additional `project_transfer_preview_failed` request

## Linear
- fixes GROWTH-1510
2026-11-17 16:25:49 +09:00

177 lines
5.6 KiB
TypeScript

import { useParams } from 'common'
import { ComponentProps, useEffect, useMemo, useState } from 'react'
import { DowngradeModal } from './DowngradeModal'
import { ExitSurveyModal } from './ExitSurveyModal'
import { MembersExceedLimitModal } from './MembersExceedLimitModal'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { useFreeProjectLimitCheckQuery } from '@/data/organizations/free-project-limit-check-query'
import { useOrgProjectsInfiniteQuery } from '@/data/projects/org-projects-infinite-query'
import { useOrgSubscriptionQuery } from '@/data/subscriptions/org-subscription-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { MANAGED_BY } from '@/lib/constants'
import { useTrack } from '@/lib/telemetry/track'
type CancellationFlowProps = {
onDowngrade?: () => void
onCancel?: () => void
visible: boolean
}
type CancellationFlowState =
| 'closed'
| 'show-downgrade-modal'
| 'show-downgrade-error'
| 'show-exit-survey'
export const CancellationFlow = (props: CancellationFlowProps) => {
const { slug } = useParams()
const track = useTrack()
const [flowState, setFlowState] = useState<CancellationFlowState>(
props.visible ? 'show-downgrade-modal' : 'closed'
)
useEffect(() => {
setFlowState(props.visible ? 'show-downgrade-modal' : 'closed')
}, [props.visible])
const { data: membersExceededLimit, isLoading: projectLimitQueryLoading } =
useFreeProjectLimitCheckQuery({ slug }, { enabled: flowState !== 'closed' })
const { data: orgProjectsData, isLoading: orgProjectsQueryLoading } = useOrgProjectsInfiniteQuery(
{ slug },
{ enabled: flowState !== 'closed' }
)
const { data: subscription, isLoading: orgSubscriptionQueryLoading } = useOrgSubscriptionQuery({
orgSlug: slug,
})
const isLoading =
projectLimitQueryLoading || orgProjectsQueryLoading || orgSubscriptionQueryLoading
const orgProjects =
useMemo(
() => orgProjectsData?.pages.flatMap((page) => page.projects),
[orgProjectsData?.pages]
) || []
// [Joshen] Note that orgProjects is paginated so there's a chance this may omit certain projects
// Although I don't foresee this affecting a majority of users. Ideally perhaps we could return
// this data from the organization query
const hasRunningProjects =
orgProjects.filter((it) => it.status !== 'INACTIVE' && it.status !== 'GOING_DOWN').length > 0
const hasMembersExceedingFreeTierLimit =
(membersExceededLimit || []).length > 0 && hasRunningProjects
const onConfirmDowngrade = () => {
if (hasMembersExceedingFreeTierLimit) {
track('studio_billing_downgrade_blocked_modal_opened', {
currentPlan: subscription?.plan.id || 'free',
membersExceedingLimitCount: (membersExceededLimit || []).length,
})
setFlowState('show-downgrade-error')
} else {
setFlowState('show-exit-survey')
}
}
const onCancelFlow = () => {
setFlowState('closed')
props.onCancel?.()
}
return (
<>
<DowngradeModal
visible={flowState === 'show-downgrade-modal'}
subscription={subscription}
onClose={onCancelFlow}
confirmDisabled={isLoading}
onConfirm={onConfirmDowngrade}
projects={orgProjects}
/>
<MembersExceedLimitModal
visible={flowState === 'show-downgrade-error'}
onClose={onCancelFlow}
/>
<ExitSurveyModal
visible={flowState === 'show-exit-survey'}
projects={orgProjects}
onClose={(success?: boolean) => {
if (success) {
setFlowState('closed')
props.onDowngrade?.()
} else {
onCancelFlow()
}
}}
/>
</>
)
}
type InitiateCancellationFlowButtonProps = Pick<
ComponentProps<typeof ButtonTooltip>,
'children' | 'variant'
>
export const InitiateCancellationFlowButton = (props: InitiateCancellationFlowButtonProps) => {
const { slug } = useParams()
const track = useTrack()
const { data: selectedOrganization } = useSelectedOrganizationQuery()
const isAwsManaged = selectedOrganization?.managed_by === MANAGED_BY.AWS_MARKETPLACE
const { can: canUpdateSubscription } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.ORGANIZATION.BILLING_WRITE
)
const { data: subscription } = useOrgSubscriptionQuery({
orgSlug: slug,
})
const isDowngradeablePlan = !subscription || ['pro', 'team'].includes(subscription.plan.id)
const [visible, setVisible] = useState(false)
const tooltipText = [
[isAwsManaged, 'You cannot change the plan for an organization managed by AWS Marketplace.'],
[!isDowngradeablePlan, 'Reach out to us via support to update your plan.'],
[!canUpdateSubscription, "You need additional permissions to change your organization's plan."],
].find(([cond]) => cond)?.[1]
return (
<>
<ButtonTooltip
variant={props.variant}
disabled={!isDowngradeablePlan || !canUpdateSubscription || isAwsManaged}
tooltip={{
content: {
side: 'bottom',
text: tooltipText,
},
}}
onClick={() => {
track('studio_billing_cancel_subscription_clicked', {
currentPlan: subscription?.plan.id || 'free',
})
setVisible(true)
}}
>
{props.children ?? 'Cancel subscription'}
</ButtonTooltip>
<CancellationFlow
onCancel={() => setVisible(false)}
onDowngrade={() => setVisible(false)}
visible={visible}
/>
</>
)
}