Line data Source code
1 : import type { LocationQuery, LocationQueryRaw } from 'vue-router'
2 :
3 2356 : const STORAGE_PREFIX = 'Comfy.PreservedQuery.'
4 2356 : const preservedQueries = new Map<string, Record<string, string>>()
5 :
6 2356 : const readQueryParam = (value: unknown): string | undefined => {
7 8 : if (typeof value === 'string') return value
8 4 : if (!Array.isArray(value)) return undefined
9 0 : return value.find(
10 : (entry: unknown): entry is string =>
11 : typeof entry === 'string' && entry !== ''
12 : )
13 : }
14 :
15 2356 : const getStorageKey = (namespace: string) => `${STORAGE_PREFIX}${namespace}`
16 :
17 2356 : const isValidQueryRecord = (
18 : value: unknown
19 : ): value is Record<string, string> => {
20 0 : if (typeof value !== 'object' || value === null || Array.isArray(value)) {
21 0 : return false
22 : }
23 0 : return Object.values(value).every((v) => typeof v === 'string')
24 : }
25 :
26 2356 : const readFromStorage = (namespace: string): Record<string, string> | null => {
27 66808 : try {
28 66808 : const raw = sessionStorage.getItem(getStorageKey(namespace))
29 66808 : if (!raw) return null
30 :
31 0 : const parsed = JSON.parse(raw)
32 0 : if (!isValidQueryRecord(parsed)) {
33 0 : console.warn('[preservedQuery] invalid storage format')
34 0 : sessionStorage.removeItem(getStorageKey(namespace))
35 0 : return null
36 : }
37 0 : return parsed
38 : } catch {
39 0 : console.warn('[preservedQuery] storage operation failed')
40 0 : removeFromStorage(namespace)
41 0 : return null
42 : }
43 : }
44 :
45 2356 : const removeFromStorage = (namespace: string) => {
46 0 : try {
47 0 : sessionStorage.removeItem(getStorageKey(namespace))
48 : } catch {
49 0 : return
50 : }
51 : }
52 :
53 2356 : const writeToStorage = (
54 : namespace: string,
55 : payload: Record<string, string> | null
56 : ) => {
57 6 : try {
58 6 : if (!payload || Object.keys(payload).length === 0) {
59 2 : sessionStorage.removeItem(getStorageKey(namespace))
60 2 : return
61 : }
62 4 : sessionStorage.setItem(getStorageKey(namespace), JSON.stringify(payload))
63 : } catch (error) {
64 0 : console.warn('[preservedQuery] failed to write storage', {
65 : namespace,
66 : error
67 : })
68 : }
69 : }
70 :
71 2356 : export const hydratePreservedQuery = (namespace: string) => {
72 66813 : if (preservedQueries.has(namespace)) {
73 5 : return
74 : }
75 66808 : const payload = readFromStorage(namespace)
76 66813 : if (payload) {
77 0 : preservedQueries.set(namespace, payload)
78 : }
79 : }
80 :
81 : /**
82 : * By default each capture replaces the namespace stash with the values present
83 : * in the given query. With `merge`, values are merged into the existing stash
84 : * and a key supplied with an empty value clears its stashed entry — for
85 : * namespaces where the stash, not the URL, is the surviving carrier.
86 : */
87 2356 : export const capturePreservedQuery = (
88 : namespace: string,
89 : query: LocationQuery,
90 : keys: string[],
91 : { merge = false }: { merge?: boolean } = {}
92 : ) => {
93 4 : if (!merge) {
94 4 : const payload: Record<string, string> = {}
95 4 : keys.forEach((key) => {
96 8 : const value = readQueryParam(query[key])
97 8 : if (value) {
98 4 : payload[key] = value
99 : }
100 : })
101 4 : if (Object.keys(payload).length === 0) {
102 0 : return
103 : }
104 4 : preservedQueries.set(namespace, payload)
105 4 : writeToStorage(namespace, payload)
106 4 : return
107 : }
108 :
109 0 : hydratePreservedQuery(namespace)
110 0 : const payload: Record<string, string> = {
111 : ...preservedQueries.get(namespace)
112 : }
113 0 : const state = { changed: false }
114 :
115 0 : keys.forEach((key) => {
116 0 : if (!Object.hasOwn(query, key)) return
117 :
118 0 : const value = readQueryParam(query[key])
119 0 : if (value) {
120 0 : payload[key] = value
121 0 : state.changed = true
122 0 : return
123 : }
124 :
125 0 : if (key in payload) {
126 0 : delete payload[key]
127 0 : state.changed = true
128 : }
129 : })
130 :
131 0 : if (!state.changed) return
132 :
133 0 : if (Object.keys(payload).length === 0) {
134 0 : preservedQueries.delete(namespace)
135 : } else {
136 0 : preservedQueries.set(namespace, payload)
137 : }
138 0 : writeToStorage(namespace, payload)
139 : }
140 :
141 0 : export function getPreservedQueryParam(
142 : namespace: string,
143 : key: string
144 : ): string | undefined {
145 0 : hydratePreservedQuery(namespace)
146 0 : return preservedQueries.get(namespace)?.[key]
147 : }
148 :
149 2356 : export const mergePreservedQueryIntoQuery = (
150 : namespace: string,
151 : query?: LocationQueryRaw
152 : ): LocationQueryRaw | undefined => {
153 7068 : const payload = preservedQueries.get(namespace)
154 7068 : if (!payload) return undefined
155 :
156 4 : const nextQuery: LocationQueryRaw = { ...query }
157 4 : let changed = false
158 :
159 4 : for (const [key, value] of Object.entries(payload)) {
160 4 : if (typeof nextQuery[key] === 'string') continue
161 0 : nextQuery[key] = value
162 0 : changed = true
163 : }
164 :
165 7068 : return changed ? nextQuery : undefined
166 : }
167 :
168 2356 : export const clearPreservedQuery = (namespace: string) => {
169 2 : if (!preservedQueries.has(namespace)) return
170 2 : preservedQueries.delete(namespace)
171 2 : writeToStorage(namespace, null)
172 : }
|