'use client'
import { useState, useTransition } from 'react'
import type { Note } from '@/lib/types'
import { getNoteFeedImage, getNotePlainExcerpt, getNoteDisplayTitle } from '@/lib/note-preview'
import { useLanguage } from '@/lib/i18n'
import { useNoteRefresh } from '@/context/NoteRefreshContext'
import { motion, AnimatePresence } from 'motion/react'
import { ChevronRight, MoreHorizontal, Trash2, Archive, Pin, History, Pencil } from 'lucide-react'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { deleteNote, toggleArchive, togglePin } from '@/app/actions/notes'
import { toast } from 'sonner'
type NotesEditorialViewProps = {
notes: Note[]
onOpen: (note: Note, readOnly?: boolean) => void
notebookName?: string
onOpenHistory?: (note: Note) => void
}
function formatNoteDate(date: Date | string): string {
const d = typeof date === 'string' ? new Date(date) : date
return d.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
}).toUpperCase()
}
function EditorialNoteMenu({ note, onOpen, onOpenHistory }: {
note: Note
onOpen: (note: Note) => void
onOpenHistory?: (note: Note) => void
}) {
const { t } = useLanguage()
const { triggerRefresh } = useNoteRefresh()
const [, startTransition] = useTransition()
const handleDelete = (e: React.MouseEvent) => {
e.stopPropagation()
startTransition(async () => {
try {
await deleteNote(note.id)
triggerRefresh()
toast.success(t('notes.deleted') || 'Note supprimée')
} catch {
toast.error(t('general.error'))
}
})
}
const handleArchive = (e: React.MouseEvent) => {
e.stopPropagation()
startTransition(async () => {
try {
await toggleArchive(note.id, !note.isArchived)
triggerRefresh()
toast.success(note.isArchived ? (t('notes.unarchived') || 'Désarchivée') : (t('notes.archived') || 'Archivée'))
} catch {
toast.error(t('general.error'))
}
})
}
const handlePin = (e: React.MouseEvent) => {
e.stopPropagation()
startTransition(async () => {
try {
await togglePin(note.id, !note.isPinned)
triggerRefresh()
} catch {
toast.error(t('general.error'))
}
})
}
return (
{excerpt}
) : ({t('notes.noContent')}
)} {t('notes.readMore') || 'Read more'}