import { createContext, useContext, useState, ReactNode } from "react"; import { Note } from "../models/Note" type NotesStoreType = { notes: Map; createNote: () => void; updateNote: (noteId: string, changeset: Partial) => void; deleteNote: (noteId: string) => void; getNoteById: (noteId: string) => Note; } const NotesStore = createContext(null); export function NotesStoreProvider({ children }: { children: ReactNode }) { const [notes, setNotes] = useState>(new Map()); const createNote = () => { const newNote = new Note(); setNotes(prev => new Map(prev.set(newNote.id, newNote))); return newNote; } const updateNote = (noteId: string, changeset: Partial) => { const noteToUpdate = notes.get(noteId) const updatedNote = new Note({ ...noteToUpdate, ...changeset }) setNotes(prev => new Map(prev.set(noteId, updatedNote))); } const deleteNote = (noteId: string) => { notes.delete(noteId); setNotes(prev => new Map(prev)) } const getNoteById = (noteId: string) => { return notes.get(noteId); } return ( {children} ) } export function useNotesStore() { const context = useContext(NotesStore) if (!context) { throw new Error("useNotesStore must be used from within NotesStoreProvider") } return context; }