Line data Source code
1 : import { useToast } from '@/components/ui/toast/toastStore'
2 : import { ref } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
6 : import { useBillingRouting } from '@/platform/billing/composables/useBillingRouting'
7 : import { useTelemetry } from '@comfyorg/telemetry'
8 : import { categorizeBillingApiError } from '@/platform/telemetry/utils/billingFailureCategory'
9 : import { useBillingUI } from '@/platform/billing/composables/useBillingUI'
10 :
11 : /**
12 : * Reactivates a cancelled-but-still-active subscription and surfaces success or
13 : * failure as a toast, tracking the in-flight state for the calling button.
14 : */
15 6 : export function useResubscribe() {
16 6 : const { t } = useI18n()
17 6 : const toast = useToast()
18 6 : const { resubscribe } = useBillingContext()
19 6 : const { shouldUseWorkspaceBilling } = useBillingRouting()
20 6 : const { canReactivatePlan } = useBillingUI()
21 :
22 6 : const isResubscribing = ref(false)
23 :
24 0 : async function handleResubscribe() {
25 0 : if (!canReactivatePlan.value) return
26 :
27 0 : const source = 'settings_billing_panel' as const
28 0 : const startedAt = Date.now()
29 0 : const isWorkspaceResubscribe = shouldUseWorkspaceBilling.value
30 :
31 0 : useTelemetry()?.trackResubscribeClicked({ source })
32 : // Emitted before the awaited call so a failure always has a preceding
33 : // `started` — emitting it only after the call meant a failure produced a
34 : // `failed` with no matching `started`, pushing failed/started over 100%.
35 0 : useTelemetry()?.trackBillingEvent({
36 : operation: 'resubscribe',
37 : stage: 'started',
38 : outcome: 'pending',
39 : source
40 : })
41 0 : isResubscribing.value = true
42 0 : try {
43 0 : await resubscribe({ source })
44 : // Workspace's resubscribe() call is itself the terminal reactivation, so it
45 : // gets an immediate `succeeded` here. Legacy only opens a Stripe checkout
46 : // tab, which isn't terminal — its `succeeded` is emitted later, from
47 : // useSubscription.ts's pending-checkout recovery, once a status poll
48 : // confirms the payment actually went through.
49 0 : if (isWorkspaceResubscribe) {
50 0 : useTelemetry()?.trackBillingEvent({
51 : operation: 'resubscribe',
52 : stage: 'succeeded',
53 : outcome: 'success',
54 : source,
55 : duration_ms: Date.now() - startedAt
56 : })
57 : }
58 0 : toast.success(t('subscription.resubscribeSuccess'), { duration: 5000 })
59 : } catch (error) {
60 0 : const detail =
61 : error instanceof Error && error.message.trim()
62 : ? error.message
63 : : t('subscription.resubscribeFailed')
64 0 : useTelemetry()?.trackBillingEvent({
65 : operation: 'resubscribe',
66 : stage: 'failed',
67 : outcome: 'failure',
68 : source,
69 : failure_category: categorizeBillingApiError(error),
70 : ...(isWorkspaceResubscribe && {
71 : duration_ms: Date.now() - startedAt
72 : })
73 : })
74 0 : toast.error(t('g.error'), { description: detail })
75 : } finally {
76 0 : isResubscribing.value = false
77 : }
78 : }
79 :
80 6 : return { isResubscribing, handleResubscribe }
81 : }
|