diff --git a/plugins/view-resources/src/utils.ts b/plugins/view-resources/src/utils.ts index 34d4b80dfa..f5b9cd8a38 100644 --- a/plugins/view-resources/src/utils.ts +++ b/plugins/view-resources/src/utils.ts @@ -481,7 +481,18 @@ export function makeViewletKey (loc?: Location): string { return 'viewlet' + locationToUrl(loc) } -export const activeViewlet = writable | null>>({}) +function getSavedViewlets (): Record | null> { + const res: Record | null> = {} + const keys = Object.keys(localStorage) + for (const key of keys) { + if (!key.startsWith('viewlet')) continue + const item = localStorage.getItem(key) as Ref | null + res[key] = item + } + return res +} + +export const activeViewlet = writable | null>>(getSavedViewlets()) export function setActiveViewletId (viewletId: Ref | null, loc?: Location): void { const key = makeViewletKey(loc) diff --git a/plugins/workbench-resources/src/components/SpecialView.svelte b/plugins/workbench-resources/src/components/SpecialView.svelte index 99b05e1d0c..91311423a8 100644 --- a/plugins/workbench-resources/src/components/SpecialView.svelte +++ b/plugins/workbench-resources/src/components/SpecialView.svelte @@ -88,8 +88,15 @@ }) ) - $: { - const newViewlet = viewlets.find((viewlet) => viewlet?._id === $activeViewlet[key]) ?? viewlets[0] + $: getActiveViewlet(viewlets, $activeViewlet, key) + + function getActiveViewlet ( + viewlets: WithLookup[], + activeViewlet: Record | 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 }