Line data Source code
1 : import { computed } from 'vue'
2 : import { useI18n } from 'vue-i18n'
3 :
4 : import type { AddCreditsPlanLine } from '@comfyorg/account-core/billing'
5 : import { topUpPlanCreditLine } from '@/platform/billing/topUpPlanCreditLine'
6 : import type { TopUpPlanCreditLine } from '@/platform/billing/topUpPlanCreditLine'
7 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
8 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
9 : import { useFreeTierQuota } from '@/platform/billing/subscription/composables/useFreeTierQuota'
10 : import { isCloud } from '@/platform/distribution/types'
11 :
12 2356 : export const TOP_UP_PLAN_CREDIT_LINE_KEYS = {
13 : planRequiredFreeRuns: 'credits.topUp.planRequiredFreeRuns',
14 : planRequiredSubscribe: 'credits.topUp.planRequiredSubscribe',
15 : planRequiredResubscribe: 'credits.topUp.planRequiredResubscribe'
16 : } as const
17 :
18 2356 : const ADD_CREDITS_LINES = {
19 : planRequiredFreeRuns: 'free_runs',
20 : planRequiredSubscribe: 'no_plan',
21 : planRequiredResubscribe: 'ended'
22 : } as const satisfies Record<TopUpPlanCreditLine, AddCreditsPlanLine>
23 :
24 : /** Which plan-required line applies on Cloud, or null when none does. */
25 14 : export function usePlanCreditLine() {
26 14 : const { isInitialized, subscriptionState } = useBillingContext()
27 14 : const { canTopUp } = useBillingCapabilities()
28 14 : const { available: freeRunsRemaining } = useFreeTierQuota()
29 :
30 14 : return computed(() =>
31 : topUpPlanCreditLine({
32 : isCloud,
33 : isBillingInitialized: isInitialized.value,
34 : canTopUp: canTopUp.value,
35 : subscriptionState: subscriptionState.value,
36 : freeRunsRemaining: freeRunsRemaining.value
37 : })
38 : )
39 : }
40 :
41 : /** The plan-required Add credits line, or null when it does not apply. */
42 9 : export function useTopUpPlanCreditCopy() {
43 9 : const { t } = useI18n()
44 9 : const line = usePlanCreditLine()
45 9 : return computed(() =>
46 : line.value === null ? null : t(TOP_UP_PLAN_CREDIT_LINE_KEYS[line.value])
47 : )
48 : }
49 :
50 : /** The same line as the Add credits dialog's plan-line fact. */
51 4 : export function useAddCreditsPlanLine() {
52 4 : const line = usePlanCreditLine()
53 4 : return computed(() =>
54 : line.value === null ? undefined : ADD_CREDITS_LINES[line.value]
55 : )
56 : }
|