Line data Source code
1 : import { useRoute, useRouter } from 'vue-router'
2 :
3 : import {
4 : clearPreservedQuery,
5 : hydratePreservedQuery,
6 : mergePreservedQueryIntoQuery
7 : } from '@/platform/navigation/preservedQueryManager'
8 : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
9 : import { useTelemetry } from '@comfyorg/telemetry'
10 : import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
11 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
12 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
13 :
14 2355 : const NAMESPACE = PRESERVED_QUERY_NAMESPACES.TOPUP
15 :
16 : /**
17 : * Opens the credit top-up dialog from a `?topup=1` deep link, to send existing
18 : * paid users straight to buying more credits (lifecycle emails).
19 : *
20 : * Gated to users who can top up; an ineligible user is a silent no-op with the
21 : * param stripped. Survives the login redirect via the preserved-query system,
22 : * like the pricing URL loader.
23 : */
24 : export function useTopUpUrlLoader() {
25 : const route = useRoute()
26 : const router = useRouter()
27 : const { showTopUpCreditsDialog } = useBillingDialogs()
28 : const { canTopUp, initialize } = useBillingCapabilities()
29 : const telemetry = useTelemetry()
30 :
31 : /** Reads `?topup=`, strips it, and opens the dialog when the gate allows. */
32 : async function loadTopUpFromUrl() {
33 : hydratePreservedQuery(NAMESPACE)
34 : const query =
35 : mergePreservedQueryIntoQuery(NAMESPACE, route.query) ?? route.query
36 : const param = query.topup
37 : if (param === undefined) return
38 :
39 : const shouldOpen = typeof param === 'string' && param.length > 0
40 : if (shouldOpen) await initialize()
41 :
42 : // Strip any present topup param (even ineligible or malformed values) and
43 : // write the clean URL in a single replace once capability loading settles.
44 : const cleanQuery = { ...query }
45 : delete cleanQuery.topup
46 : router.replace({ query: cleanQuery }).catch((error) => {
47 : console.warn('[useTopUpUrlLoader] Failed to clean URL params:', error)
48 : })
49 : clearPreservedQuery(NAMESPACE)
50 :
51 : // Only a non-empty string value opens the dialog; an empty/array param
52 : // just gets stripped above.
53 : if (!shouldOpen) return
54 :
55 : if (!canTopUp.value) return
56 :
57 : telemetry?.trackAddApiCreditButtonClicked({ source: 'deep_link' })
58 :
59 : void showTopUpCreditsDialog({
60 : source: paymentIntentSourceForAddCreditsClick('deep_link')
61 : })
62 : }
63 :
64 : return {
65 : loadTopUpFromUrl
66 : }
67 : }
|