Files
takerofnotes-app/src/renderer/src/composables/useNotes.js
2026-05-07 13:11:16 -04:00

109 lines
2.7 KiB
JavaScript

import useNoteListeners from '@/composables/useNoteListeners'
import useCore from '@/composables/useCore'
import { ref } from 'vue'
const categories = ref([])
const searchResults = ref([])
const decryptionFailures = ref([])
export default () => {
const { getNotesAPI } = useCore()
// Change listeners for electron
const { setupListeners, broadcastChange, changeCount } = useNoteListeners()
setupListeners()
getNotesAPI()
const getDecryptionFailures = async () => {
const api = await getNotesAPI()
decryptionFailures.value = api.getDecryptionFailures()
}
getDecryptionFailures()
// Load
const loadCategories = async () => {
const api = await getNotesAPI()
categories.value = api.getCategories()
}
const loadCategoryNotes = async (category = null) => {
const api = await getNotesAPI()
return api.getCategoryNotes(category)
}
const loadNote = async (id) => {
const api = await getNotesAPI()
return await api.getNote(id)
}
// Create
const createNote = async (metadata, content, plainText = '') => {
const api = await getNotesAPI()
const note = await api.createNote(metadata, content, plainText)
await loadCategories()
broadcastChange('note-created', note)
return note
}
// Update
const updateNote = async (id, updates) => {
const api = await getNotesAPI()
const note = await api.updateNote(id, updates)
if (updates.category !== undefined || updates.title !== undefined) {
await loadCategories()
}
broadcastChange('note-updated', note)
return note
}
const updateCategory = async (category, update) => {
const notes = await loadCategoryNotes(category)
for (const note of notes) {
await updateNote(note.id, { category: update })
}
await loadCategories()
}
// Delete
const deleteNote = async (id) => {
const api = await getNotesAPI()
await api.deleteNote(id)
await loadCategories()
broadcastChange('note-deleted', { id })
getDecryptionFailures()
}
// Search
const search = async (query) => {
const api = await getNotesAPI()
if (!query) {
searchResults.value = []
return
}
searchResults.value = api.search(query)
}
return {
categories,
searchResults,
decryptionFailures,
changeCount,
loadCategories,
loadCategoryNotes,
loadNote,
createNote,
updateNote,
updateCategory,
deleteNote,
search,
}
}