Line data Source code
1 : import { computed, watch } from 'vue'
2 :
3 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
4 : import { isCloud } from '@/platform/distribution/types'
5 : import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard'
6 : import { deriveAutoTopUpBanner } from '@comfyorg/account-core/billing'
7 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
8 : import { usePausedBanner } from '@/platform/billing/composables/usePausedBanner'
9 :
10 5 : export function useAutoTopUpBannerKind() {
11 5 : const billing = useBillingContext()
12 5 : const { autoTopUpEnabled, canManageAutoTopUp } = useBillingCapabilities()
13 5 : const { isPaused } = usePausedBanner()
14 :
15 5 : const readEnabled = computed(() => {
16 5 : if (!autoTopUpEnabled.value) return false
17 2 : return canManageAutoTopUp.value
18 : })
19 5 : const { state, ensureRead } = useBillingAutoTopUpCard()
20 5 : watch(
21 : readEnabled,
22 : (enabled) => {
23 5 : if (enabled) ensureRead()
24 : },
25 : { immediate: true }
26 : )
27 :
28 5 : return computed(() => {
29 7 : const read = state.value
30 7 : const loaded = read.kind === 'loaded' ? read.settings : null
31 7 : return deriveAutoTopUpBanner({
32 : enabled: autoTopUpEnabled.value,
33 : canManageAutoTopUp: canManageAutoTopUp.value,
34 : isPaused: isPaused.value,
35 : hasActivePlan: !isCloud || billing.subscription.value?.isActive === true,
36 : status: loaded?.status ?? null,
37 : disabledReason: loaded?.disabled_reason ?? null
38 : })
39 : })
40 : }
|