Line data Source code
1 : <template>
2 19 : <div
3 : ref="root"
4 : :class="
5 : cn(
6 : 'flex max-h-[calc(100dvh-2rem)] min-h-0 w-[min(400px,95vw)] flex-col rounded-2xl border bg-background text-sm text-foreground shadow-md',
7 : ui.root
8 : )
9 : "
10 : :style="{ transform: `translateY(${growth / 2}px)` }"
11 : data-testid="add-credits-dialog"
12 : :data-step="state.step"
13 : >
14 : <div
15 : :class="
16 : cn('flex shrink-0 items-center justify-between px-4 pt-4', ui.header)
17 : "
18 : >
19 : <component
20 : :is="titleAs"
21 : :class="cn('m-0 text-base font-normal', ui.title)"
22 : >
23 : {{ title }}
24 : </component>
25 : <button
26 : type="button"
27 : :class="
28 : cn(
29 : buttonVariants({ variant: 'muted-textonly', size: 'icon' }),
30 : ui.close
31 : )
32 : "
33 : :aria-label="translate('close')"
34 : @click="emit('close')"
35 : >
36 : <i class="icon-[lucide--x]" aria-hidden="true" />
37 : </button>
38 : </div>
39 :
40 : <AddCreditsBlockedStep
41 : v-if="blockedUrl !== null"
42 : :checkout-url="blockedUrl"
43 : :reissues="
44 : state.step === 'checkout_blocked' && state.checkoutUrl === undefined
45 : "
46 : :can-reissue="canTopUp"
47 : :translate
48 : :ui
49 : @follow="dialog.followBlockedLink"
50 : @reissue="dialog.reopenCheckout"
51 : />
52 : <AddCreditsAmountStep
53 : v-else-if="dialog.amountView.value"
54 : :view="dialog.amountView.value"
55 : :quote="dialog.quote.value"
56 : :bounds
57 : :negative-balance-credits
58 : :plan-line
59 : :support-url
60 : :translate
61 : :format
62 : :ui
63 : @preset="dialog.pickPreset"
64 : @type="dialog.typeAmount"
65 : @continue="dialog.continueToCheckout"
66 : >
67 : <template #description><slot name="description" /></template>
68 : <template #autoTopUp>
69 : <AddCreditsAutoTopUpCard
70 : v-if="autoTopUpOffer.kind === 'checkbox' && billingLink"
71 : v-model:checked="autoTopUpFields.checked.value"
72 : :rules="autoTopUpOffer.rules"
73 : :default-card="autoTopUpOffer.defaultCard"
74 : :input="autoTopUpFields.input.value"
75 : :values="autoTopUpFields.values.value"
76 : :errors="autoTopUpFields.errors.value"
77 : :billing-url="billingLink"
78 : :locked="dialog.amountView.value?.locked ?? false"
79 : :translate
80 : :format
81 : :ui
82 : @edit="autoTopUpFields.edit"
83 : @open-billing="openBilling"
84 : />
85 : <p
86 : v-else-if="autoTopUpOffer.kind === 'on' && billingLink"
87 : :class="cn('m-0 text-muted-foreground', ui.line)"
88 : data-testid="add-credits-auto-top-up-is-on"
89 : >
90 : {{ translate('autoTopUp.isOn') }}
91 : <AddCreditsBillingLink
92 : :to="billingLink"
93 : rel="noopener noreferrer"
94 : inline
95 : @click="openBilling"
96 : >{{ translate('autoTopUp.manage')
97 : }}<i
98 : class="ml-0.5 icon-[lucide--arrow-up-right] size-3"
99 : aria-hidden="true"
100 : /></AddCreditsBillingLink>
101 : </p>
102 : </template>
103 : <template #notice="scope"><slot name="notice" v-bind="scope" /></template>
104 : <template #extra><slot name="extra" /></template>
105 : </AddCreditsAmountStep>
106 : <AddCreditsOpenedStep
107 : v-else-if="state.step === 'checkout_opened'"
108 : :amount-cents="state.amountCents"
109 : :credits="openedCredits"
110 : :refusal="state.refusal"
111 : :can-reopen="canTopUp && !addCreditsRefusalBlocks(state.refusal)"
112 : :support-url
113 : :translate
114 : :format
115 : :ui
116 : @reopen="dialog.reopenCheckout"
117 : @close="emit('close')"
118 : />
119 : <AddCreditsAutoTopUpResultStep
120 : v-else-if="state.step === 'auto_top_up_on'"
121 : :settings="state.settings"
122 : :link-url="billingLink"
123 : :translate
124 : :format
125 : :ui
126 : @close="emit('close')"
127 : @open-billing="openBilling"
128 : />
129 : <AddCreditsAutoTopUpResultStep
130 : v-else-if="state.step === 'auto_top_up_failed'"
131 : :link-url="linkTo(failedSetupUrl)"
132 : :translate
133 : :format
134 : :ui
135 : @close="emit('close')"
136 : @open-billing="openBilling"
137 : />
138 : <AddCreditsAutoTopUpOfferStep
139 : v-else-if="offerSetupUrl"
140 : :setup-url="linkTo(offerSetupUrl)"
141 : :translate
142 : :ui
143 : @set-up="setUpAutoTopUp"
144 : @close="declineAutoTopUp"
145 : />
146 : </div>
147 : </template>
148 :
149 : <script setup lang="ts">
150 : import { computed, ref, useTemplateRef, watch } from 'vue'
151 : import type { Component } from 'vue'
152 : import { useResizeObserver, useWindowSize } from '@vueuse/core'
153 :
154 : import type {
155 : AddCreditsAutoTopUpOffer,
156 : AddCreditsBounds,
157 : AddCreditsPendingTopup,
158 : AddCreditsPlanLine,
159 : AddCreditsTranslate,
160 : CapabilityDenialReason
161 : } from '@comfyorg/account-core/billing'
162 : import {
163 : ADD_CREDITS_FALLBACK_BOUNDS,
164 : addCreditsRefusalBlocks,
165 : addCreditsRefusalForDenial,
166 : initialAddCreditsAmount,
167 : translateAddCreditsDefault
168 : } from '@comfyorg/account-core/billing'
169 : import { buttonVariants } from '@comfyorg/design-system/button.variants'
170 : import { cn } from '@comfyorg/tailwind-utils'
171 :
172 : import AddCreditsAmountStep from './AddCreditsAmountStep.vue'
173 : import AddCreditsAutoTopUpCard from './AddCreditsAutoTopUpCard.vue'
174 : import AddCreditsAutoTopUpResultStep from './AddCreditsAutoTopUpResultStep.vue'
175 : import AddCreditsAutoTopUpOfferStep from './AddCreditsAutoTopUpOfferStep.vue'
176 : import AddCreditsBillingLink from './AddCreditsBillingLink.vue'
177 : import AddCreditsBlockedStep from './AddCreditsBlockedStep.vue'
178 : import AddCreditsOpenedStep from './AddCreditsOpenedStep.vue'
179 : import type {
180 : AddCreditsBillingLinkTarget,
181 : AddCreditsUi
182 : } from './addCreditsPresentation'
183 : import { addCreditsFormat } from './addCreditsPresentation'
184 : import type { AddCreditsPorts } from './useAddCreditsDialog'
185 : import { useAddCreditsAutoTopUp } from './useAddCreditsAutoTopUp'
186 : import { useAddCreditsDialog } from './useAddCreditsDialog'
187 :
188 : const {
189 : ports,
190 : canTopUp,
191 : topUpDenial,
192 : minTopupCents,
193 : maxTopupCents,
194 : suggestedTopupCents,
195 : negativeBalanceCredits,
196 : pendingTopup,
197 : planLine,
198 : autoTopUp = { kind: 'hidden' },
199 : billingUrl,
200 : autoTopUpSetupHidden = false,
201 : hostOpensBilling = false,
202 : opensIn = 'tab',
203 : supportUrl,
204 : locale,
205 : translate = translateAddCreditsDefault,
206 : titleAs = 'h2',
207 : ui = {}
208 : } = defineProps<{
209 : ports: AddCreditsPorts
210 : canTopUp: boolean
211 : topUpDenial?: CapabilityDenialReason
212 : minTopupCents?: number
213 : maxTopupCents?: number
214 : suggestedTopupCents?: number | null
215 : negativeBalanceCredits?: number
216 : pendingTopup?: AddCreditsPendingTopup
217 : planLine?: AddCreditsPlanLine
218 : autoTopUp?: AddCreditsAutoTopUpOffer
219 : billingUrl?: string
220 : /** Withdraws a setup link the dialog already holds, e.g. once Platform links stop being offered. */
221 : autoTopUpSetupHidden?: boolean
222 : /** Renders every billing link as a button, with no URL in the page, for a host that opens billing itself on `openBilling` and `setUpAutoTopUp`. */
223 : hostOpensBilling?: boolean
224 : opensIn?: 'tab' | 'browser'
225 : supportUrl: string
226 : locale: string
227 : translate?: AddCreditsTranslate
228 : titleAs?: string | Component
229 : ui?: AddCreditsUi
230 : }>()
231 :
232 4 : const emit = defineEmits<{
233 : close: []
234 : setUpAutoTopUp: []
235 : declineAutoTopUp: []
236 : openBilling: []
237 : }>()
238 :
239 0 : function linkTo<Url extends string | undefined>(
240 : url: Url
241 : ): Url | AddCreditsBillingLinkTarget {
242 0 : return url && hostOpensBilling ? true : url
243 : }
244 :
245 4 : const billingLink = computed(() => linkTo(billingUrl))
246 :
247 0 : function openBilling() {
248 0 : if (hostOpensBilling) emit('openBilling')
249 : }
250 :
251 0 : function setUpAutoTopUp() {
252 0 : emit('setUpAutoTopUp')
253 0 : emit('close')
254 : }
255 :
256 0 : function declineAutoTopUp() {
257 0 : emit('declineAutoTopUp')
258 0 : emit('close')
259 : }
260 :
261 4 : const bounds = computed<AddCreditsBounds>(() => ({
262 : minCents: minTopupCents ?? ADD_CREDITS_FALLBACK_BOUNDS.minCents,
263 : maxCents: maxTopupCents ?? ADD_CREDITS_FALLBACK_BOUNDS.maxCents
264 : }))
265 :
266 4 : const autoTopUpOffer = computed<AddCreditsAutoTopUpOffer>(() => {
267 8 : if (!billingUrl) return { kind: 'hidden' }
268 8 : return autoTopUp.kind === 'checkbox' && !ports.activatesAutoTopUp
269 : ? { kind: 'hidden' }
270 : : autoTopUp
271 : })
272 :
273 4 : const autoTopUpFields = useAddCreditsAutoTopUp(
274 : autoTopUpOffer,
275 : () => dialog.pickedCents.value
276 : )
277 :
278 4 : const dialog = useAddCreditsDialog({
279 : ports,
280 : initialAmount: initialAddCreditsAmount(suggestedTopupCents),
281 : autoTopUp: autoTopUpFields.request,
282 : facts: () => ({
283 : bounds: bounds.value,
284 : canTopUp,
285 : canTopUpRefusal: addCreditsRefusalForDenial(topUpDenial),
286 : ...(pendingTopup === undefined ? {} : { pendingTopup })
287 : }),
288 : onLanded: () => emit('close')
289 : })
290 4 : const state = dialog.state
291 :
292 4 : const failedSetupUrl = computed(() =>
293 : state.value.step === 'auto_top_up_failed' && !autoTopUpSetupHidden
294 : ? state.value.setupUrl
295 : : undefined
296 : )
297 4 : const offerSetupUrl = computed(() =>
298 : state.value.step === 'auto_top_up_offer' && !autoTopUpSetupHidden
299 : ? state.value.setupUrl
300 : : undefined
301 : )
302 :
303 4 : watch(
304 : () => state.value.step === 'auto_top_up_offer' && autoTopUpSetupHidden,
305 : (withdrawn) => {
306 0 : if (withdrawn) emit('close')
307 : }
308 : )
309 :
310 4 : const format = computed(() => addCreditsFormat(locale))
311 :
312 : const VIEWPORT_GUTTER_PX = 16
313 4 : const root = useTemplateRef('root')
314 4 : const defaultHeight = ref<number>()
315 4 : const height = ref(0)
316 4 : const { height: viewportHeight } = useWindowSize()
317 4 : useResizeObserver(root, ([entry]) => {
318 6 : height.value = entry.borderBoxSize[0]?.blockSize ?? entry.contentRect.height
319 6 : defaultHeight.value ??= height.value
320 : })
321 4 : const growth = computed(() => {
322 10 : if (defaultHeight.value === undefined) return 0
323 6 : const room = viewportHeight.value - 2 * VIEWPORT_GUTTER_PX - height.value
324 6 : return Math.max(0, Math.min(height.value - defaultHeight.value, room))
325 : })
326 :
327 4 : const blockedUrl = computed(() => {
328 9 : const current = state.value
329 9 : if (current.step === 'checkout_blocked') return current.checkoutUrl
330 9 : return current.step === 'opening' && !current.tabReserved ? undefined : null
331 : })
332 :
333 4 : const title = computed(() => {
334 9 : if (state.value.step === 'auto_top_up_on') {
335 0 : return translate('autoTopUpOn.title')
336 : }
337 9 : if (state.value.step === 'auto_top_up_failed') {
338 0 : return translate('autoTopUpFailed.title')
339 : }
340 9 : if (state.value.step === 'checkout_opened') {
341 1 : return translate(
342 : opensIn === 'browser' ? 'opened.titleBrowser' : 'opened.title'
343 : )
344 : }
345 9 : return translate(blockedUrl.value === null ? 'title' : 'blocked.title')
346 : })
347 :
348 4 : const openedCredits = computed(() => {
349 1 : const quote = dialog.quote.value
350 1 : return quote?.status === 'ok' ? format.value.count(quote.credits) : '—'
351 : })
352 : </script>
|