Files
speckle-server/packages/frontend-2/lib/common/composables/url.ts
T
Kristaps Fabians Geikins 9427686d42 fix(fe2): various follow mode & thread viewer state sync fixes & improvements (#1595)
* fix(fe2): unfollow on camera move

* WIP new state hydration function

* WIP sync state

* minor cleanup

* fix coloring not being tracked

* fix for post thread close camera pos restore

* supporting duplicate users

* preventing guest commenting + state reset fixes

* fixed guests not receiving viewer comment updates

* post-thread creation opens new thread

* removing gap between 'X is typing' and bubble appearing

* reset filters will also reset colors now

* fixed thread full context

* camera reset fix

* thread reset fix

* fixed router concurrency issues

* followed user avatar fix

* TONS OF DEBUGGING FOR ROUTER QUEUING

* removing queued routing debugging stuff + disabling spotlight cancelation

* WIP async URL updates

* missing authLogger fixed

* fix for broken projection

* fix for bubbles positions not updating correctly

* queued routing cleanup

* fixed spotlight mode disabling unnecessarily

* added back stoplight stop on ctrl

* undid spotlight debugging
2023-05-29 15:20:32 +03:00

53 lines
1.4 KiB
TypeScript

import { reduce } from 'lodash-es'
import { 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('&')}`
}
/**
* 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: () => {
const hash = route.hash
if (hash.length < 2 || !hash.startsWith('#')) return {}
const keyValuePairs = hash.substring(1).split('&')
return reduce(
keyValuePairs,
(result, item) => {
const [key, value] = item.split('=')
if (key && value) {
result[key] = value
}
return result
},
{} as Record<string, Nullable<string>>
)
},
set: async (newVal) => {
const hashString = serializeHashState(newVal)
await router.push({
query: route.query,
hash: hashString
})
},
initialState: {}
})
return { hashState }
}