Skip to content

Commit 5f7b188

Browse files
feat: persist preferences and devtools menu selection in session (#724)
* feat: persist preferences and devtools menu selection in session Use sessionStorage to remember the last active menu item in preferences and devtools views, so navigation state survives within a session but resets on app restart. * feat(devtools): scroll sidebar to selected item on mount * fix(devtools): add bottom padding to sidebar menu
1 parent b83b20d commit 5f7b188

File tree

7 files changed

+46
-7
lines changed

7 files changed

+46
-7
lines changed

src/renderer/components/space-rail/SpaceRail.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ watch(
9797
<RouterLink
9898
v-slot="{ navigate }"
9999
custom
100-
:to="{ name: RouterName.preferencesStorage }"
100+
:to="{ name: RouterName.preferences }"
101101
>
102102
<UiActionButton
103103
:tooltip="i18n.t('preferences:label')"

src/renderer/ipc/listeners/__tests__/main-menu.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ async function setup() {
6868

6969
vi.doMock('@/router', () => ({
7070
RouterName: {
71-
preferencesStorage: 'preferences/storage',
72-
devtoolsCaseConverter: 'devtools/case-converter',
71+
preferences: 'preferences',
72+
devtools: 'devtools',
7373
notesSpace: 'notes-space',
7474
mathNotebook: 'math-notebook',
7575
},

src/renderer/ipc/listeners/main-menu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export function registerMainMenuListeners() {
4747
registerMainMenuContextSync()
4848

4949
ipc.on('main-menu:goto-preferences', () => {
50-
router.push({ name: RouterName.preferencesStorage })
50+
router.push({ name: RouterName.preferences })
5151
})
5252

5353
ipc.on('main-menu:goto-devtools', () => {
54-
router.push({ name: RouterName.devtoolsCaseConverter })
54+
router.push({ name: RouterName.devtools })
5555
})
5656

5757
ipc.on('main-menu:navigate-back', () => {

src/renderer/router/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const routes = [
4444
path: '/preferences',
4545
name: RouterName.preferences,
4646
component: () => import('@/views/Preferences.vue'),
47+
redirect: () => {
48+
const saved = sessionStorage.getItem('preferences:lastRoute')
49+
return { name: saved || RouterName.preferencesStorage }
50+
},
4751
children: [
4852
{
4953
path: 'storage',
@@ -86,6 +90,10 @@ const routes = [
8690
path: '/devtools',
8791
name: RouterName.devtools,
8892
component: () => import('@/views/Devtools.vue'),
93+
redirect: () => {
94+
const saved = sessionStorage.getItem('devtools:lastRoute')
95+
return { name: saved || RouterName.devtoolsCaseConverter }
96+
},
8997
children: [
9098
{
9199
path: 'text/case-converter',

src/renderer/spaceDefinitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function getSpaceDefinitions(): SpaceDefinition[] {
5757
label: i18n.t('spaces.tools.label'),
5858
tooltip: i18n.t('spaces.tools.tooltip'),
5959
icon: Blocks,
60-
to: { name: RouterName.devtoolsCaseConverter },
60+
to: { name: RouterName.devtools },
6161
isActive: routeName =>
6262
isRouteNameInSpace(routeName, RouterName.devtools),
6363
},

src/renderer/views/Devtools.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import { RouterName } from '@/router'
44
import { useRoute } from 'vue-router'
55
66
const route = useRoute()
7+
const sidebarRef = useTemplateRef<HTMLElement>('sidebarRef')
8+
9+
onMounted(() => {
10+
nextTick(() => {
11+
const el = sidebarRef.value?.querySelector('[data-selected="true"]')
12+
el?.scrollIntoView({ block: 'center' })
13+
})
14+
})
715
816
const isActiveRoute = computed(() => {
917
return (name: string) => route.name === name
@@ -89,6 +97,16 @@ const generatorsNav = [
8997
},
9098
]
9199
100+
watch(
101+
() => route.name,
102+
(name) => {
103+
if (name && typeof name === 'string' && name.startsWith('devtools/')) {
104+
sessionStorage.setItem('devtools:lastRoute', name)
105+
}
106+
},
107+
{ immediate: true },
108+
)
109+
92110
const compareNav = [
93111
{
94112
label: i18n.t('devtools:compare.jsonDiff.label'),
@@ -111,7 +129,10 @@ const compareNav = [
111129
</div>
112130
</template>
113131
<template #left>
114-
<div class="scrollbar h-full min-h-0 overflow-y-auto px-2">
132+
<div
133+
ref="sidebarRef"
134+
class="scrollbar h-full min-h-0 overflow-y-auto px-2 pb-2"
135+
>
115136
<RouterLink
116137
v-for="item in convertersNav"
117138
:key="item.name"

src/renderer/views/Preferences.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ const nav: { label: string, name: string, icon: Component }[] = [
6262
},
6363
]
6464
65+
watch(
66+
() => route.name,
67+
(name) => {
68+
if (name && typeof name === 'string' && name.startsWith('preferences/')) {
69+
sessionStorage.setItem('preferences:lastRoute', name)
70+
}
71+
},
72+
{ immediate: true },
73+
)
74+
6575
provide(preferencesKeys, {
6676
scrollRef,
6777
})

0 commit comments

Comments
 (0)