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

            Line data    Source code
       1              : import { readWorkspaceLink } from '@comfyorg/account-core/workspaceLink'
       2              : import type { WorkspaceLinkRead } from '@comfyorg/account-core/workspaceLink'
       3              : import { storeToRefs } from 'pinia'
       4              : import { useI18n } from 'vue-i18n'
       5              : import type { LocationQueryRaw } from 'vue-router'
       6              : import { useRoute, useRouter } from 'vue-router'
       7              : 
       8              : import { useToast } from '@/components/ui/toast/toastStore'
       9              : import { isSafeExtensionBootActive } from '@/platform/customNodes/safeExtensionBoot'
      10              : import {
      11              :   clearPreservedQuery,
      12              :   hydratePreservedQuery,
      13              :   mergePreservedQueryIntoQuery
      14              : } from '@/platform/navigation/preservedQueryManager'
      15              : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
      16              : 
      17              : import { useWorkspaceSwitch } from './useWorkspaceSwitch'
      18              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
      19              : 
      20         2355 : const NAMESPACE = PRESERVED_QUERY_NAMESPACES.WORKSPACE
      21              : 
      22              : /**
      23              :  * Reads the `workspace` link from a merged query. `undefined` only when the
      24              :  * key itself is absent. A repeated value (still possible when the stash
      25              :  * predates router.ts's `rejectRepeated`, or from a caller that bypasses the
      26              :  * tracker) joins with `,`, outside any real workspace id's charset, so it
      27              :  * still resolves to `invalid`. A bare `?workspace` (no `=`) reaches the
      28              :  * router as `null` — present but empty — which readWorkspaceLink can't tell
      29              :  * apart from truly absent (both serialize to `''` in a URLSearchParams
      30              :  * value), so it's classified `invalid` directly here instead.
      31              :  */
      32              : function readWorkspaceLinkFromQuery(
      33              :   query: LocationQueryRaw
      34              : ): WorkspaceLinkRead | undefined {
      35              :   const raw = query.workspace
      36              :   if (raw === undefined) return undefined
      37              : 
      38              :   const values = Array.isArray(raw) ? raw : [raw]
      39              :   if (values.includes(null)) return { status: 'invalid' }
      40              : 
      41              :   return readWorkspaceLink(new URLSearchParams({ workspace: values.join(',') }))
      42              : }
      43              : 
      44              : /**
      45              :  * Opens the workspace a `?workspace=` deep link names, before any other URL
      46              :  * action loader runs — so `?settings=plan-credits&workspace=w-1` opens
      47              :  * Settings on the requested workspace rather than the active one.
      48              :  *
      49              :  * A navigation destination: a member switches into the named workspace: a
      50              :  * non-member, a gone workspace, or an invalid link stays on the active
      51              :  * workspace, but always says which one that is via a toast, per the
      52              :  * workspace deep-link contract (`@comfyorg/account-core/workspaceLink`).
      53              :  * Absent is a silent no-op. Survives the login redirect via the
      54              :  * preserved-query system, like the other loaders — a repeated value is
      55              :  * stashed joined with `,` (router.ts's `rejectRepeated`), so it still reads
      56              :  * as invalid rather than picking one of the colliding values.
      57              :  */
      58              : export function useWorkspaceUrlLoader() {
      59              :   const route = useRoute()
      60              :   const router = useRouter()
      61              :   const { t } = useI18n()
      62              :   const toast = useToast()
      63              :   const { switchWorkspace } = useWorkspaceSwitch()
      64              :   const { activeWorkspace, workspaceName } = storeToRefs(
      65              :     useTeamWorkspaceStore()
      66              :   )
      67              : 
      68              :   function notifyStayed() {
      69              :     toast.info(
      70              :       t(
      71              :         'workspace.deepLinkStayed',
      72              :         { workspaceName: workspaceName.value },
      73              :         { escapeParameter: false }
      74              :       )
      75              :     )
      76              :   }
      77              : 
      78              :   /** Strips `workspace`, keeping other params, and clears the stash. Must
      79              :    * complete before a workspace switch reloads the page, or the reload would
      80              :    * re-trigger this loader on the same link. */
      81              :   async function stripParam(query: LocationQueryRaw) {
      82              :     const cleanQuery = { ...query }
      83              :     delete cleanQuery.workspace
      84              :     try {
      85              :       await router.replace({ query: cleanQuery })
      86              :     } catch (error) {
      87              :       console.warn('[useWorkspaceUrlLoader] Failed to clean URL params:', error)
      88              :     }
      89              :     clearPreservedQuery(NAMESPACE)
      90              :   }
      91              : 
      92              :   /**
      93              :    * Reads `?workspace=`, strips it, and switches (or explains why not).
      94              :    * Returns `true` when it triggered a switch that will reload the page —
      95              :    * the caller must stop running further loaders immediately: `location.reload()`
      96              :    * doesn't halt the current script, so a later loader's own URL cleanup
      97              :    * (e.g. settings unconditionally strips `?settings=`) can otherwise land
      98              :    * on the still-live page and get baked into the URL the reload re-fetches.
      99              :    */
     100              :   async function loadWorkspaceFromUrl(): Promise<boolean> {
     101              :     hydratePreservedQuery(NAMESPACE)
     102              :     const query =
     103              :       mergePreservedQueryIntoQuery(NAMESPACE, route.query) ?? route.query
     104              :     const link = readWorkspaceLinkFromQuery(query)
     105              :     if (!link) return false
     106              : 
     107              :     if (link.status === 'absent') {
     108              :       await stripParam(query)
     109              :       return false
     110              :     }
     111              : 
     112              :     if (link.status === 'invalid') {
     113              :       await stripParam(query)
     114              :       notifyStayed()
     115              :       return false
     116              :     }
     117              : 
     118              :     // Already there: strip and say nothing, nothing changed.
     119              :     if (link.workspaceId === activeWorkspace.value?.id) {
     120              :       await stripParam(query)
     121              :       return false
     122              :     }
     123              : 
     124              :     return switchFromLink(query, link.workspaceId)
     125              :   }
     126              : 
     127              :   async function switchFromLink(
     128              :     query: LocationQueryRaw,
     129              :     workspaceId: string
     130              :   ): Promise<boolean> {
     131              :     const keepSafeBoot = isSafeExtensionBootActive()
     132              :     await stripParam(keepSafeBoot ? { ...query, safe: '' } : query)
     133              :     if (await switchWorkspace(workspaceId)) return true
     134              :     if (keepSafeBoot) await stripParam(query)
     135              :     notifyStayed()
     136              :     return false
     137              :   }
     138              : 
     139              :   return {
     140              :     loadWorkspaceFromUrl
     141              :   }
     142              : }
        

Generated by: LCOV version 2.0-1