'use client' import { useState, useMemo } from 'react' import { useRouter } from 'next/navigation' import { motion, AnimatePresence } from 'motion/react' import { Trash2, RotateCcw, X, FileText, Folder, Search, Clock, AlertCircle, } from 'lucide-react' import { Note, Notebook } from '@/lib/types' import { restoreNote, permanentDeleteNote, emptyTrash } from '@/app/actions/notes' import { restoreNotebook, permanentDeleteNotebook } from '@/context/notebooks-context' import { useLanguage } from '@/lib/i18n' import { toast } from 'sonner' import { useNotebooks } from '@/context/notebooks-context' type FilterType = 'all' | 'notes' | 'notebooks' interface TrashItem { id: string title: string itemType: 'note' | 'notebook' deletedAt: string | null content?: string notesCount?: number } function getDaysRemaining(deletedAt?: string | null): number { if (!deletedAt) return 30 const deletedDate = new Date(deletedAt) const now = new Date() const diffTime = now.getTime() - deletedDate.getTime() const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)) return Math.max(0, 30 - diffDays) } export function TrashClient({ initialNotes, initialNotebooks, }: { initialNotes: Note[] initialNotebooks: any[] }) { const { t } = useLanguage() const router = useRouter() const { restoreNotebook: restoreNb, permanentDeleteNotebook: permDeleteNb } = useNotebooks() const [notes, setNotes] = useState(initialNotes) const [notebooks, setNotebooks] = useState(initialNotebooks) const [searchQuery, setSearchQuery] = useState('') const [filterType, setFilterType] = useState('all') const [isEmptying, setIsEmptying] = useState(false) const items: TrashItem[] = useMemo(() => { const all: TrashItem[] = [ ...notes.map(n => ({ id: n.id, title: n.title || t('notes.untitled'), itemType: 'note' as const, deletedAt: (n as any).trashedAt || null, content: n.content, })), ...notebooks.map((nb: any) => ({ id: nb.id, title: nb.name, itemType: 'notebook' as const, deletedAt: nb.trashedAt || null, notesCount: nb._count?.notes ?? 0, })), ] return all .filter(item => { const matchesSearch = item.title.toLowerCase().includes(searchQuery.toLowerCase()) const matchesType = filterType === 'all' || (filterType === 'notes' && item.itemType === 'note') || (filterType === 'notebooks' && item.itemType === 'notebook') return matchesSearch && matchesType }) .sort((a, b) => { const dateA = a.deletedAt ? new Date(a.deletedAt).getTime() : 0 const dateB = b.deletedAt ? new Date(b.deletedAt).getTime() : 0 return dateB - dateA }) }, [notes, notebooks, searchQuery, filterType, t]) const handleRestore = async (item: TrashItem) => { try { if (item.itemType === 'note') { await restoreNote(item.id) setNotes(prev => prev.filter(n => n.id !== item.id)) } else { await restoreNb(item.id) setNotebooks(prev => prev.filter((nb: any) => nb.id !== item.id)) } toast.success(t('trash.restoreSuccess')) } catch { toast.error(t('trash.restoreError')) } } const handlePermanentDelete = async (item: TrashItem) => { try { if (item.itemType === 'note') { await permanentDeleteNote(item.id) setNotes(prev => prev.filter(n => n.id !== item.id)) } else { await permDeleteNb(item.id) setNotebooks(prev => prev.filter((nb: any) => nb.id !== item.id)) } toast.success(t('trash.permanentDeleteSuccess')) } catch { toast.error(t('trash.deleteError')) } } const handleEmptyTrash = async () => { if (!window.confirm(t('trash.emptyTrashConfirm'))) return setIsEmptying(true) try { await emptyTrash() setNotes([]) setNotebooks([]) toast.success(t('trash.emptyTrashSuccess')) } catch { toast.error(t('trash.deleteError')) } finally { setIsEmptying(false) } } return (

{t('sidebar.trash')}

{t('trash.autoDelete30')}

{items.length > 0 && ( )}
setSearchQuery(e.target.value)} className="w-full bg-white dark:bg-white/5 border border-border/40 rounded-2xl pl-12 pr-6 py-4 text-sm outline-none focus:ring-4 ring-foreground/5 transition-all shadow-sm" />
{(['all', 'notes', 'notebooks'] as FilterType[]).map(type => ( ))}
{items.length > 0 ? (
{items.map(item => { const daysLeft = getDaysRemaining(item.deletedAt) return (
{item.itemType === 'note' ? : }

{item.title}

{daysLeft} {t('trash.daysRemaining')}
{item.deletedAt && ( {new Date(item.deletedAt).toLocaleDateString()} )}
{item.itemType === 'note' && item.content ? (
{item.content.replace(/[#*`]/g, '').slice(0, 200)}
) : (
{t('trash.notebookContentPreserved')}
)}
) })}
) : (

{t('trash.empty')}

{t('trash.emptyDescription')}

)}
) }