LCOV - code coverage report
Current view: top level - src/platform/workspace/composables - useDeploymentUrlLoader.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 100.0 % 3 3
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { storeToRefs } from 'pinia'
       2              : import { useI18n } from 'vue-i18n'
       3              : import type { LocationQueryRaw } from 'vue-router'
       4              : import { useRoute, useRouter } from 'vue-router'
       5              : 
       6              : import { useToast } from '@/components/ui/toast/toastStore'
       7              : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
       8              : import {
       9              :   clearPreservedQuery,
      10              :   hydratePreservedQuery,
      11              :   mergePreservedQueryIntoQuery
      12              : } from '@/platform/navigation/preservedQueryManager'
      13              : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
      14              : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
      15              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      16              : import { WORKSPACE_STORAGE_KEYS } from '@/platform/workspace/workspaceConstants'
      17              : 
      18              : import { useWorkspaceSwitch } from './useWorkspaceSwitch'
      19              : 
      20         2355 : const WORKSPACE_NAMESPACE = PRESERVED_QUERY_NAMESPACES.WORKSPACE
      21         2355 : const DEPLOYMENT_NAMESPACE = PRESERVED_QUERY_NAMESPACES.DEPLOYMENT
      22         2355 : const SWITCH_KEY = WORKSPACE_STORAGE_KEYS.DEPLOYMENT_LINK_SWITCH
      23              : 
      24              : const COMFY_CLOUD_LINK_VALUE = 'standard'
      25              : 
      26              : /**
      27              :  * One usable value: a non-empty string. Null for absent, empty, a bare
      28              :  * `?deployment` (null), and a repeated value (an array, or the stash's `,`
      29              :  * join of one), so a link that names two deployments picks neither.
      30              :  */
      31              : function readLinkValue(raw: unknown): string | null {
      32              :   if (typeof raw !== 'string') return null
      33              :   const value = raw.trim()
      34              :   return value && !value.includes(',') ? value : null
      35              : }
      36              : 
      37              : /** The link's query: the URL's, plus what the sign-in redirect stashed. */
      38              : function linkQuery(routeQuery: LocationQueryRaw): LocationQueryRaw {
      39              :   hydratePreservedQuery(WORKSPACE_NAMESPACE)
      40              :   hydratePreservedQuery(DEPLOYMENT_NAMESPACE)
      41              :   const withWorkspace =
      42              :     mergePreservedQueryIntoQuery(WORKSPACE_NAMESPACE, routeQuery) ?? routeQuery
      43              :   return (
      44              :     mergePreservedQueryIntoQuery(DEPLOYMENT_NAMESPACE, withWorkspace) ??
      45              :     withWorkspace
      46              :   )
      47              : }
      48              : 
      49              : /**
      50              :  * The workspace this page switched to for a deployment link, taken once: the
      51              :  * next boot reads and drops it, so a switch that did not land is never tried
      52              :  * again.
      53              :  */
      54              : function takeSwitchTarget(): string | null {
      55              :   try {
      56              :     const id = sessionStorage.getItem(SWITCH_KEY)
      57              :     sessionStorage.removeItem(SWITCH_KEY)
      58              :     return id
      59              :   } catch {
      60              :     return null
      61              :   }
      62              : }
      63              : 
      64              : function setSwitchTarget(workspaceId: string | null): boolean {
      65              :   try {
      66              :     if (workspaceId === null) sessionStorage.removeItem(SWITCH_KEY)
      67              :     else sessionStorage.setItem(SWITCH_KEY, workspaceId)
      68              :     return true
      69              :   } catch {
      70              :     return false
      71              :   }
      72              : }
      73              : 
      74              : /**
      75              :  * Opens the deployment, or Comfy Cloud for `standard`, that a
      76              :  * `?workspace=<id>&deployment=<id>` link names.
      77              :  */
      78              : export function useDeploymentUrlLoader() {
      79              :   const route = useRoute()
      80              :   const router = useRouter()
      81              :   const { t } = useI18n()
      82              :   const toast = useToast()
      83              :   const { isApiKeyLogin } = useCurrentUser()
      84              :   const { switchWorkspace } = useWorkspaceSwitch()
      85              :   const { activeWorkspace, workspaceName } = storeToRefs(
      86              :     useTeamWorkspaceStore()
      87              :   )
      88              :   const pickStore = useDeploymentPickStore()
      89              : 
      90              :   /** Strips the given params, keeping the rest, and clears their stashes.
      91              :    * Must complete before a pick reloads the page, or the reload would run
      92              :    * the link again. */
      93              :   async function stripParams(
      94              :     query: LocationQueryRaw,
      95              :     keys: ('workspace' | 'deployment')[]
      96              :   ) {
      97              :     const cleanQuery = { ...query }
      98              :     for (const key of keys) delete cleanQuery[key]
      99              :     try {
     100              :       await router.replace({ query: cleanQuery })
     101              :     } catch (error) {
     102              :       console.warn(
     103              :         '[useDeploymentUrlLoader] Failed to clean URL params:',
     104              :         error
     105              :       )
     106              :     }
     107              :     if (keys.includes('workspace')) clearPreservedQuery(WORKSPACE_NAMESPACE)
     108              :     clearPreservedQuery(DEPLOYMENT_NAMESPACE)
     109              :   }
     110              : 
     111              :   function notifyWorkspaceRefused() {
     112              :     toast.error(
     113              :       t(
     114              :         'deploymentSwitcher.linkWorkspaceRefused',
     115              :         { workspaceName: workspaceName.value },
     116              :         { escapeParameter: false }
     117              :       ),
     118              :       { duration: 8000 }
     119              :     )
     120              :   }
     121              : 
     122              :   /**
     123              :    * In another workspace: switch there, keeping the link for the boot after
     124              :    * the switch's reload. Returns `true` when that reload is in flight.
     125              :    */
     126              :   async function openWorkspace(
     127              :     query: LocationQueryRaw,
     128              :     workspaceId: string,
     129              :     switchedTo: string | null
     130              :   ): Promise<boolean> {
     131              :     // A switch this link already made did not land: never reload again.
     132              :     if (switchedTo !== workspaceId && setSwitchTarget(workspaceId)) {
     133              :       if (await switchWorkspace(workspaceId)) return true
     134              :       setSwitchTarget(null)
     135              :     }
     136              :     await stripParams(query, ['workspace', 'deployment'])
     137              :     notifyWorkspaceRefused()
     138              :     return false
     139              :   }
     140              : 
     141              :   async function pickDeployment(
     142              :     query: LocationQueryRaw,
     143              :     linkValue: string
     144              :   ): Promise<boolean> {
     145              :     await stripParams(query, ['workspace', 'deployment'])
     146              :     const outcome = await pickStore.pickFromLink(
     147              :       linkValue === COMFY_CLOUD_LINK_VALUE ? null : linkValue
     148              :     )
     149              :     if (outcome.kind === 'timedOut') {
     150              :       toast.error(t('deploymentSwitcher.linkTimedOut'), { duration: 8000 })
     151              :     }
     152              :     if (outcome.kind === 'refused') {
     153              :       toast.error(t('deploymentSwitcher.linkRefused'), {
     154              :         description: outcome.message,
     155              :         duration: 8000
     156              :       })
     157              :     }
     158              :     return outcome.kind === 'picked'
     159              :   }
     160              : 
     161              :   /**
     162              :    * Reads the link and acts on it. Returns `true` when a reload is in flight
     163              :    * (a workspace switch or a pick), so the caller stops running loaders.
     164              :    */
     165              :   async function loadDeploymentFromUrl(): Promise<boolean> {
     166              :     const query = linkQuery(route.query)
     167              :     const switchedTo = takeSwitchTarget()
     168              :     if (!('deployment' in query)) return false
     169              : 
     170              :     const workspaceId = readLinkValue(query.workspace)
     171              :     const deploymentId = readLinkValue(query.deployment)
     172              :     // `workspace` stays for its own loader, which opens the workspace alone.
     173              :     if (!workspaceId || !deploymentId || isApiKeyLogin.value) {
     174              :       await stripParams(query, ['deployment'])
     175              :       return false
     176              :     }
     177              :     if (activeWorkspace.value?.id !== workspaceId) {
     178              :       return openWorkspace(query, workspaceId, switchedTo)
     179              :     }
     180              :     return pickDeployment(query, deploymentId)
     181              :   }
     182              : 
     183              :   return {
     184              :     loadDeploymentFromUrl
     185              :   }
     186              : }
        

Generated by: LCOV version 2.0-1