feat(notes): add notes hook

This commit is contained in:
devcodetools committed 2026-09-13 15:22:43 +02:00
1 parent c64c5d408c
commit b86baee257
2 files changed
+136

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import type { Note } from '../types'
import { loadNotes, saveNotes } from '../lib/notesStorage'
export function useNotes() {
const [notes, setNotes] = useState<Note[]>(() => loadNotes())
useEffect(() => {
saveNotes(notes)
}, [notes])
const addNote = useCallback(() => {
const note: Note = { id: Date.now(), title: '', content: '' }
setNotes((current) => [...current, note])
return note
}, [])
const updateNote = useCallback((id: number, patch: Partial<Pick<Note, 'title' | 'content'>>) => {
setNotes((current) => current.map((note) => (note.id === id ? { ...note, ...patch } : note)))
}, [])
const deleteNote = useCallback((id: number) => {
setNotes((current) => current.filter((note) => note.id !== id))
}, [])
const sortedNotes = useMemo(
() =>
[...notes].sort((a, b) => a.title.localeCompare(b.title, undefined, { sensitivity: 'base' })),
[notes],
)
return { notes: sortedNotes, addNote, updateNote, deleteNote }
}