25 lines
740 B
TypeScript
25 lines
740 B
TypeScript
import { createContext, Dispatch, SetStateAction, ReactNode, useContext, useState } from "react";
|
|
|
|
type ActiveNoteType = {
|
|
currentNoteId: string | null;
|
|
setCurrentNoteId: Dispatch<SetStateAction<string | null>>;
|
|
}
|
|
|
|
const ActiveNote = createContext<ActiveNoteType | null>(null);
|
|
|
|
export function NoteProvider({ children }: { children: ReactNode }) {
|
|
const [currentNoteId, setCurrentNoteId] = useState<string | null>(null);
|
|
|
|
return (
|
|
<ActiveNote.Provider value={{ currentNoteId, setCurrentNoteId }}>
|
|
{children}
|
|
</ActiveNote.Provider>
|
|
);
|
|
}
|
|
|
|
export function useNote() {
|
|
const context = useContext(ActiveNote);
|
|
if (!context) throw new Error("useNote must be used from within ActiveNote.Provider")
|
|
return context;
|
|
}
|