Line data Source code
1 : import { useTelemetry } from '@comfyorg/telemetry'
2 : import { effectScope } from 'vue'
3 :
4 : import { refreshBilling } from '@/platform/billing/billingRefresh'
5 : import { openBillingUrl } from '@/platform/billing/billingTab'
6 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
7 : import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn'
8 : import { createJourneyId } from '@/platform/billing/utils/checkoutJourney'
9 : import { platformLink } from '@/platform/workspace/utils/platformLink'
10 :
11 2356 : let stopReturnRefresh: (() => void) | null = null
12 :
13 0 : async function refreshAfterPlans(): Promise<void> {
14 0 : const scope = effectScope(true)
15 0 : const capabilities = scope.run(useBillingCapabilities)
16 0 : await Promise.allSettled([refreshBilling('account'), capabilities?.refresh()])
17 0 : scope.stop()
18 : }
19 :
20 0 : function trackHandoff(correlationId: string): void {
21 0 : useTelemetry()?.trackBillingEvent({
22 : operation: 'platform_handoff',
23 : stage: 'opened',
24 : outcome: 'pending',
25 : correlation_id: correlationId,
26 : target: 'plans'
27 : })
28 : }
29 :
30 0 : function refreshOnReturn(): void {
31 0 : stopReturnRefresh?.()
32 0 : stopReturnRefresh = registerRefreshOnReturn(refreshAfterPlans)
33 : }
34 :
35 : /** The platform Plans page for the active workspace, for a link the user follows, and what to run when they follow it. */
36 0 : export function platformPlansLink(): { href: string; onFollow: () => void } {
37 0 : const correlationId = createJourneyId()
38 0 : return {
39 : href: platformLink('/profile/billing/plans', correlationId),
40 : onFollow: () => {
41 0 : trackHandoff(correlationId)
42 0 : refreshOnReturn()
43 : }
44 : }
45 : }
46 :
47 : /** Opens the platform Plans page for the active workspace in a new tab, as Manage does, and re-reads billing on return; `false` when the browser blocked it. */
48 0 : export function openPlatformPlans(): boolean {
49 0 : const { href, onFollow } = platformPlansLink()
50 0 : if (!openBillingUrl(href)) return false
51 0 : onFollow()
52 0 : return true
53 : }
54 :
55 : /** Leaves the app for the platform Plans page of the active workspace in this tab, and re-reads billing if a back navigation restores this page. */
56 0 : export function navigateToPlatformPlans(): void {
57 0 : const correlationId = createJourneyId()
58 0 : trackHandoff(correlationId)
59 0 : refreshOnReturn()
60 0 : window.location.assign(platformLink('/profile/billing/plans', correlationId))
61 : }
|