feat: hierarchical notebook system - trash, selectors, breadcrumb, sidebar tree
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2m9s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 2m9s
- Schema: soft delete with trashedAt on Notebook model - API: PATCH/GET notebooks support trashedAt filtering with cascade - Sidebar: recursive tree rendering with collapse/expand, visual guides, hover actions - HierarchicalNotebookSelector: portal-based dropdown with search, breadcrumbs, dropUp support - AI chat: context selector with Toutes mes notes + notebook selector - Agent detail: flat selects replaced with HierarchicalNotebookSelector - Breadcrumb: notebook path display on home page - Trash view: card grid with countdown, restore/permanent delete - CSS: design tokens (ink, paper, blueprint, concrete, etc.) - Types: parentId, trashedAt added to Notebook interface
This commit is contained in:
@@ -3,7 +3,19 @@ import prisma from '@/lib/prisma'
|
||||
import { auth } from '@/auth'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
|
||||
// PATCH /api/notebooks/[id] - Update a notebook
|
||||
async function getDescendantIds(notebookId: string): Promise<string[]> {
|
||||
const ids: string[] = []
|
||||
const children = await prisma.notebook.findMany({
|
||||
where: { parentId: notebookId },
|
||||
select: { id: true },
|
||||
})
|
||||
for (const child of children) {
|
||||
ids.push(child.id)
|
||||
ids.push(...await getDescendantIds(child.id))
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
@@ -16,9 +28,8 @@ export async function PATCH(
|
||||
try {
|
||||
const { id } = await params
|
||||
const body = await request.json()
|
||||
const { name, icon, color, order } = body
|
||||
const { name, icon, color, order, trashedAt } = body
|
||||
|
||||
// Verify ownership
|
||||
const existing = await prisma.notebook.findUnique({
|
||||
where: { id },
|
||||
select: { userId: true }
|
||||
@@ -38,32 +49,30 @@ export async function PATCH(
|
||||
)
|
||||
}
|
||||
|
||||
// Build update data
|
||||
const updateData: any = {}
|
||||
if (name !== undefined) updateData.name = name.trim()
|
||||
if (icon !== undefined) updateData.icon = icon
|
||||
if (color !== undefined) updateData.color = color
|
||||
if (order !== undefined) updateData.order = order
|
||||
if (trashedAt !== undefined) updateData.trashedAt = trashedAt
|
||||
|
||||
// Update notebook
|
||||
const notebook = await prisma.notebook.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
include: {
|
||||
labels: true,
|
||||
_count: {
|
||||
select: { notes: true }
|
||||
}
|
||||
}
|
||||
})
|
||||
if (trashedAt !== undefined) {
|
||||
const descendantIds = await getDescendantIds(id)
|
||||
const allIds = [id, ...descendantIds]
|
||||
await prisma.notebook.updateMany({
|
||||
where: { id: { in: allIds }, userId: session.user.id },
|
||||
data: { trashedAt },
|
||||
})
|
||||
} else {
|
||||
await prisma.notebook.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
})
|
||||
}
|
||||
|
||||
revalidatePath('/')
|
||||
try { revalidatePath('/') } catch {}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
...notebook,
|
||||
notesCount: notebook._count.notes
|
||||
})
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('Error updating notebook:', error)
|
||||
return NextResponse.json(
|
||||
@@ -73,7 +82,6 @@ export async function PATCH(
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/notebooks/[id] - Delete a notebook
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
@@ -86,16 +94,9 @@ export async function DELETE(
|
||||
try {
|
||||
const { id } = await params
|
||||
|
||||
// Verify ownership and get notebook info
|
||||
const notebook = await prisma.notebook.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
userId: true,
|
||||
name: true,
|
||||
_count: {
|
||||
select: { notes: true, labels: true }
|
||||
}
|
||||
}
|
||||
select: { userId: true, name: true }
|
||||
})
|
||||
|
||||
if (!notebook) {
|
||||
@@ -112,18 +113,13 @@ export async function DELETE(
|
||||
)
|
||||
}
|
||||
|
||||
// Delete notebook (cascade will handle labels and notes)
|
||||
await prisma.notebook.delete({
|
||||
where: { id }
|
||||
})
|
||||
await prisma.notebook.delete({ where: { id } })
|
||||
|
||||
revalidatePath('/')
|
||||
try { revalidatePath('/') } catch {}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `Notebook "${notebook.name}" deleted`,
|
||||
notesCount: notebook._count.notes,
|
||||
labelsCount: notebook._count.labels
|
||||
message: `Notebook "${notebook.name}" permanently deleted`,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error deleting notebook:', error)
|
||||
|
||||
@@ -31,7 +31,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
try {
|
||||
const notebooks = await prisma.notebook.findMany({
|
||||
where: { userId: session.user.id },
|
||||
where: { userId: session.user.id, trashedAt: null },
|
||||
include: {
|
||||
labels: { orderBy: { name: 'asc' } },
|
||||
_count: {
|
||||
|
||||
Reference in New Issue
Block a user