Load active viewlet (#2994)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2023-04-16 17:11:40 +06:00 committed by GitHub
parent 986f130dc2
commit 458ba29ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -481,7 +481,18 @@ export function makeViewletKey (loc?: Location): string {
return 'viewlet' + locationToUrl(loc)
}
export const activeViewlet = writable<Record<string, Ref<Viewlet> | null>>({})
function getSavedViewlets (): Record<string, Ref<Viewlet> | null> {
const res: Record<string, Ref<Viewlet> | null> = {}
const keys = Object.keys(localStorage)
for (const key of keys) {
if (!key.startsWith('viewlet')) continue
const item = localStorage.getItem(key) as Ref<Viewlet> | null
res[key] = item
}
return res
}
export const activeViewlet = writable<Record<string, Ref<Viewlet> | null>>(getSavedViewlets())
export function setActiveViewletId (viewletId: Ref<Viewlet> | null, loc?: Location): void {
const key = makeViewletKey(loc)

View File

@ -88,8 +88,15 @@
})
)
$: {
const newViewlet = viewlets.find((viewlet) => viewlet?._id === $activeViewlet[key]) ?? viewlets[0]
$: getActiveViewlet(viewlets, $activeViewlet, key)
function getActiveViewlet (
viewlets: WithLookup<Viewlet>[],
activeViewlet: Record<string, Ref<Viewlet> | null>,
key: string
) {
if (viewlets.length === 0) return
const newViewlet = viewlets.find((viewlet) => viewlet?._id === activeViewlet[key]) ?? viewlets[0]
if (viewlet?._id !== newViewlet?._id) {
preference = undefined
}