'use client'; import { useState } from 'react'; import { Plus, Search, Filter, MoreHorizontal, Edit, Trash2, Eye } from 'lucide-react'; import { Button } from '@/components/ui/button'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; interface ContentType { id: string; name: string; apiIdentifier: string; description?: string | null; fields: Field[]; entries: ContentEntry[]; } interface Field { id: string; label: string; apiIdentifier: string; type: string; isRequired: boolean; } interface ContentEntry { id: string; data: any; status: string; createdAt: string; updatedAt: string; } interface ContentEntriesPageProps { contentType: ContentType; } export function ContentEntriesPage({ contentType }: ContentEntriesPageProps) { const [searchTerm, setSearchTerm] = useState(''); const [statusFilter, setStatusFilter] = useState('all'); const router = useRouter(); const filteredEntries = contentType.entries.filter(entry => { const matchesSearch = searchTerm === '' || Object.values(entry.data).some(value => String(value).toLowerCase().includes(searchTerm.toLowerCase()) ); const matchesStatus = statusFilter === 'all' || entry.status === statusFilter; return matchesSearch && matchesStatus; }); const getDisplayValue = (entry: ContentEntry, field: Field) => { const value = entry.data[field.apiIdentifier]; if (!value) return '-'; if (field.type === 'RICH_TEXT') { // Extraer texto plano del HTML const div = document.createElement('div'); div.innerHTML = value; return div.textContent?.slice(0, 100) + '...' || '-'; } if (field.type === 'MEDIA') { return value.url ? '📷 Imagen' : '-'; } if (field.type === 'BOOLEAN') { return value ? '✅' : '❌'; } if (field.type === 'DATE') { return new Date(value).toLocaleDateString(); } return String(value).slice(0, 50) + (String(value).length > 50 ? '...' : ''); }; const handleDelete = async (entryId: string) => { if (!confirm('¿Estás seguro de que quieres eliminar esta entrada?')) return; try { const response = await fetch(`/api/content-types/${contentType.apiIdentifier}/entries/${entryId}`, { method: 'DELETE' }); if (response.ok) { router.refresh(); } } catch (error) { console.error('Error deleting entry:', error); } }; return (
{/* Header */}

{contentType.name}

{contentType.description || `Gestiona las entradas de ${contentType.name}`}

{/* Stats */}
{contentType.entries.length}
Total
{contentType.entries.filter(e => e.status === 'published').length}
Publicadas
{contentType.entries.filter(e => e.status === 'draft').length}
Borradores
{contentType.entries.filter(e => e.status === 'archived').length}
Archivadas
{/* Filters */}
setSearchTerm(e.target.value)} className="w-full pl-10 pr-4 py-2 border border-gray-200 dark:border-gray-700 rounded-lg bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100" />
{/* Content Table */}
{filteredEntries.length === 0 ? (

No hay entradas

Comienza creando tu primera entrada de {contentType.name}

) : (
{contentType.fields.slice(0, 3).map(field => ( ))} {filteredEntries.map(entry => ( {contentType.fields.slice(0, 3).map(field => ( ))} ))}
{field.label} Estado Actualizado Acciones
{getDisplayValue(entry, field)} {entry.status === 'published' ? 'Publicada' : entry.status === 'draft' ? 'Borrador' : 'Archivada'} {new Date(entry.updatedAt).toLocaleDateString()}
)}
); }