Skip to content

Commit 826e458

Browse files
committed
fix: reload notes and math after vault path change
1 parent ab48790 commit 826e458

File tree

8 files changed

+113
-2
lines changed

8 files changed

+113
-2
lines changed

src/renderer/components/preferences/Storage.vue

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22
import type { DialogOptions } from '~/main/types/ipc'
33
import type { SnippetsCountsResponse } from '~/renderer/services/api/generated'
44
import { Button } from '@/components/ui/shadcn/button'
5-
import { useDialog, useFolders, useSnippets, useSonner } from '@/composables'
5+
import {
6+
resetNotesSpaceInitialization,
7+
useDialog,
8+
useFolders,
9+
useMathNotebook,
10+
useNoteFolders,
11+
useNotes,
12+
useNoteTags,
13+
useSnippets,
14+
useSonner,
15+
} from '@/composables'
616
import { i18n, ipc, store } from '@/electron'
717
import { LoaderCircle } from 'lucide-vue-next'
818
import { api } from '~/renderer/services/api'
919
1020
const { sonner } = useSonner()
1121
const { confirm } = useDialog()
1222
const { getFolders } = useFolders()
23+
const { reset: resetMathNotebook } = useMathNotebook()
24+
const { resetNoteFoldersState } = useNoteFolders()
25+
const { clearNotesState } = useNotes()
26+
const { resetNoteTags } = useNoteTags()
1327
const { getSnippets } = useSnippets()
1428
1529
function getDefaultVaultPath(baseStoragePath: string): string {
@@ -89,6 +103,11 @@ async function openVaultStorage() {
89103
90104
vaultPath.value = result
91105
store.preferences.set('storage.vaultPath', result)
106+
resetMathNotebook()
107+
clearNotesState()
108+
resetNoteFoldersState()
109+
resetNoteTags()
110+
resetNotesSpaceInitialization()
92111
93112
await nextTick()
94113
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { computed, ref } from 'vue'
3+
4+
const invoke = vi.fn()
5+
6+
globalThis.ref = ref
7+
globalThis.computed = computed
8+
9+
vi.mock('@/electron', () => ({
10+
i18n: {
11+
t: (key: string) => key,
12+
},
13+
ipc: {
14+
invoke,
15+
},
16+
}))
17+
18+
vi.mock('@/composables/useStorageMutation', () => ({
19+
markPersistedStorageMutation: vi.fn(),
20+
}))
21+
22+
describe('useMathNotebook', () => {
23+
beforeEach(() => {
24+
vi.resetModules()
25+
vi.clearAllMocks()
26+
invoke.mockResolvedValue({
27+
sheets: [],
28+
activeSheetId: null,
29+
})
30+
})
31+
32+
it('loads again after reset', async () => {
33+
const { useMathNotebook } = await import(
34+
'../math-notebook/useMathNotebook'
35+
)
36+
const notebook = useMathNotebook()
37+
38+
await notebook.init()
39+
expect(invoke).toHaveBeenCalledTimes(1)
40+
41+
notebook.reset()
42+
await notebook.init()
43+
44+
expect(invoke).toHaveBeenCalledTimes(2)
45+
})
46+
})

src/renderer/composables/math-notebook/useMathNotebook.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ async function loadFromDisk() {
5656
activeSheetId.value = data?.activeSheetId ?? null
5757
}
5858

59+
function resetMathNotebook() {
60+
sheets.value = []
61+
activeSheetId.value = null
62+
initialized = false
63+
}
64+
5965
export function useMathNotebook() {
6066
async function init() {
6167
if (initialized) {
@@ -133,6 +139,7 @@ export function useMathNotebook() {
133139
activeSheet,
134140
init,
135141
reloadFromDisk,
142+
reset: resetMathNotebook,
136143
createSheet,
137144
deleteSheet,
138145
updateSheet,

src/renderer/composables/spaces/notes/__tests__/useNotesSpaceInitialization.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,24 @@ describe('useNotesSpaceInitialization', () => {
130130
expect(context.getNotes).not.toHaveBeenCalled()
131131
expect(context.getNoteTags).not.toHaveBeenCalled()
132132
})
133+
134+
it('loads again after initialization state reset', async () => {
135+
const context = await setup({
136+
isInitialized: true,
137+
noteId: 1,
138+
displayedNoteIds: [1, 2, 3],
139+
})
140+
141+
const { resetNotesSpaceInitialization } = await import(
142+
'../useNotesSpaceInitialization'
143+
)
144+
145+
resetNotesSpaceInitialization()
146+
await context.initNotesSpace()
147+
148+
expect(context.getNoteFolders).toHaveBeenCalledTimes(1)
149+
expect(context.getNotes).toHaveBeenCalledTimes(1)
150+
expect(context.getNoteTags).toHaveBeenCalledTimes(1)
151+
expect(context.isNotesSpaceInitialized.value).toBe(true)
152+
})
133153
})

src/renderer/composables/spaces/notes/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export { useNoteFolderTree } from './useNoteFolderTree'
55
export { useNotes } from './useNotes'
66
export { useNotesApp } from './useNotesApp'
77
export { useNoteSearch } from './useNoteSearch'
8-
export { useNotesSpaceInitialization } from './useNotesSpaceInitialization'
8+
export {
9+
resetNotesSpaceInitialization,
10+
useNotesSpaceInitialization,
11+
} from './useNotesSpaceInitialization'
912
export { useNoteTags } from './useNoteTags'
1013
export { useNoteUpdate } from './useNoteUpdate'

src/renderer/composables/spaces/notes/useNoteFolders.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ function clearFolderSelection() {
168168
lastSelectedFolderId.value = undefined
169169
}
170170

171+
function resetNoteFoldersState() {
172+
folders.value = []
173+
renameFolderId.value = null
174+
clearFolderSelection()
175+
}
176+
171177
function setFolderSelection(ids: number[]) {
172178
if (!ids.length) {
173179
clearFolderSelection()
@@ -409,6 +415,7 @@ export function useNoteFolders() {
409415
getNoteFolders,
410416
lastSelectedFolderId,
411417
renameFolderId,
418+
resetNoteFoldersState,
412419
selectedFolderIds,
413420
selectNoteFolder,
414421
setFolderSelection,

src/renderer/composables/spaces/notes/useNoteTags.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ async function deleteNoteTag(tagId: number) {
5959
}
6060
}
6161

62+
function resetNoteTags() {
63+
tags.value = []
64+
}
65+
6266
export function useNoteTags() {
6367
return {
6468
addNoteTag,
6569
deleteNoteTag,
6670
getNoteTags,
6771
isLoading,
72+
resetNoteTags,
6873
tags,
6974
updateNoteTag,
7075
}

src/renderer/composables/spaces/notes/useNotesSpaceInitialization.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const { getNotes, selectFirstNote } = useNotes()
1515
const { displayedNotes } = useNoteSearch()
1616
const { getNoteTags } = useNoteTags()
1717

18+
export function resetNotesSpaceInitialization() {
19+
isNotesSpaceInitialized.value = false
20+
}
21+
1822
function hasSelectedNoteInList(noteId: number | undefined): boolean {
1923
if (noteId === undefined || !displayedNotes.value?.length) {
2024
return false

0 commit comments

Comments
 (0)