From c7ea52c7f88e90a8316bdcd24ede2d0808d5c91d Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 26 Nov 2023 10:35:38 +0100 Subject: [PATCH] add path to new inbox file in pinia store Not ideal to have data in multiple store (pinia and PouchDB) but it work this way. May need a refactoring. --- src/components/StackedNote.vue | 12 +++++++++++- src/hooks/useComputeBacklinks.hook.ts | 2 +- src/hooks/useFile.hook.ts | 2 +- src/modules/note/cache/prepareNoteCache.ts | 15 +++++++++++++-- src/modules/repo/store/userRepo.store.ts | 3 +++ src/views/FleetingNotes.vue | 5 ++++- 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/components/StackedNote.vue b/src/components/StackedNote.vue index 95c32e6..5816776 100644 --- a/src/components/StackedNote.vue +++ b/src/components/StackedNote.vue @@ -91,6 +91,12 @@ watch(mode, async (newMode) => { if (newMode === 'read' && rawContent.value !== initialRawContent.value) { const editedSha = (await getEditedSha()) ?? sha.value + if (!path.value) { + console.warn('no path found for this file') + + return + } + const newSha = await updateFile({ content: rawContent.value, path: path.value, @@ -98,10 +104,14 @@ watch(mode, async (newMode) => { }) if (!newSha) { + console.warn('no new SHA found for this file') + return } - await saveCacheNote(encodeUTF8ToBase64(rawContent.value), newSha) + await saveCacheNote(encodeUTF8ToBase64(rawContent.value), { + editedSha: newSha + }) initialRawContent.value = rawContent.value } }) diff --git a/src/hooks/useComputeBacklinks.hook.ts b/src/hooks/useComputeBacklinks.hook.ts index ac64362..f622c19 100644 --- a/src/hooks/useComputeBacklinks.hook.ts +++ b/src/hooks/useComputeBacklinks.hook.ts @@ -17,7 +17,7 @@ const isMarkdown = (filename?: string) => filename?.endsWith('.md') ?? false export const useComputeBacklinks = () => { const store = useUserRepoStore() - watch(store,async () => { + watch(store, async () => { if (!store.userSettings?.backlink) { return } diff --git a/src/hooks/useFile.hook.ts b/src/hooks/useFile.hook.ts index a0fbe85..14b281a 100644 --- a/src/hooks/useFile.hook.ts +++ b/src/hooks/useFile.hook.ts @@ -10,7 +10,7 @@ export const useFile = (sha: Ref | string, retrieveContent = true) => { const path = computed(() => { const file = store.files.find((file) => file.sha === toValue(sha)) - return file?.path ?? '' + return file?.path }) const { diff --git a/src/modules/note/cache/prepareNoteCache.ts b/src/modules/note/cache/prepareNoteCache.ts index 7165cb5..c5d4524 100644 --- a/src/modules/note/cache/prepareNoteCache.ts +++ b/src/modules/note/cache/prepareNoteCache.ts @@ -1,19 +1,30 @@ import { data } from '@/data/data' import { DataType } from '@/data/DataType.enum' import { Note } from '@/modules/note/models/Note' +import { useUserRepoStore } from '@/modules/repo/store/userRepo.store' export const prepareNoteCache = (sha: string) => { + const store = useUserRepoStore() + const noteId = data.generateId(DataType.Note, sha) const getCachedNote = async () => data.get(noteId) - const saveCacheNote = async (content: string, editedSha?: string) => { + const saveCacheNote = async ( + content: string, + params?: { editedSha?: string; path?: string } + ) => { const newNote: Note = { _id: noteId, $type: DataType.Note, content, - editedSha + editedSha: params?.editedSha } + store.addFile({ + path: params?.path, + sha: params?.editedSha + }) + await data.update(newNote) } diff --git a/src/modules/repo/store/userRepo.store.ts b/src/modules/repo/store/userRepo.store.ts index f726d3b..7330186 100644 --- a/src/modules/repo/store/userRepo.store.ts +++ b/src/modules/repo/store/userRepo.store.ts @@ -86,6 +86,9 @@ export const useUserRepoStore = defineStore({ }, 350) }) }, + addFile(file: RepoFile) { + this.files = [...this.files.filter((f) => f.sha !== file.sha), file] + }, resetUserRepo() { this.user = '' this.repo = '' diff --git a/src/views/FleetingNotes.vue b/src/views/FleetingNotes.vue index b417b8c..6409e17 100644 --- a/src/views/FleetingNotes.vue +++ b/src/views/FleetingNotes.vue @@ -50,7 +50,10 @@ watch(mode, async (newMode) => { newContent.value = initialContent const { saveCacheNote } = prepareNoteCache(newSha) - await saveCacheNote(encodeUTF8ToBase64(content)) + await saveCacheNote(encodeUTF8ToBase64(content), { + editedSha: newSha, + path: newContentPath + }) addStackedNote('', newSha) }