Rend les liens entre notes visibles et persistants (sync NoteLink au save, auto-save, graphe réseau rafraîchi), ajoute living blocks, Memory Echo, recherche globale, consentement IA explicite et consolide les prototypes design en architectural-grid. Co-authored-by: Cursor <cursoragent@cursor.com>
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import prisma from '@/lib/prisma'
|
|
import { auth } from '@/auth'
|
|
|
|
function extractBlocks(html: string): Array<{ blockId: string; content: string }> {
|
|
const blocks: Array<{ blockId: string; content: string }> = []
|
|
const regex = /<(?:p|h[1-6]|blockquote)[^>]*data-id="([^"]+)"[^>]*>([\s\S]*?)<\/(?:p|h[1-6]|blockquote)>/gi
|
|
let match
|
|
while ((match = regex.exec(html)) !== null) {
|
|
const blockId = match[1]
|
|
const content = match[2].replace(/<[^>]+>/g, '').trim()
|
|
if (content.length >= 20) {
|
|
blocks.push({ blockId, content })
|
|
}
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
// GET /api/blocks/search?q=xxx&excludeNoteId=yyy
|
|
export async function GET(request: NextRequest) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const q = request.nextUrl.searchParams.get('q')?.trim()
|
|
const excludeNoteId = request.nextUrl.searchParams.get('excludeNoteId')
|
|
|
|
if (!q) {
|
|
return NextResponse.json({ blocks: [] })
|
|
}
|
|
|
|
const where: Record<string, unknown> = {
|
|
userId: session.user.id,
|
|
isArchived: false,
|
|
trashedAt: null,
|
|
}
|
|
if (excludeNoteId) where.id = { not: excludeNoteId }
|
|
|
|
const notes = await prisma.note.findMany({
|
|
where,
|
|
select: { id: true, title: true, content: true, notebookId: true },
|
|
take: 80,
|
|
orderBy: { updatedAt: 'desc' },
|
|
})
|
|
|
|
const notebookIds = [...new Set(notes.map(n => n.notebookId).filter(Boolean) as string[])]
|
|
const notebooks = notebookIds.length > 0
|
|
? await prisma.notebook.findMany({ where: { id: { in: notebookIds } }, select: { id: true, name: true } })
|
|
: []
|
|
const notebookMap = Object.fromEntries(notebooks.map(nb => [nb.id, nb.name]))
|
|
|
|
const qLower = q.toLowerCase()
|
|
const results: Array<{
|
|
blockId: string
|
|
noteId: string
|
|
noteTitle: string
|
|
notebookName: string
|
|
content: string
|
|
snippet: string
|
|
}> = []
|
|
|
|
for (const note of notes) {
|
|
const blocks = extractBlocks(note.content)
|
|
for (const block of blocks) {
|
|
if (!block.content.toLowerCase().includes(qLower) && !note.title?.toLowerCase().includes(qLower)) continue
|
|
const words = block.content.split(/\s+/)
|
|
const snippet = words.slice(0, 30).join(' ') + (words.length > 30 ? '…' : '')
|
|
results.push({
|
|
blockId: block.blockId,
|
|
noteId: note.id,
|
|
noteTitle: note.title || 'Sans titre',
|
|
notebookName: note.notebookId ? (notebookMap[note.notebookId] || 'Général') : 'Général',
|
|
content: block.content,
|
|
snippet,
|
|
})
|
|
if (results.length >= 20) break
|
|
}
|
|
if (results.length >= 20) break
|
|
}
|
|
|
|
return NextResponse.json({ blocks: results })
|
|
}
|