Skip to content

Commit 69626bb

Browse files
committed
Fix(vault): a vault that fills up shows its notes without a reload (#1023)
Opening an empty folder and then copying a vault into it left every note under a collapsed Inbox until a reload. With no layout stated in vault.json, main infers it from the root's own entries, so the first folder to land moves the vault to root mode, and nothing told the window. Root-level events now ask, and refresh the notice about hidden root notes with it. A second way to the same empty sidebar: refreshNotes drops a listing when the layout moved while it ran, which is right, but nothing followed it. A resync fetches settings and notes side by side, so when settings landed first the notes were thrown away. It now lists again for the same vault, and not for another one. This one was found on Android, where it left the drawer empty until the next resume; the phones get it when they adopt this core. Large attachments were not the cause of the report: those sizes open and list normally on macOS, Windows and Android.
1 parent e65bae5 commit 69626bb

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

‎packages/app-core/src/rename-vault.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,75 @@ describe('store.renameVault', () => {
164164
})
165165
})
166166

167+
describe('a vault whose layout is inferred (#1023)', () => {
168+
const settle = (): Promise<void> => new Promise((resolve) => setTimeout(resolve, 0))
169+
170+
it('follows main when a root entry moves it to root mode', async () => {
171+
const useStore = await loadStore()
172+
expect(useStore.getState().vaultSettings.primaryNotesLocation).toBe('inbox')
173+
;(window.zen.getVaultSettings as ReturnType<typeof vi.fn>).mockResolvedValue({
174+
...DEFAULT_VAULT_SETTINGS,
175+
primaryNotesLocation: 'root'
176+
})
177+
await useStore.getState().applyChange({ kind: 'add', path: 'Trips', folder: 'inbox', scope: 'folder' } as never)
178+
await settle()
179+
expect(useStore.getState().vaultSettings.primaryNotesLocation).toBe('root')
180+
})
181+
182+
it('lists again when the layout moves while the notes are being listed', async () => {
183+
const useStore = await loadStore()
184+
const note = { path: 'Trips/Rome.md', title: 'Rome', folder: 'inbox' }
185+
let answerFirst: (notes: unknown[]) => void = () => {}
186+
const listNotes = window.zen.listNotes as ReturnType<typeof vi.fn>
187+
listNotes.mockReset()
188+
listNotes
189+
.mockImplementationOnce(() => new Promise((resolve) => { answerFirst = resolve }))
190+
.mockResolvedValue([note])
191+
192+
const refresh = useStore.getState().refreshNotes()
193+
await settle()
194+
useStore.setState({ vaultSettings: { ...DEFAULT_VAULT_SETTINGS, primaryNotesLocation: 'root' } })
195+
answerFirst([])
196+
await refresh
197+
198+
expect(listNotes).toHaveBeenCalledTimes(2)
199+
expect(useStore.getState().notes.map((n) => n.path)).toEqual(['Trips/Rome.md'])
200+
})
201+
202+
it('does not list again for another vault', async () => {
203+
const useStore = await loadStore()
204+
let answerFirst: (notes: unknown[]) => void = () => {}
205+
const listNotes = window.zen.listNotes as ReturnType<typeof vi.fn>
206+
listNotes.mockReset()
207+
listNotes.mockImplementationOnce(() => new Promise((resolve) => { answerFirst = resolve }))
208+
209+
const refresh = useStore.getState().refreshNotes()
210+
await settle()
211+
useStore.setState({ vault: { root: '/elsewhere', name: 'elsewhere' } } as never)
212+
answerFirst([])
213+
await refresh
214+
expect(listNotes).toHaveBeenCalledTimes(1)
215+
})
216+
217+
it('does not ask for an entry nested inside a folder', async () => {
218+
const useStore = await loadStore()
219+
const get = window.zen.getVaultSettings as ReturnType<typeof vi.fn>
220+
get.mockClear()
221+
await useStore.getState().applyChange({ kind: 'add', path: 'Trips/Rome.md', folder: 'inbox' } as never)
222+
await settle()
223+
expect(get).not.toHaveBeenCalled()
224+
})
225+
226+
it('keeps the same settings object when the layout did not move', async () => {
227+
const useStore = await loadStore()
228+
const before = useStore.getState().vaultSettings
229+
;(window.zen.getVaultSettings as ReturnType<typeof vi.fn>).mockResolvedValue({ ...DEFAULT_VAULT_SETTINGS })
230+
await useStore.getState().applyChange({ kind: 'change', path: 'Loose.md', folder: 'inbox' } as never)
231+
await settle()
232+
expect(useStore.getState().vaultSettings).toBe(before)
233+
})
234+
})
235+
167236
describe('renameVaultWithPrompt', () => {
168237
it('prefills the current name, names the folder, and saves the answer', async () => {
169238
const useStore = await loadStore()

‎packages/app-core/src/store.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8584,6 +8584,8 @@ export const useStore = create<Store>((set, get) => {
85848584
refreshNotes: async () => {
85858585
const request = ++noteIndexRequest
85868586
const isCurrent = captureFolderActionContext(get)
8587+
const listedVault = get().vault
8588+
const listedBridge = window.zen
85878589
try {
85888590
// Load this vault's manual note order once per vault (drives #224).
85898591
const orderRoot = get().vault?.root ?? ''
@@ -8597,7 +8599,15 @@ export const useStore = create<Store>((set, get) => {
85978599
window.zen.listFolders(),
85988600
window.zen.hasAssetsDir()
85998601
])
8600-
if (!isCurrent() || request !== noteIndexRequest) return
8602+
if (request !== noteIndexRequest) return
8603+
if (!isCurrent()) {
8604+
// The same vault's layout moved while it was listed, which is what a
8605+
// resync that fetches settings and notes side by side does when files
8606+
// settle it (#1023). This listing may predate the move and is dropped,
8607+
// but with nothing to follow it the sidebar stayed empty: list again.
8608+
if (get().vault === listedVault && window.zen === listedBridge) await get().refreshNotes()
8609+
return
8610+
}
86018611
recordRendererPerf('store.refreshNotes.fetch', performance.now() - startedAt, {
86028612
notes: notes.length,
86038613
folders: folders.length,
@@ -8887,6 +8897,7 @@ export const useStore = create<Store>((set, get) => {
88878897
.catch((err) => {
88888898
console.error('resync vault settings failed', err)
88898899
}),
8900+
get().refreshRootContentHidden(),
88908901
tasksSurfaceVisible(get()) ? get().refreshTasks() : Promise.resolve(),
88918902
// Templates ride the feed too (scope 'templates'), so a gap may have
88928903
// swallowed a template saved on another device.
@@ -8988,6 +8999,28 @@ export const useStore = create<Store>((set, get) => {
89888999
if (ev.kind !== 'change') await get().refreshAssets()
89899000
return
89909001
}
9002+
// A vault whose vault.json does not state its layout has it inferred from
9003+
// the root's own entries, so the first folder or note to land there can
9004+
// move it to root mode, and only main notices (#1023). Filling an empty
9005+
// vault left every note under a collapsed Inbox until a reload.
9006+
if (!ev.path.includes('/') && (ev.scope === undefined || ev.scope === 'folder')) {
9007+
const root = get().vault?.root
9008+
void window.zen
9009+
.getVaultSettings()
9010+
.then((settings) => {
9011+
const next = normalizeVaultSettings(settings)
9012+
if (
9013+
get().vault?.root === root &&
9014+
next.primaryNotesLocation !== get().vaultSettings.primaryNotesLocation
9015+
) {
9016+
set({ vaultSettings: next })
9017+
}
9018+
void get().refreshRootContentHidden()
9019+
})
9020+
.catch((err) => {
9021+
console.error('refresh vault settings failed', err)
9022+
})
9023+
}
89919024
if (ev.scope === 'folder') {
89929025
// A folder was created/removed/renamed externally (e.g. in another
89939026
// client sharing this vault). An empty folder produces no note event,

0 commit comments

Comments
 (0)