'use client' import { createContext, useContext, useState, useEffect, useMemo, useCallback } from 'react' import { useQueryClient } from '@tanstack/react-query' import type { Notebook, Label, Note } from '@/lib/types' import { LabelColorName, LABEL_COLORS } from '@/lib/types' import { getHashColor } from '@/lib/utils' import { emitNoteChange } from '@/lib/note-change-sync' import { getNoteById } from '@/app/actions/notes' import { useNoteRefresh } from './NoteRefreshContext' import { toast } from 'sonner' import { queryKeys } from '@/lib/query-keys' // ===== INPUT TYPES ===== export interface CreateNotebookInput { name: string icon?: string color?: string parentId?: string | null } export interface UpdateNotebookInput { name?: string icon?: string color?: string parentId?: string | null } export interface CreateLabelInput { name: string color?: string notebookId: string } export interface UpdateLabelInput { name?: string color?: string } // ===== CONTEXT VALUE ===== export interface NotebooksContextValue { // État global notebooks: Notebook[] currentNotebook: Notebook | null // null = "Notes générales" currentLabels: Label[] // Labels du notebook actuel isLoading: boolean isMovingNote: boolean error: string | null // Labels from /api/labels (merged from LabelContext) labels: Label[] loading: boolean notebookId: string | null setNotebookId: (notebookId: string | null) => void addLabel: (name: string, color?: LabelColorName, labelNotebookId?: string | null) => Promise refreshLabels: () => Promise getLabelColor: (name: string) => LabelColorName // Actions: Notebooks createNotebookOptimistic: (data: CreateNotebookInput) => Promise updateNotebook: (notebookId: string, data: UpdateNotebookInput) => Promise moveNotebookToParent: (notebookId: string, parentId: string | null) => Promise deleteNotebook: (notebookId: string) => Promise trashNotebook: (notebookId: string) => Promise restoreNotebook: (notebookId: string) => Promise permanentDeleteNotebook: (notebookId: string) => Promise updateNotebookOrderOptimistic: (notebookIds: string[]) => Promise setCurrentNotebook: (notebook: Notebook | null) => void // Actions: Labels createLabel: (data: CreateLabelInput) => Promise