Line data Source code
1 : import type { PlatformHandoffDestination } from '@comfyorg/account-core/billing'
2 : import { computed, ref, toValue } from 'vue'
3 : import type { MaybeRefOrGetter } from 'vue'
4 :
5 : import { useTelemetry } from '@comfyorg/telemetry'
6 : import type { PlatformHandoffView } from '@/platform/billing/localBillingHandoff'
7 : import { openPlatformBillingHandoff } from '@/platform/billing/localBillingHandoff'
8 : import { opensLocalBillingHandoff } from '@/platform/billing/localBillingWebEnabled'
9 : import { createJourneyId } from '@/platform/billing/utils/checkoutJourney'
10 : import { platformLink } from '@/platform/workspace/utils/platformLink'
11 :
12 : /**
13 : * A platform link that carries one journey id and reports the click with it.
14 : * Unavailable where billing opens through a local handoff; only `usePlatformBillingLink` opens through one.
15 : */
16 37 : export function usePlatformHandoff(
17 : path: MaybeRefOrGetter<`/${string}`>,
18 : destination: MaybeRefOrGetter<PlatformHandoffDestination>
19 : ) {
20 37 : const correlationId = ref(createJourneyId())
21 37 : const href = computed(() => platformLink(toValue(path), correlationId.value))
22 37 : const available = computed(() => !opensLocalBillingHandoff())
23 :
24 3 : function track() {
25 3 : useTelemetry()?.trackBillingEvent({
26 : operation: 'platform_handoff',
27 : stage: 'opened',
28 : outcome: 'pending',
29 : correlation_id: correlationId.value,
30 : ...toValue(destination)
31 : })
32 3 : setTimeout(() => {
33 3 : correlationId.value = createJourneyId()
34 : })
35 : }
36 :
37 37 : return { href, track, available }
38 : }
39 :
40 : /** Reports a Platform billing link's click and, where billing opens through a local handoff, opens Platform billing, or the page `view` names, through one. */
41 3 : export function openPlatformBilling(
42 : track: () => void,
43 : view?: PlatformHandoffView
44 : ): void {
45 3 : track()
46 3 : if (opensLocalBillingHandoff()) void openPlatformBillingHandoff(view)
47 : }
48 :
49 : /**
50 : * A link to Platform's billing page, or the page `view` names: `button` is the
51 : * props for a design-system `Button`, and `open` runs on every click.
52 : */
53 28 : export function usePlatformBillingLink(
54 : path: MaybeRefOrGetter<`/${string}`>,
55 : destination: MaybeRefOrGetter<PlatformHandoffDestination>,
56 : view?: PlatformHandoffView
57 : ) {
58 28 : const { href: platformHref, track } = usePlatformHandoff(path, destination)
59 28 : const href = computed(() =>
60 : opensLocalBillingHandoff() ? undefined : platformHref.value
61 : )
62 : const button = computed(() =>
63 : href.value
64 : ? { as: 'a', href: href.value, target: '_blank', rel: 'noopener' }
65 : : { as: 'button' }
66 : )
67 28 : return { href, button, open: () => openPlatformBilling(track, view) }
68 : }
69 :
70 : /** A link to Platform's Plans page, shaped as `usePlatformBillingLink`'s; through a local handoff it opens Plans signed in as the local account. */
71 12 : export function usePlatformPlansLink() {
72 12 : return usePlatformBillingLink(
73 : '/profile/billing/plans',
74 : { target: 'plans' },
75 : 'plans'
76 : )
77 : }
|