Ajoute la base organisable par carnet (schéma, champs partagés, valeurs par note) avec activation guidée, tableau éditable, kanban et suppression de colonnes. Corrige le multiselect en vue tableau et enrichit sidebar, grille et i18n FR/EN. Inclut aussi les améliorations flashcards SM-2, l'audit consentement IA et la robustesse du serveur MCP (config, validation, rate-limit, métriques). Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
4.2 KiB
TypeScript
140 lines
4.2 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import type { NotebookSchemaPayload, NotePropertyValues } from '@/lib/structured-views/types'
|
|
|
|
type SchemaState = {
|
|
schema: NotebookSchemaPayload | null
|
|
noteValues: Record<string, NotePropertyValues>
|
|
loading: boolean
|
|
error: string | null
|
|
}
|
|
|
|
export function useNotebookSchema(notebookId: string | null | undefined) {
|
|
const [state, setState] = useState<SchemaState>({
|
|
schema: null,
|
|
noteValues: {},
|
|
loading: false,
|
|
error: null,
|
|
})
|
|
|
|
const reload = useCallback(async () => {
|
|
if (!notebookId) {
|
|
setState({ schema: null, noteValues: {}, loading: false, error: null })
|
|
return
|
|
}
|
|
setState((s) => ({ ...s, loading: true, error: null }))
|
|
try {
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`)
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) {
|
|
throw new Error(json.error || 'Failed to load schema')
|
|
}
|
|
setState({
|
|
schema: json.data.schema,
|
|
noteValues: json.data.noteValues ?? {},
|
|
loading: false,
|
|
error: null,
|
|
})
|
|
} catch (e) {
|
|
setState((s) => ({
|
|
...s,
|
|
loading: false,
|
|
error: e instanceof Error ? e.message : 'Error',
|
|
}))
|
|
}
|
|
}, [notebookId])
|
|
|
|
useEffect(() => {
|
|
void reload()
|
|
}, [reload])
|
|
|
|
const enableStructuredMode = useCallback(async () => {
|
|
if (!notebookId) return null
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`, { method: 'POST' })
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) throw new Error(json.error || 'Failed')
|
|
await reload()
|
|
return json.data.schema as NotebookSchemaPayload
|
|
}, [notebookId, reload])
|
|
|
|
const disableStructuredMode = useCallback(async () => {
|
|
if (!notebookId) return
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ action: 'disable' }),
|
|
})
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) throw new Error(json.error || 'Failed')
|
|
await reload()
|
|
}, [notebookId, reload])
|
|
|
|
const addProperty = useCallback(
|
|
async (name: string, type: string, options?: string[]) => {
|
|
if (!notebookId) return null
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ action: 'addProperty', name, type, options }),
|
|
})
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) throw new Error(json.error || 'Failed')
|
|
await reload()
|
|
return json.data.schema as NotebookSchemaPayload
|
|
},
|
|
[notebookId, reload],
|
|
)
|
|
|
|
const deleteProperty = useCallback(
|
|
async (propertyId: string) => {
|
|
if (!notebookId) return
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ action: 'deleteProperty', propertyId }),
|
|
})
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) throw new Error(json.error || 'Failed')
|
|
await reload()
|
|
},
|
|
[notebookId, reload],
|
|
)
|
|
|
|
const setKanbanGroupProperty = useCallback(
|
|
async (propertyId: string | null) => {
|
|
if (!notebookId) return
|
|
const res = await fetch(`/api/notebooks/${notebookId}/schema`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ action: 'updateViewSettings', kanbanGroupPropertyId: propertyId }),
|
|
})
|
|
const json = await res.json()
|
|
if (!res.ok || !json.success) throw new Error(json.error || 'Failed')
|
|
await reload()
|
|
},
|
|
[notebookId, reload],
|
|
)
|
|
|
|
const patchNoteValuesLocal = useCallback((noteId: string, patch: NotePropertyValues) => {
|
|
setState((s) => ({
|
|
...s,
|
|
noteValues: {
|
|
...s.noteValues,
|
|
[noteId]: { ...(s.noteValues[noteId] ?? {}), ...patch },
|
|
},
|
|
}))
|
|
}, [])
|
|
|
|
return {
|
|
...state,
|
|
reload,
|
|
enableStructuredMode,
|
|
disableStructuredMode,
|
|
addProperty,
|
|
deleteProperty,
|
|
setKanbanGroupProperty,
|
|
patchNoteValuesLocal,
|
|
}
|
|
}
|