Line data Source code
1 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
2 : import type { AccountPrecondition } from '@/platform/workflowErrors/errorCatalog/accountPreconditionRouting'
3 : import type { PaymentIntentSource } from '@comfyorg/telemetry/types'
4 : import { useAuthDialogs } from '@/composables/auth/useAuthDialogs'
5 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
6 :
7 : interface AccountPreconditionContext {
8 : /** Node type that triggered the precondition, used as modal context. */
9 : nodeType?: string
10 : source?: PaymentIntentSource
11 : creditsLocked?: boolean
12 : refusalType?: string
13 : runRefused?: boolean
14 : }
15 :
16 1 : function subscriptionDialogOptions(context: AccountPreconditionContext) {
17 1 : return {
18 : reason: context.source ?? 'subscription_required',
19 : ...(context.creditsLocked !== undefined && {
20 : creditsLocked: context.creditsLocked
21 : }),
22 : ...(context.refusalType && { refusalType: context.refusalType }),
23 : ...(context.runRefused && { runRefused: true })
24 : }
25 : }
26 :
27 2 : export function useAccountPreconditionDialog() {
28 2 : const { showApiNodesSignInDialog } = useAuthDialogs()
29 2 : const { showSubscriptionRequiredDialog, showTopUpCreditsDialog } =
30 : useBillingDialogs()
31 :
32 1 : async function open(
33 : precondition: AccountPrecondition,
34 : context: AccountPreconditionContext = {}
35 : ): Promise<boolean> {
36 1 : switch (precondition) {
37 : case 'sign_in':
38 0 : void showApiNodesSignInDialog(
39 : context.nodeType ? [context.nodeType] : []
40 : )
41 0 : return true
42 : case 'subscription':
43 1 : return showSubscriptionRequiredDialog(
44 : subscriptionDialogOptions(context)
45 : )
46 : case 'credits': {
47 0 : const { fetchStatus, fetchBalance } = useBillingContext()
48 0 : void Promise.allSettled([fetchStatus(), fetchBalance()])
49 0 : void showTopUpCreditsDialog({
50 : isInsufficientCredits: true,
51 : ...(context.source && { source: context.source })
52 : })
53 0 : return true
54 : }
55 : }
56 : }
57 :
58 2 : return { open }
59 : }
|