slight refactoring

This commit is contained in:
2023-05-28 22:23:41 +02:00
parent b4eab2d8e8
commit b3f740770a
3 changed files with 23 additions and 31 deletions

View File

@@ -1,9 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
import { activeNote, updateNote, notes, activeNotesSource } from '@/composables/useNotes' import { activeNote, updateNote, notes, activeNotesSource, getNotes } from '@/composables/useNotes'
import { viewModes, activeViewMode } from '@/composables/useViewMode' import { viewModes, activeViewMode } from '@/composables/useViewMode'
import { import {
getClientKey, getClientKey,
getEncryptionKey, getEncryptionKey,
encryptionKey,
setClientKey, setClientKey,
passphraseRequired passphraseRequired
} from '@/composables/useEncryption' } from '@/composables/useEncryption'
@@ -30,12 +31,13 @@ const firebaseAuthUI =
provide('firebaseAuthUI', firebaseAuthUI) provide('firebaseAuthUI', firebaseAuthUI)
watch( watch(
activeNotesSource, [activeNotesSource, encryptionKey],
() => { () => {
if (activeNotesSource.value === 'firebase') { if (activeNotesSource.value === 'firebase') {
getClientKey() getClientKey()
getEncryptionKey() getEncryptionKey()
} }
getNotes()
}, },
{ immediate: true } { immediate: true }
) )

View File

@@ -59,9 +59,7 @@ async function getEncryptedEncryptionKey(): Promise<EncryptedEncryptionKey | voi
return data?.key return data?.key
} }
async function setEncryptedEncryptionKey( async function setEncryptedEncryptionKey(encryptedEncryptionKey: EncryptedEncryptionKey | null) {
encryptedEncryptionKey: EncryptedEncryptionKey | null
): Promise<void> {
if (!user.value || !db.value) return if (!user.value || !db.value) return
const docRef = doc(db.value, 'encryptionKeys', user.value.uid) const docRef = doc(db.value, 'encryptionKeys', user.value.uid)
await setDoc(docRef, { key: encryptedEncryptionKey }) await setDoc(docRef, { key: encryptedEncryptionKey })

View File

@@ -250,30 +250,22 @@ const parseBaseNotes = (notes: BaseNotes): BaseNotes => {
) )
} }
watch( export const getNotes = async () => {
[activeNotesSource, encryptionKey], baseNotes.value = {}
async () => { let notes: BaseNotes = {}
if (!activeNotesSource.value) return if (activeNotesSource.value === 'local') {
baseNotes.value = {} try {
let notes: BaseNotes = {} notes = JSON.parse(localStorage.getItem('notes') || '{}')
if (activeNotesSource.value === 'local') { } catch (error) {
try { console.log(error)
notes = JSON.parse(localStorage.getItem('notes') || '{}')
} catch (error) {
console.log(error)
}
} else if (activeNotesSource.value === 'firebase' && db.value) {
if (encryptionKey.value === undefined || !user.value) return
const firebaseNotes = (await getDoc(doc(db.value, 'pages', user.value.uid))).data() as {
[noteId: string]: any
}
notes = encryptionKey.value ? decryptNotes(firebaseNotes, encryptionKey.value) : firebaseNotes
console.log('get notes from firebase', notes)
} }
baseNotes.value = parseBaseNotes(notes) } else if (activeNotesSource.value === 'firebase') {
if (!rootNote.value) insertDefaultNotes(defaultNotes) if (encryptionKey.value === undefined || !user.value || !db.value) return
const firebaseNotes = (await getDoc(doc(db.value, 'pages', user.value.uid))).data() as BaseNotes
setActiveNote(rootNote.value?.id) notes = encryptionKey.value ? decryptNotes(firebaseNotes, encryptionKey.value) : firebaseNotes
}, console.log('get notes from firebase', notes)
{ immediate: true } }
) baseNotes.value = parseBaseNotes(notes)
if (!rootNote.value) insertDefaultNotes(defaultNotes)
setActiveNote(rootNote.value?.id)
}