Line data Source code
1 : import { useRoute, useRouter } from 'vue-router'
2 :
3 : import {
4 : clearPreservedQuery,
5 : hydratePreservedQuery,
6 : mergePreservedQueryIntoQuery
7 : } from '@/platform/navigation/preservedQueryManager'
8 : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
9 : import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore'
10 :
11 2355 : const NAMESPACE = PRESERVED_QUERY_NAMESPACES.ASSETS
12 : const ASSETS_TAB_ID = 'assets'
13 :
14 : /**
15 : * Opens the Assets sidebar panel from an `?assets=1` deep link, so external
16 : * surfaces like comfy.org can send a visitor from a saved generation to the
17 : * library that holds the rest of them.
18 : *
19 : * Only `1` opens the panel; any other value is a silent no-op with the param
20 : * stripped. Survives the login redirect via the preserved-query system, like
21 : * the settings and top-up URL loaders.
22 : */
23 : export function useAssetsUrlLoader() {
24 : const route = useRoute()
25 : const router = useRouter()
26 : const sidebarTabStore = useSidebarTabStore()
27 :
28 : /** Reads `?assets=`, strips it, and opens the Assets panel. Resolves once
29 : * the strip has landed, so the loader that runs next reads a query this one
30 : * has already cleaned rather than restoring the param. */
31 : async function loadAssetsFromUrl() {
32 : hydratePreservedQuery(NAMESPACE)
33 : const query =
34 : mergePreservedQueryIntoQuery(NAMESPACE, route.query) ?? route.query
35 : const param = query.assets
36 : if (param === undefined) return
37 :
38 : // Strip the param (even an unrecognized value) and write the clean URL in
39 : // a single replace, so the URL is clean even if the replace rejects.
40 : const cleanQuery = { ...query }
41 : delete cleanQuery.assets
42 : const stripped = router.replace({ query: cleanQuery }).catch((error) => {
43 : console.warn('[useAssetsUrlLoader] Failed to clean URL params:', error)
44 : })
45 : clearPreservedQuery(NAMESPACE)
46 :
47 : // Assigned rather than toggled: a deep link must leave the panel open,
48 : // including for a visitor who already had it open.
49 : if (param === '1') sidebarTabStore.activeSidebarTabId = ASSETS_TAB_ID
50 :
51 : await stripped
52 : }
53 :
54 : return {
55 : loadAssetsFromUrl
56 : }
57 : }
|