chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:20:55 +08:00
commit d25d482dc2
13754 changed files with 4996608 additions and 0 deletions
@@ -0,0 +1 @@
export { type UpgradeState, useUpgradeState } from './use-upgrade-state'
@@ -0,0 +1,222 @@
'use client'
import { useCallback, useEffect, useRef, useState } from 'react'
import { toast } from '@sim/emcn'
import { isOrgAdminRole } from '@sim/platform-authz/predicates'
import { getErrorMessage } from '@sim/utils/errors'
import { requestJson } from '@/lib/api/client/request'
import { billingSwitchPlanContract } from '@/lib/api/contracts/subscription'
import { useSubscriptionUpgrade } from '@/lib/billing/client/upgrade'
import { CREDIT_TIERS } from '@/lib/billing/constants'
import {
getPlanTierCredits,
isEnterprise,
isFree,
isPaid,
isPro,
isTeam,
} from '@/lib/billing/plan-helpers'
import { hasPaidSubscriptionStatus } from '@/lib/billing/subscriptions/utils'
import { getSubscriptionPermissions } from '@/app/workspace/[workspaceId]/upgrade/subscription-permissions'
import { useSubscriptionData } from '@/hooks/queries/subscription'
const PRO_TIER = CREDIT_TIERS[0]
const MAX_TIER = CREDIT_TIERS[1]
type TargetPlan = 'pro' | 'team'
export interface UpgradeState {
isLoading: boolean
isAnnual: boolean
setIsAnnual: (v: boolean) => void
subscription: {
isFree: boolean
isPro: boolean
isTeam: boolean
isEnterprise: boolean
isPaid: boolean
isOrgScoped: boolean
plan: string
status: string
}
showUpgradePlans: boolean
proTier: { credits: number; dollars: number; name: string }
maxTier: { credits: number; dollars: number; name: string }
isOnPro: boolean
isOnMax: boolean
isOnMaxTier: boolean
wantsIntervalSwitch: boolean
doUpgrade: (targetPlan: 'pro' | 'team', creditTier: number, seats?: number) => Promise<void>
handleSwitchInterval: (interval: 'month' | 'year') => Promise<void>
upgradeOrSwitchToMax: () => Promise<void>
onUpgradeToOtherTier: () => Promise<void>
}
/**
* Plan-selection state hook for the Upgrade page. Surfaces only what the plan
* cards and billing-period toggle need: the resolved tier, upgrade/downgrade/
* interval-switch handlers, and whether to show the upgrade plans at all.
*
* Plan and billing management (payment method, cancellation, invoices, usage
* limits) lives on the Billing settings page, not here.
*/
export function useUpgradeState(): UpgradeState {
const { handleUpgrade } = useSubscriptionUpgrade()
const {
data: subscriptionData,
isLoading,
refetch: refetchSubscription,
} = useSubscriptionData({ includeOrg: true })
const [isAnnual, setIsAnnual] = useState(true)
const hasInitializedInterval = useRef(false)
const subscription = {
isFree: isFree(subscriptionData?.data?.plan),
isPro: isPro(subscriptionData?.data?.plan),
isTeam: isTeam(subscriptionData?.data?.plan),
isEnterprise: isEnterprise(subscriptionData?.data?.plan),
isPaid:
isPaid(subscriptionData?.data?.plan) &&
hasPaidSubscriptionStatus(subscriptionData?.data?.status),
/**
* True when the subscription is attached to an org (regardless of plan
* name). Feeds the permission resolution below.
*/
isOrgScoped: Boolean(subscriptionData?.data?.isOrgScoped),
plan: subscriptionData?.data?.plan || 'free',
status: subscriptionData?.data?.status || 'inactive',
}
const isLegacyPlan = subscription.plan === 'pro' || subscription.plan === 'team'
/**
* Sync the billing-period toggle to a paid subscriber's actual interval once
* subscription data loads. Free/unsubscribed users keep the Annual default
* (the API reports `billingInterval: 'month'` for them, which must not
* override the default).
*/
useEffect(() => {
if (!hasInitializedInterval.current && subscription.isPaid) {
hasInitializedInterval.current = true
setIsAnnual(subscriptionData?.data?.billingInterval === 'year')
}
}, [subscription.isPaid, subscriptionData?.data?.billingInterval])
const userRole = subscriptionData?.data?.organization?.role ?? 'member'
const isTeamAdmin = isOrgAdminRole(userRole)
const permissions = getSubscriptionPermissions(
{
isFree: subscription.isFree,
isPro: subscription.isPro,
isTeam: subscription.isTeam,
isEnterprise: subscription.isEnterprise,
isPaid: subscription.isPaid,
isOrgScoped: subscription.isOrgScoped,
plan: subscription.plan || 'free',
status: subscription.status || 'inactive',
},
{ isTeamAdmin, userRole: userRole || 'member' }
)
const showUpgradePlans = permissions.showUpgradePlans
const doUpgrade = useCallback(
async (targetPlan: TargetPlan, creditTier: number, seats?: number) => {
try {
await handleUpgrade(targetPlan, {
creditTier,
annual: isAnnual,
...(seats ? { seats } : {}),
})
} catch (error) {
toast.error(getErrorMessage(error, 'Unknown error occurred'))
}
},
[handleUpgrade, isAnnual]
)
const currentInterval: 'month' | 'year' =
subscriptionData?.data?.billingInterval === 'year' ? 'year' : 'month'
const handleSwitchInterval = useCallback(
async (interval: 'month' | 'year') => {
if (isLegacyPlan) {
throw new Error(
'Interval switching is not available on legacy plans. Please upgrade first.'
)
}
await requestJson(billingSwitchPlanContract, {
body: { targetPlanName: subscription.plan, interval },
})
await refetchSubscription()
},
[refetchSubscription, subscription.plan, isLegacyPlan]
)
const currentCredits = getPlanTierCredits(subscription.plan)
const hasPaidPlan = isPro(subscription.plan) || isTeam(subscription.plan)
const isLegacyTeam = subscription.plan === 'team'
const isOnKnownTier = currentCredits === PRO_TIER.credits || currentCredits === MAX_TIER.credits
const isOnProTier =
hasPaidPlan &&
!isLegacyTeam &&
(currentCredits === PRO_TIER.credits || (!isOnKnownTier && !subscription.isTeam))
const isOnMaxTier =
hasPaidPlan &&
(currentCredits === MAX_TIER.credits || isLegacyTeam || (!isOnKnownTier && subscription.isTeam))
const wantsIntervalSwitch =
hasPaidPlan && !isLegacyPlan && isAnnual !== (currentInterval === 'year')
const isOnPro = isOnProTier && !wantsIntervalSwitch
const isOnMax = isOnMaxTier && !wantsIntervalSwitch
const upgradeOrSwitchToMax = useCallback(async () => {
const planType = subscription.isTeam ? 'team' : 'pro'
try {
await requestJson(billingSwitchPlanContract, {
body: {
targetPlanName: `${planType}_${MAX_TIER.credits}`,
interval: isAnnual ? 'year' : 'month',
},
})
await refetchSubscription()
} catch (e) {
toast.error(getErrorMessage(e, 'Failed to upgrade'))
}
}, [subscription.isTeam, isAnnual, refetchSubscription])
const onUpgradeToOtherTier = useCallback(async () => {
const onMax =
getPlanTierCredits(subscription.plan) === MAX_TIER.credits || subscription.plan === 'team'
const targetTier = onMax ? PRO_TIER : MAX_TIER
const planType = subscription.isTeam ? 'team' : 'pro'
const targetPlanName = `${planType}_${targetTier.credits}`
try {
await requestJson(billingSwitchPlanContract, {
body: { targetPlanName },
})
await refetchSubscription()
} catch (e) {
toast.error(getErrorMessage(e, 'Failed to switch plan'))
}
}, [subscription.plan, subscription.isTeam, refetchSubscription])
return {
isLoading,
isAnnual,
setIsAnnual,
subscription,
showUpgradePlans,
proTier: PRO_TIER,
maxTier: MAX_TIER,
isOnPro,
isOnMax,
isOnMaxTier,
wantsIntervalSwitch,
doUpgrade,
handleSwitchInterval,
upgradeOrSwitchToMax,
onUpgradeToOtherTier,
}
}