Line data Source code
1 : import { onMounted, ref } from 'vue'
2 :
3 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
4 : import { useErrorHandling } from '@/composables/useErrorHandling'
5 : import { useTelemetry } from '@comfyorg/telemetry'
6 : import { reportError } from '@comfyorg/telemetry/reportError'
7 : import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
8 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
9 : import { useCommandStore } from '@/platform/keybindings/commandStore'
10 :
11 : /** Opens support through Comfy.ContactSupport, tracks the click, and reports a failure to open it. */
12 6 : export function useMessageSupport() {
13 6 : const commandStore = useCommandStore()
14 6 : const telemetry = useTelemetry()
15 6 : const { wrapWithErrorHandlingAsync, toastErrorHandler } = useErrorHandling()
16 :
17 6 : const isLoadingSupport = ref(false)
18 :
19 6 : const reportSupportFailure = (error: unknown) => {
20 0 : reportError(error, {
21 : surface: 'billing',
22 : errorType: 'contact_support_failed'
23 : })
24 0 : toastErrorHandler(error)
25 : }
26 :
27 : const handleMessageSupport = wrapWithErrorHandlingAsync(
28 : async () => {
29 0 : isLoadingSupport.value = true
30 0 : telemetry?.trackHelpResourceClicked({
31 : resource_type: 'help_feedback',
32 : is_external: true,
33 : source: 'subscription'
34 : })
35 0 : await commandStore.execute('Comfy.ContactSupport')
36 : },
37 : reportSupportFailure,
38 : () => {
39 0 : isLoadingSupport.value = false
40 : }
41 : )
42 :
43 6 : return { isLoadingSupport, handleMessageSupport }
44 : }
45 :
46 : /**
47 : * Composable for handling subscription panel actions and loading states
48 : */
49 0 : export function useSubscriptionActions() {
50 0 : const { showTopUpCreditsDialog } = useBillingDialogs()
51 0 : const telemetry = useTelemetry()
52 0 : const { fetchBalance, fetchStatus } = useBillingContext()
53 0 : const { isLoadingSupport, handleMessageSupport } = useMessageSupport()
54 :
55 0 : onMounted(() => {
56 0 : void handleRefresh()
57 : })
58 :
59 0 : const handleAddApiCredits = () => {
60 0 : telemetry?.trackAddApiCreditButtonClicked({
61 : source: 'settings_billing_panel'
62 : })
63 0 : void showTopUpCreditsDialog({
64 : source: paymentIntentSourceForAddCreditsClick('settings_billing_panel')
65 : })
66 : }
67 :
68 0 : const handleRefresh = async () => {
69 0 : try {
70 0 : await Promise.all([fetchBalance(), fetchStatus()])
71 : } catch (error) {
72 0 : console.error('[useSubscriptionActions] Error refreshing data:', error)
73 : }
74 : }
75 :
76 0 : const handleLearnMoreClick = () => {
77 0 : window.open('https://docs.comfy.org/get_started/cloud', '_blank')
78 : }
79 :
80 0 : return {
81 : isLoadingSupport,
82 : handleAddApiCredits,
83 : handleMessageSupport,
84 : handleRefresh,
85 : handleLearnMoreClick
86 : }
87 : }
|