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

View File

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

View File

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