Files
speckle-server/packages/frontend-2/lib/common/composables/url.ts
T
Kristaps Fabians Geikins b54fea0a7c feat: various automate fe2 fixes (#2321)
* minor cleanup

* fix(web-982): hide Open View button if already on that view

* fix(web-1000): showing updated notification when creating automation

* fix(web-1014;web-1016): clearing model select when project changed + listing only valid projects
2024-06-04 13:21:49 +03:00

58 lines
1.5 KiB
TypeScript

import { reduce } from 'lodash-es'
import type { Nullable, Optional } from '@speckle/shared'
import { writableAsyncComputed } from '~~/lib/common/composables/async'
export function serializeHashState(
state: Record<string, Nullable<string>>
): Optional<string> {
return !Object.values(state).filter((i) => i !== null).length
? undefined
: `#${Object.entries(state)
.filter((entry): entry is [string, string] => !!entry[1])
.map(([key, val]) => `${key}=${val}`)
.join('&')}`
}
export function deserializeHashState(hashString: string) {
if (hashString.length < 2 || !hashString.startsWith('#')) return {}
const keyValuePairs = hashString.substring(1).split('&')
const result = reduce(
keyValuePairs,
(result, item) => {
const [key, value] = item.split('=')
if (key && value) {
result[key] = value
}
return result
},
{} as Record<string, Nullable<string>>
)
return result
}
/**
* Read/writable state similar to one in the querystring, but one that uses anchor (#) data instead
*/
export function useRouteHashState() {
const route = useRoute()
const router = useRouter()
const hashState = writableAsyncComputed({
get: () => {
return deserializeHashState(route.hash)
},
set: async (newVal) => {
const hashString = serializeHashState(newVal)
await router.push({
query: route.query,
hash: hashString
})
},
initialState: {},
asyncRead: false
})
return { hashState }
}