feat: Publication de pages — notes publiques sur URL
- Migration: champs isPublic + publicSlug + publishedAt sur Note - Route publique /p/[slug] — rendu SSR sans auth, prose styled - Server actions: publishNote / unpublishNote / getPublishedNote - API /api/notes/publish — toggle publication + génération slug - PublishDialog — modal avec lien copiable + bouton dépublier - Bouton Globe dans le toolbar (vert si publiée) - i18n FR/EN - Pattern inspiré de BrainstormSession.isPublic
This commit is contained in:
67
memento-note/app/(public)/p/[slug]/page.tsx
Normal file
67
memento-note/app/(public)/p/[slug]/page.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
import { notFound } from 'next/navigation'
|
||||
import { getPublishedNote } from '@/app/actions/notes-publishing'
|
||||
import { FileText, Calendar } from 'lucide-react'
|
||||
import { format } from 'date-fns'
|
||||
|
||||
export async function generateMetadata({ params }: { params: Promise<{ slug: string }> }) {
|
||||
const { slug } = await params
|
||||
const note = await getPublishedNote(slug)
|
||||
if (!note) return { title: 'Note not found' }
|
||||
return { title: note.title || 'Published note', description: note.content?.replace(/<[^>]+>/g, '').slice(0, 160) }
|
||||
}
|
||||
|
||||
export const dynamic = 'force-static'
|
||||
export const revalidate = 3600
|
||||
|
||||
export default async function PublishedNotePage({ params }: { params: Promise<{ slug: string }> }) {
|
||||
const { slug } = await params
|
||||
const note = await getPublishedNote(slug)
|
||||
|
||||
if (!note) notFound()
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-[#F2F0E9] dark:bg-zinc-950">
|
||||
<div className="max-w-3xl mx-auto px-6 py-12">
|
||||
{/* Header */}
|
||||
<div className="mb-8 pb-6 border-b border-black/10 dark:border-white/10">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground mb-3">
|
||||
<FileText size={14} />
|
||||
<span>Note publiée</span>
|
||||
{note.publishedAt && (
|
||||
<>
|
||||
<span>·</span>
|
||||
<Calendar size={12} />
|
||||
<span>{format(new Date(note.publishedAt), 'MMM d, yyyy')}</span>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
<h1 className="text-3xl font-serif font-medium tracking-tight text-foreground leading-tight mb-2">
|
||||
{note.title || 'Sans titre'}
|
||||
</h1>
|
||||
{note.user?.name && (
|
||||
<p className="text-sm text-muted-foreground">par {note.user.name}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<article
|
||||
dir="auto"
|
||||
className="prose prose-stone dark:prose-invert max-w-none
|
||||
prose-headings:font-serif prose-headings:font-medium
|
||||
prose-p:leading-relaxed prose-p:text-[15px]
|
||||
prose-pre:bg-zinc-100 prose-pre:dark:bg-zinc-900
|
||||
prose-blockquote:border-brand-accent/40
|
||||
prose-a:text-brand-accent"
|
||||
dangerouslySetInnerHTML={{ __html: note.content || '' }}
|
||||
/>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="mt-12 pt-6 border-t border-black/10 dark:border-white/10 text-center">
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Publié sur Momento
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
81
memento-note/app/actions/notes-publishing.ts
Normal file
81
memento-note/app/actions/notes-publishing.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
'use server'
|
||||
|
||||
import { auth } from '@/auth'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
|
||||
function generateSlug(title: string): string {
|
||||
const base = title
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 60) || 'note'
|
||||
|
||||
return `${base}-${Math.random().toString(36).slice(2, 8)}`
|
||||
}
|
||||
|
||||
export async function publishNote(noteId: string): Promise<{ success: boolean; slug?: string; url?: string; error?: string }> {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return { success: false, error: 'Unauthorized' }
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id },
|
||||
select: { id: true, title: true, isPublic: true, publicSlug: true },
|
||||
})
|
||||
if (!note) return { success: false, error: 'Note not found' }
|
||||
|
||||
let slug = note.publicSlug
|
||||
if (!slug) {
|
||||
slug = generateSlug(note.title || 'note')
|
||||
// Ensure uniqueness
|
||||
const existing = await prisma.note.findUnique({ where: { publicSlug: slug } })
|
||||
if (existing && existing.id !== noteId) {
|
||||
slug = `${slug}-${Date.now().toString(36)}`
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { isPublic: true, publicSlug: slug, publishedAt: new Date() },
|
||||
})
|
||||
|
||||
revalidatePath(`/p/${slug}`)
|
||||
return { success: true, slug, url: `/p/${slug}` }
|
||||
}
|
||||
|
||||
export async function unpublishNote(noteId: string): Promise<{ success: boolean; error?: string }> {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return { success: false, error: 'Unauthorized' }
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id },
|
||||
select: { id: true, publicSlug: true },
|
||||
})
|
||||
if (!note) return { success: false, error: 'Note not found' }
|
||||
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { isPublic: false, publicSlug: null, publishedAt: null },
|
||||
})
|
||||
|
||||
if (note.publicSlug) revalidatePath(`/p/${note.publicSlug}`)
|
||||
return { success: true }
|
||||
}
|
||||
|
||||
export async function getPublishedNote(slug: string) {
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { publicSlug: slug, isPublic: true, trashedAt: null },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
publishedAt: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
user: { select: { name: true, image: true } },
|
||||
},
|
||||
})
|
||||
return note
|
||||
}
|
||||
52
memento-note/app/api/notes/publish/route.ts
Normal file
52
memento-note/app/api/notes/publish/route.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import prisma from '@/lib/prisma'
|
||||
|
||||
function generateSlug(title: string): string {
|
||||
const base = title
|
||||
.toLowerCase()
|
||||
.normalize('NFD')
|
||||
.replace(/[\u0300-\u036f]/g, '')
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 60) || 'note'
|
||||
return `${base}-${Math.random().toString(36).slice(2, 8)}`
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { noteId, action } = await request.json()
|
||||
if (!noteId) return NextResponse.json({ error: 'noteId required' }, { status: 400 })
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id },
|
||||
select: { id: true, title: true, publicSlug: true },
|
||||
})
|
||||
if (!note) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
if (action === 'publish') {
|
||||
let slug = note.publicSlug
|
||||
if (!slug) {
|
||||
slug = generateSlug(note.title || 'note')
|
||||
const existing = await prisma.note.findUnique({ where: { publicSlug: slug } })
|
||||
if (existing && existing.id !== noteId) slug = `${slug}-${Date.now().toString(36)}`
|
||||
}
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { isPublic: true, publicSlug: slug, publishedAt: new Date() },
|
||||
})
|
||||
return NextResponse.json({ success: true, slug })
|
||||
}
|
||||
|
||||
if (action === 'unpublish') {
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { isPublic: false, publicSlug: null, publishedAt: null },
|
||||
})
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
|
||||
}
|
||||
Reference in New Issue
Block a user