Line data Source code
1 : import { computed } from 'vue'
2 : import { useI18n } from 'vue-i18n'
3 :
4 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
5 : import { useFreeTierQuota } from '@/platform/billing/subscription/composables/useFreeTierQuota'
6 : import { isSalesManagedTier } from '@/platform/billing/subscription/constants/tierPricing'
7 : import type { SubscriptionTier } from '@comfyorg/billing-core'
8 : import { resolveSubscriptionTierKey } from '@/platform/billing/components/subscriptionPanelWorkspace.logic'
9 : import { derivePlanRow } from '@comfyorg/account-core/billing'
10 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
11 : import { useScheduledPlanChange } from '@/platform/billing/composables/useScheduledPlanChange'
12 :
13 9 : export function useBillingPlanRow() {
14 9 : const { t } = useI18n()
15 9 : const {
16 : subscription,
17 : subscriptionStatus,
18 : subscriptionState,
19 : billingStatus,
20 : isTeamPlan
21 : } = useBillingContext()
22 9 : const { quotaEnabled, available, maxAvailable } = useFreeTierQuota()
23 9 : const scheduled = useScheduledPlanChange()
24 9 : const { planKind } = useBillingCapabilities()
25 :
26 9 : function planNameFor(tier: SubscriptionTier | null): string | null {
27 9 : if (tier === null || isSalesManagedTier(tier)) return null
28 9 : if (isTeamPlan.value) return t('subscription.teamPlanName')
29 9 : return t(`subscription.tiers.${resolveSubscriptionTierKey(tier)}.name`)
30 : }
31 :
32 9 : const scheduledChange = computed(() => {
33 9 : const change = scheduled.scheduledChange.value
34 9 : const planName = scheduled.planName.value
35 9 : return change && planName
36 : ? { planName, effectiveAt: change.effective_at }
37 : : null
38 : })
39 :
40 9 : const freeRuns = computed(() =>
41 : quotaEnabled.value
42 : ? { remaining: available.value, total: maxAvailable.value }
43 : : null
44 : )
45 :
46 9 : const subscriptionFacts = computed(() => {
47 9 : const current = subscription.value
48 9 : if (!current) {
49 0 : return {
50 : duration: null,
51 : planName: null,
52 : renewalDate: null,
53 : endDate: null
54 : }
55 : }
56 9 : return {
57 : duration: current.duration,
58 : planName: planNameFor(current.tier),
59 : renewalDate: current.renewalDate,
60 : endDate: current.endDate,
61 : endedAt: current.endedAt
62 : }
63 : })
64 :
65 : const state = computed(() =>
66 : derivePlanRow({
67 : ...subscriptionFacts.value,
68 : subscriptionStatus: subscriptionStatus.value,
69 : subscriptionState: subscriptionState.value,
70 : billingStatus: billingStatus.value,
71 : scheduledChange: scheduledChange.value,
72 : freeRuns: freeRuns.value,
73 : planKind: planKind.value
74 : })
75 : )
76 :
77 9 : return { state }
78 : }
|