feat: panel admin pages publiées + force-dépublier + nav
- /admin/published : liste toutes les notes publiées - Bouton dépublier (force) pour chaque note - Notification envoyée au propriétaire quand dépublié par admin - API GET /api/admin/published (liste) + DELETE (force unpublish) - Liens signalements affichés si notifications - Onglet 'Pages publiées' dans sidebar admin (icône Shield) - i18n FR/EN - Fix: report page params Promise unwrap
This commit is contained in:
92
memento-note/app/(admin)/admin/published/page.tsx
Normal file
92
memento-note/app/(admin)/admin/published/page.tsx
Normal file
@@ -0,0 +1,92 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { Globe, Eye, Trash2, ExternalLink, Loader2, Shield, ShieldAlert } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
interface PublishedNote {
|
||||
id: string
|
||||
title: string | null
|
||||
publicSlug: string | null
|
||||
publishedAt: string | null
|
||||
user: { name: string | null }
|
||||
_count?: { reports: number }
|
||||
}
|
||||
|
||||
export default function PublishedAdminPage() {
|
||||
const [notes, setNotes] = useState<PublishedNote[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const res = await fetch('/api/admin/published')
|
||||
const data = await res.json()
|
||||
setNotes(data.notes || [])
|
||||
} catch { toast.error('Erreur') }
|
||||
finally { setLoading(false) }
|
||||
}, [])
|
||||
|
||||
useEffect(() => { load() }, [load])
|
||||
|
||||
const forceUnpublish = async (noteId: string) => {
|
||||
try {
|
||||
const res = await fetch('/api/admin/published', {
|
||||
method: 'DELETE',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ noteId }),
|
||||
})
|
||||
if (res.ok) {
|
||||
toast.success('Note dépubliée')
|
||||
load()
|
||||
} else { toast.error('Erreur') }
|
||||
} catch { toast.error('Erreur') }
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-5xl mx-auto" dir="auto">
|
||||
<div className="flex items-center gap-2 mb-6">
|
||||
<Shield size={20} className="text-brand-accent" />
|
||||
<h1 className="text-xl font-semibold">Pages publiées</h1>
|
||||
<span className="text-sm text-muted-foreground">({notes.length})</span>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12"><Loader2 className="h-6 w-6 animate-spin text-muted-foreground" /></div>
|
||||
) : notes.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground text-center py-12">Aucune note publiée.</p>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
{notes.map(note => (
|
||||
<div key={note.id} className="flex items-center gap-3 p-3.5 rounded-xl border border-border bg-card hover:shadow-sm transition-all">
|
||||
<div className="w-9 h-9 rounded-lg bg-green-50 dark:bg-green-950/20 flex items-center justify-center shrink-0">
|
||||
<Globe size={16} className="text-green-600 dark:text-green-400" />
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="text-sm font-medium truncate">{note.title || 'Sans titre'}</div>
|
||||
<div className="text-[11px] text-muted-foreground">
|
||||
par {note.user?.name || 'Inconnu'} · {note.publishedAt ? new Date(note.publishedAt).toLocaleDateString() : ''}
|
||||
{note._count?.reports ? (
|
||||
<span className="ml-2 inline-flex items-center gap-1 text-red-500 font-medium">
|
||||
<ShieldAlert size={10} /> {note._count.reports} signalement{note._count.reports > 1 ? 's' : ''}
|
||||
</span>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 shrink-0">
|
||||
<a href={`/p/${note.publicSlug}`} target="_blank" rel="noopener noreferrer"
|
||||
className="p-2 rounded-lg hover:bg-muted text-muted-foreground hover:text-foreground transition-colors" title="Voir">
|
||||
<ExternalLink size={15} />
|
||||
</a>
|
||||
<button onClick={() => forceUnpublish(note.id)}
|
||||
className="p-2 rounded-lg hover:bg-red-50 dark:hover:bg-red-950/20 text-muted-foreground hover:text-red-500 transition-colors" title="Dépublier">
|
||||
<Trash2 size={15} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
58
memento-note/app/api/admin/published/route.ts
Normal file
58
memento-note/app/api/admin/published/route.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import prisma from '@/lib/prisma'
|
||||
|
||||
async function requireAdmin() {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) return null
|
||||
const user = await prisma.user.findUnique({ where: { id: session.user.id }, select: { role: true } })
|
||||
if (user?.role !== 'ADMIN') return null
|
||||
return session.user.id
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
const userId = await requireAdmin()
|
||||
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { isPublic: true, trashedAt: null },
|
||||
select: {
|
||||
id: true, title: true, publicSlug: true, publishedAt: true,
|
||||
user: { select: { name: true } },
|
||||
},
|
||||
orderBy: { publishedAt: 'desc' },
|
||||
})
|
||||
|
||||
return NextResponse.json({ notes })
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest) {
|
||||
const userId = await requireAdmin()
|
||||
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { noteId } = await request.json()
|
||||
if (!noteId) return NextResponse.json({ error: 'noteId required' }, { status: 400 })
|
||||
|
||||
await prisma.note.update({
|
||||
where: { id: noteId },
|
||||
data: { isPublic: false, publicSlug: null, publishedAt: null },
|
||||
})
|
||||
|
||||
// Notify the owner
|
||||
const note = await prisma.note.findUnique({
|
||||
where: { id: noteId },
|
||||
select: { userId: true, publicSlug: true },
|
||||
})
|
||||
if (note) {
|
||||
await prisma.notification.create({
|
||||
data: {
|
||||
userId: note.userId,
|
||||
type: 'publish_revoked',
|
||||
title: 'Publication retirée par un administrateur',
|
||||
message: 'Votre note a été dépubliée par la modération. Si vous pensez qu\'il s\'agit d\'une erreur, contactez le support.',
|
||||
},
|
||||
}).catch(() => {})
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import { performSignOut } from '@/lib/auth-client'
|
||||
import {
|
||||
LayoutDashboard,
|
||||
Users,
|
||||
Brain,
|
||||
Shield,
|
||||
Settings,
|
||||
StickyNote,
|
||||
Shield,
|
||||
@@ -42,6 +42,11 @@ const ADMIN_NAV_ITEMS = [
|
||||
href: '/admin/ai',
|
||||
icon: Brain,
|
||||
},
|
||||
{
|
||||
titleKey: 'admin.sidebar.published',
|
||||
href: '/admin/published',
|
||||
icon: Shield,
|
||||
},
|
||||
{
|
||||
titleKey: 'admin.sidebar.settings',
|
||||
href: '/admin/settings',
|
||||
|
||||
@@ -1453,6 +1453,7 @@
|
||||
"dashboard": "Dashboard",
|
||||
"users": "Users",
|
||||
"aiManagement": "AI Management",
|
||||
"published": "Published pages",
|
||||
"chat": "AI Chat",
|
||||
"lab": "The Lab (Ideas)",
|
||||
"agents": "Agents",
|
||||
|
||||
@@ -1459,6 +1459,7 @@
|
||||
"dashboard": "Tableau de bord",
|
||||
"users": "Utilisateurs",
|
||||
"aiManagement": "Gestion IA",
|
||||
"published": "Pages publiées",
|
||||
"chat": "Chat IA",
|
||||
"lab": "Le Lab (Idées)",
|
||||
"agents": "Agents",
|
||||
|
||||
Reference in New Issue
Block a user