Line data Source code
1 : import type { BillingOpStatusResponse } from '@comfyorg/ingest-types'
2 : import { useDocumentVisibility } from '@vueuse/core'
3 : import { computed, shallowRef, watch } from 'vue'
4 :
5 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
6 : import { isCloud } from '@/platform/distribution/types'
7 : import { reportError } from '@comfyorg/telemetry/reportError'
8 : import { billingApi } from '@/platform/billing/api/billingApi'
9 : import { planCheckoutBannerFor } from '@comfyorg/account-core/billing'
10 : import type { PlanCheckoutBanner } from '@comfyorg/account-core/billing'
11 : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
12 :
13 5 : export function usePlanCheckoutBanner() {
14 5 : const { flags } = useFeatureFlags()
15 5 : const workspaceStore = useWorkspaceIdentity()
16 5 : const operation = shallowRef<BillingOpStatusResponse | null>(null)
17 5 : const dismissed = shallowRef<ReadonlySet<string>>(new Set())
18 5 : const shown = computed(() => isCloud && flags.unifiedBillingSettingsEnabled)
19 5 : let refreshGeneration = 0
20 :
21 5 : const banner = computed<PlanCheckoutBanner | null>(() => {
22 5 : if (!shown.value || !operation.value) return null
23 0 : const next = planCheckoutBannerFor(operation.value)
24 5 : if (!next || dismissed.value.has(`${next.opId}:${next.kind}`)) return null
25 0 : return next
26 : })
27 :
28 0 : function currentRefreshIs(refreshId: number, launchedGeneration: number) {
29 0 : return (
30 : refreshId === refreshGeneration &&
31 : workspaceStore.workspaceTransitionGeneration === launchedGeneration
32 : )
33 : }
34 :
35 5 : async function refresh() {
36 5 : const launchedGeneration = workspaceStore.workspaceTransitionGeneration
37 5 : const refreshId = ++refreshGeneration
38 5 : if (!shown.value) {
39 5 : operation.value = null
40 5 : return
41 : }
42 5 : try {
43 0 : const status = await billingApi.getBillingStatus()
44 0 : if (!currentRefreshIs(refreshId, launchedGeneration)) return
45 0 : const opId = status.latest_subscription_op_id
46 5 : const nextOperation = opId
47 : ? await billingApi.getBillingOpStatus(opId)
48 : : null
49 5 : if (!currentRefreshIs(refreshId, launchedGeneration)) return
50 0 : operation.value = nextOperation
51 : } catch (error) {
52 0 : if (!currentRefreshIs(refreshId, launchedGeneration)) return
53 0 : reportError(error, {
54 : errorType: 'plan_checkout_banner_read_failed',
55 : surface: 'billing'
56 : })
57 : }
58 : }
59 :
60 0 : function dismiss() {
61 0 : if (!banner.value) return
62 0 : dismissed.value = new Set([
63 : ...dismissed.value,
64 : `${banner.value.opId}:${banner.value.kind}`
65 : ])
66 : }
67 :
68 5 : void refresh()
69 5 : watch(
70 : () => workspaceStore.workspaceTransitionGeneration,
71 : () => {
72 0 : refreshGeneration++
73 0 : operation.value = null
74 0 : dismissed.value = new Set()
75 0 : void refresh()
76 : }
77 : )
78 5 : watch(useDocumentVisibility(), (visibility) => {
79 0 : if (visibility === 'visible') void refresh()
80 : })
81 5 : watch(shown, () => {
82 0 : void refresh()
83 : })
84 :
85 5 : return { banner, dismiss }
86 : }
|