import { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import { api } from '../api' import ConfirmModal from './ConfirmModal' const statusLabels = { draft: 'Bozza', approved: 'Approvato', scheduled: 'Schedulato', published: 'Pubblicato' } const statusColors = { draft: { bg: '#FFFBEB', color: '#B45309' }, approved: { bg: 'var(--success-light)', color: 'var(--success)' }, scheduled: { bg: '#EFF6FF', color: '#1D4ED8' }, published: { bg: '#F5F3FF', color: '#6D28D9' }, } function groupByBatch(posts) { const groups = [] const batchMap = {} for (const post of posts) { const key = post.batch_id || `solo_${post.id}` if (batchMap[key]) { batchMap[key].posts.push(post) } else { const group = { key, posts: [post] } batchMap[key] = group groups.push(group) } } return groups } export default function ContentArchive() { const [posts, setPosts] = useState([]) const [characters, setCharacters] = useState([]) const [loading, setLoading] = useState(true) const [expandedKey, setExpandedKey] = useState(null) const [activePlatform, setActivePlatform] = useState({}) // key → index const [editingId, setEditingId] = useState(null) const [editText, setEditText] = useState('') const [filterCharacter, setFilterCharacter] = useState('') const [filterStatus, setFilterStatus] = useState('') const [deleteTarget, setDeleteTarget] = useState(null) // { id, batchKey } useEffect(() => { loadData() }, []) const loadData = async () => { setLoading(true) try { const [postsData, charsData] = await Promise.all([api.get('/content/posts'), api.get('/characters/')]) setPosts(postsData) setCharacters(charsData) } catch {} finally { setLoading(false) } } const getCharacterName = (id) => characters.find(c => c.id === id)?.name || '—' const handleApprove = async (postId) => { try { await api.post(`/content/posts/${postId}/approve`); loadData() } catch {} } const handleDelete = async () => { if (!deleteTarget) return try { await api.delete(`/content/posts/${deleteTarget.id}`) setDeleteTarget(null) loadData() } catch {} } const handleSaveEdit = async (postId) => { try { await api.put(`/content/posts/${postId}`, { text_content: editText }) setEditingId(null) loadData() } catch {} } const filtered = posts.filter(p => { if (filterCharacter && String(p.character_id) !== filterCharacter) return false if (filterStatus && p.status !== filterStatus) return false return true }) const groups = groupByBatch(filtered) const formatDate = (d) => d ? new Date(d).toLocaleDateString('it-IT', { day: '2-digit', month: 'short', year: 'numeric' }) : '—' return (
Libreria

Libreria Contenuti

I tuoi contenuti generati

← Genera nuovo
{/* Filters */}
{groups.length} contenut{groups.length === 1 ? 'o' : 'i'}
{loading ? (
) : groups.length === 0 ? (

Nessun contenuto trovato

{posts.length === 0 ? 'Genera il tuo primo contenuto dalla pagina Contenuti' : 'Prova a cambiare i filtri'}

) : (
{groups.map(group => { const activeIdx = activePlatform[group.key] || 0 const activePost = group.posts[activeIdx] || group.posts[0] const sc = statusColors[activePost.status] || statusColors.draft const isExpanded = expandedKey === group.key const isEditing = editingId === activePost.id const hasMultiple = group.posts.length > 1 return (
{ if (!isEditing) setExpandedKey(isExpanded ? null : group.key) }}>
{/* Character name */}

{getCharacterName(activePost.character_id)}

{/* Platform tabs */}
{group.posts.map((p, i) => ( ))}
{/* Status badge */}
{statusLabels[activePost.status] || activePost.status}
{/* Text content */} {isEditing ? (
e.stopPropagation()}>