Line data Source code
1 : import { useRouter } from 'vue-router'
2 : import type { LocationQuery } from 'vue-router'
3 :
4 : import { useAssetsUrlLoader } from '@/platform/assets/composables/useAssetsUrlLoader'
5 : import { usePricingTableUrlLoader } from '@/platform/billing/subscription/composables/usePricingTableUrlLoader'
6 : import { useSubscriptionDialog } from '@/platform/billing/subscription/composables/useSubscriptionDialog'
7 : import { useTopUpUrlLoader } from '@/platform/billing/subscription/composables/useTopUpUrlLoader'
8 : import {
9 : BILLING_WEB_RETURN_PARAMS,
10 : STRIPE_RETURN_PARAMS
11 : } from '@/platform/billing/subscription/utils/paymentReturnUrl'
12 : import { isCloud } from '@/platform/distribution/types'
13 : import {
14 : clearPreservedQuery,
15 : getPreservedQueryParam
16 : } from '@/platform/navigation/preservedQueryManager'
17 : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
18 : import { useLocalHandoffUrlLoader } from '@/platform/openOnCloud/composables/useLocalHandoffUrlLoader'
19 : import { LOCAL_HANDOFF_PARAM } from '@/platform/openOnCloud/localHandoffProtocol'
20 : import { useSettingsUrlLoader } from '@/platform/settings/composables/useSettingsUrlLoader'
21 : import { useWorkflowLinkUrlLoader } from '@/platform/workflow/management/composables/useWorkflowLinkUrlLoader'
22 : import { reportError } from '@comfyorg/telemetry/reportError'
23 : import { useCreateWorkspaceUrlLoader } from '@/platform/account/composables/useCreateWorkspaceUrlLoader'
24 : import { useDeploymentUrlLoader } from '@/platform/workspace/composables/useDeploymentUrlLoader'
25 : import { useInviteUrlLoader } from '@/platform/account/composables/useInviteUrlLoader'
26 : import { useWorkspaceUrlLoader } from '@/platform/workspace/composables/useWorkspaceUrlLoader'
27 :
28 : interface UrlActionStep {
29 : /** What the loader does, for the failure log — not shown to the customer. */
30 : readonly failureMessage: string
31 : /** `true` means a reload is already in flight: the loop must stop, since
32 : * `location.reload()` doesn't halt the current script and a later step's
33 : * own URL cleanup could otherwise land on the still-live page. */
34 : readonly run: () => Promise<boolean | void> | boolean | void
35 : }
36 :
37 : type UrlActionLoaders = {
38 : workflowLinkUrlLoader: ReturnType<typeof useWorkflowLinkUrlLoader> | null
39 : deploymentUrlLoader: ReturnType<typeof useDeploymentUrlLoader> | null
40 : workspaceUrlLoader: ReturnType<typeof useWorkspaceUrlLoader> | null
41 : inviteUrlLoader: ReturnType<typeof useInviteUrlLoader> | null
42 : createWorkspaceUrlLoader: ReturnType<
43 : typeof useCreateWorkspaceUrlLoader
44 : > | null
45 : pricingTableUrlLoader: ReturnType<typeof usePricingTableUrlLoader> | null
46 : topUpUrlLoader: ReturnType<typeof useTopUpUrlLoader> | null
47 : settingsUrlLoader: ReturnType<typeof useSettingsUrlLoader> | null
48 : assetsUrlLoader: ReturnType<typeof useAssetsUrlLoader> | null
49 : localHandoffUrlLoader: ReturnType<typeof useLocalHandoffUrlLoader> | null
50 : }
51 :
52 : /**
53 : * Every param a loader below consumes, stripped centrally as a backstop. Most
54 : * loaders fire their own cleanup replace without waiting for it, so with two
55 : * deep links in one URL the later navigation can cancel the earlier one and
56 : * leave its param behind; this sweep removes whatever survived once every
57 : * loader has had its turn. The Stripe ones are already gone from the address
58 : * bar by then, but only through `history.replaceState`, which leaves the
59 : * router's own query holding them — so a replace built from that query would
60 : * hand them back. `workspace` is included too: its own strip catches a
61 : * rejected `router.replace` and returns normally, so a failed strip needs
62 : * this backstop rather than leaving the link live with no retry. A
63 : * successful switch reloads the page and stops the loop before this step
64 : * runs, so it never re-strips a live workspace switch. `deployment` likewise:
65 : * the deployment link keeps both params only across its own switch reload,
66 : * which also stops the loop first. `workflows` is opened at boot, which keeps
67 : * it (and its preserved-query stash) beside a deployment link for the boot
68 : * after the pick's reload; a link that did not reload ends here, stash too.
69 : */
70 2355 : const HANDLED_PARAMS = [
71 : 'workspace',
72 : 'deployment',
73 : 'workflows',
74 : 'invite',
75 : 'create_workspace',
76 : 'pricing',
77 : 'stop',
78 : 'cycle',
79 : 'topup',
80 : 'settings',
81 : 'assets',
82 : LOCAL_HANDOFF_PARAM,
83 : ...STRIPE_RETURN_PARAMS,
84 : ...BILLING_WEB_RETURN_PARAMS
85 : ]
86 :
87 : function deploymentLinkSteps(
88 : loader: UrlActionLoaders['deploymentUrlLoader']
89 : ): UrlActionStep[] {
90 : if (!loader) return []
91 : return [
92 : {
93 : failureMessage: 'Failed to open deployment from URL:',
94 : run: () => loader.loadDeploymentFromUrl()
95 : }
96 : ]
97 : }
98 :
99 : function workflowLinkSteps(
100 : loader: UrlActionLoaders['workflowLinkUrlLoader']
101 : ): UrlActionStep[] {
102 : if (!loader) return []
103 : return [
104 : {
105 : failureMessage: 'Failed to open the linked workflow from URL:',
106 : run: async () => {
107 : await loader.openWorkflowLinkFromUrl(true)
108 : }
109 : }
110 : ]
111 : }
112 :
113 : function sweptWorkflowLinkParams(
114 : loader: UrlActionLoaders['workflowLinkUrlLoader'],
115 : query: LocationQuery
116 : ): string[] {
117 : if (!loader) return []
118 : const stashed = getPreservedQueryParam(
119 : PRESERVED_QUERY_NAMESPACES.WORKFLOW_ID,
120 : 'workflowId'
121 : )
122 : if (stashed !== undefined && loader.isLinkSettled(stashed))
123 : clearPreservedQuery(PRESERVED_QUERY_NAMESPACES.WORKFLOW_ID)
124 : return 'workflowId' in query && loader.isLinkSettled(query.workflowId)
125 : ? ['workflowId']
126 : : []
127 : }
128 :
129 : function sweepStep(
130 : loader: UrlActionLoaders['workflowLinkUrlLoader'],
131 : router: ReturnType<typeof useRouter>
132 : ): UrlActionStep {
133 : return {
134 : failureMessage: 'Failed to clear handled deep-link params from the URL:',
135 : run: async () => {
136 : clearPreservedQuery(PRESERVED_QUERY_NAMESPACES.WORKFLOWS)
137 : const query = { ...router.currentRoute.value.query }
138 : const params = [
139 : ...HANDLED_PARAMS.filter((param) => param in query),
140 : ...sweptWorkflowLinkParams(loader, query)
141 : ]
142 : if (params.length === 0) return
143 : for (const param of params) delete query[param]
144 : await router.replace({ query })
145 : }
146 : }
147 : }
148 :
149 : function deferredToNewerLink(
150 : loader: UrlActionLoaders['workflowLinkUrlLoader'],
151 : steps: UrlActionStep[]
152 : ): UrlActionStep[] {
153 : if (!loader) return steps
154 : return steps.map(({ failureMessage, run }) => ({
155 : failureMessage,
156 : run: () => (loader.hasLinkInFlight() ? undefined : run())
157 : }))
158 : }
159 :
160 : function buildUrlActionSteps(
161 : loaders: UrlActionLoaders,
162 : router: ReturnType<typeof useRouter>
163 : ): UrlActionStep[] {
164 : const steps = deploymentLinkSteps(loaders.deploymentUrlLoader)
165 :
166 : if (loaders.workspaceUrlLoader) {
167 : const loader = loaders.workspaceUrlLoader
168 : steps.push({
169 : failureMessage: 'Failed to load workspace from URL:',
170 : run: () => loader.loadWorkspaceFromUrl()
171 : })
172 : }
173 : steps.push(...workflowLinkSteps(loaders.workflowLinkUrlLoader))
174 :
175 : const laterSteps: UrlActionStep[] = []
176 : if (loaders.inviteUrlLoader) {
177 : const loader = loaders.inviteUrlLoader
178 : laterSteps.push({
179 : failureMessage: 'Failed to load invite from URL:',
180 : run: () => loader.loadInviteFromUrl()
181 : })
182 : }
183 : if (loaders.createWorkspaceUrlLoader) {
184 : const loader = loaders.createWorkspaceUrlLoader
185 : laterSteps.push({
186 : failureMessage: 'Failed to load create workspace from URL:',
187 : run: () => loader.loadCreateWorkspaceFromUrl()
188 : })
189 : }
190 : if (loaders.pricingTableUrlLoader) {
191 : const loader = loaders.pricingTableUrlLoader
192 : laterSteps.push({
193 : failureMessage: 'Failed to load pricing table from URL:',
194 : run: () => loader.loadPricingTableFromUrl()
195 : })
196 : }
197 : if (loaders.topUpUrlLoader) {
198 : const loader = loaders.topUpUrlLoader
199 : laterSteps.push({
200 : failureMessage: 'Failed to load top-up dialog from URL:',
201 : run: () => loader.loadTopUpFromUrl()
202 : })
203 : }
204 : if (loaders.settingsUrlLoader) {
205 : const loader = loaders.settingsUrlLoader
206 : laterSteps.push({
207 : failureMessage: 'Failed to load settings panel from URL:',
208 : run: () => loader.loadSettingsFromUrl()
209 : })
210 : }
211 : if (loaders.assetsUrlLoader) {
212 : const loader = loaders.assetsUrlLoader
213 : laterSteps.push({
214 : failureMessage: 'Failed to open assets panel from URL:',
215 : run: () => loader.loadAssetsFromUrl()
216 : })
217 : }
218 : if (loaders.localHandoffUrlLoader) {
219 : const loader = loaders.localHandoffUrlLoader
220 : laterSteps.push({
221 : failureMessage: 'Failed to open the workflow handed over from local:',
222 : run: () => loader.loadLocalHandoffFromUrl()
223 : })
224 : }
225 : laterSteps.push(sweepStep(loaders.workflowLinkUrlLoader, router))
226 :
227 : return [
228 : ...steps,
229 : ...deferredToNewerLink(loaders.workflowLinkUrlLoader, laterSteps)
230 : ]
231 : }
232 :
233 : /**
234 : * Runs each step in order; one loader's failure never blocks the rest. Stops
235 : * immediately when a step reports a reload already in flight — no further
236 : * step's URL cleanup should run on a page that's about to be torn down.
237 : * Returns whether a reload is in flight, so the caller can skip work that
238 : * would otherwise run against the workspace the app is switching away from.
239 : */
240 2353 : async function runUrlActionSteps(steps: UrlActionStep[]): Promise<boolean> {
241 2353 : for (const step of steps) {
242 0 : try {
243 0 : if (await step.run()) return true
244 : } catch (error) {
245 0 : console.error(`[UrlActionLoaders] ${step.failureMessage}`, error)
246 : }
247 : }
248 2353 : return false
249 : }
250 :
251 : /**
252 : * Reopens a checkout that was interrupted by a redirect payment. Called from
253 : * `onMounted`, not during workspace init, so the first-run and Templates
254 : * overlays are already settled and the recovered dialog is reachable.
255 : * Deliberately not awaited: the resume only settles once the recovered
256 : * operation reaches a terminal status, which for an abandoned checkout can
257 : * take hours, and the boot chain (emit('ready'), the tour, telemetry) must
258 : * not wait on it.
259 : */
260 2353 : function schedulePendingCheckoutRecovery(
261 : subscriptionDialog: ReturnType<typeof useSubscriptionDialog> | null
262 : ) {
263 2353 : if (!subscriptionDialog) return
264 :
265 0 : void (async () => subscriptionDialog.resumePendingPricingFlow())().catch(
266 : (error) => {
267 0 : reportError(error, {
268 : surface: 'platform',
269 : errorType: 'billing_pending_checkout_resume_failure'
270 : })
271 : }
272 : )
273 : }
274 :
275 : /** Calls a composable only on the cloud distribution; `null` off it. */
276 2355 : function useIfCloud<T>(create: () => T): T | null {
277 2355 : return isCloud ? create() : null
278 : }
279 :
280 : /**
281 : * Aggregates the query-param "deep link" loaders the cloud app checks on mount
282 : * (`?deployment`, `?workspace`, `?workflowId`, `?invite`, `?create_workspace`, `?pricing`,
283 : * `?topup`, `?settings`, `?assets`), then recovers an interrupted checkout. The loaders
284 : * are instantiated in setup so their `useRoute`/`useRouter` resolve; call
285 : * `runUrlActionLoaders()` from `onMounted` once the app is ready.
286 : */
287 2355 : export function useUrlActionLoaders() {
288 2355 : const router = useRouter()
289 :
290 : const workflowLinkUrlLoader = useIfCloud(useWorkflowLinkUrlLoader)
291 : const deploymentUrlLoader = useIfCloud(useDeploymentUrlLoader)
292 : const workspaceUrlLoader = useIfCloud(useWorkspaceUrlLoader)
293 : const inviteUrlLoader = useIfCloud(useInviteUrlLoader)
294 : const createWorkspaceUrlLoader = useIfCloud(useCreateWorkspaceUrlLoader)
295 : const pricingTableUrlLoader = useIfCloud(usePricingTableUrlLoader)
296 : const topUpUrlLoader = useIfCloud(useTopUpUrlLoader)
297 : const settingsUrlLoader = useIfCloud(useSettingsUrlLoader)
298 : const assetsUrlLoader = useIfCloud(useAssetsUrlLoader)
299 : const localHandoffUrlLoader = useIfCloud(useLocalHandoffUrlLoader)
300 2355 : const subscriptionDialog = useIfCloud(useSubscriptionDialog)
301 :
302 2355 : const steps = isCloud
303 : ? buildUrlActionSteps(
304 : {
305 : workflowLinkUrlLoader,
306 : deploymentUrlLoader,
307 : workspaceUrlLoader,
308 : inviteUrlLoader,
309 : createWorkspaceUrlLoader,
310 : pricingTableUrlLoader,
311 : topUpUrlLoader,
312 : settingsUrlLoader,
313 : assetsUrlLoader,
314 : localHandoffUrlLoader
315 : },
316 : router
317 : )
318 : : []
319 :
320 2353 : async function runUrlActionLoaders() {
321 : const reloading = await runUrlActionSteps(steps)
322 : // A switch reload is already in flight: activeWorkspaceId and its auth
323 : // context are already stale, so starting recovery here could adopt or
324 : // poll the old workspace's pending checkout. The next boot, once the
325 : // reload lands, gets a clean pass at it.
326 2353 : if (reloading) return
327 2353 : schedulePendingCheckoutRecovery(subscriptionDialog)
328 : }
329 :
330 2355 : return { runUrlActionLoaders }
331 : }
|