import { useState, useRef } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { apiService, Feed, Category, Country } from '../services/api' import { Plus, Trash2, ExternalLink, Upload, Download, RefreshCw, Power, PowerOff, AlertCircle } from 'lucide-react' export function Feeds() { const queryClient = useQueryClient() const [showAddForm, setShowAddForm] = useState(false) const [newFeed, setNewFeed] = useState({ nombre: '', url: '', descripcion: '', categoria_id: '', pais_id: '', idioma: '' }) const [filtroActivo, setFiltroActivo] = useState('') const [filtroCategoria, setFiltroCategoria] = useState('') const [filtroPais, setFiltroPais] = useState('') const fileInputRef = useRef(null) const [importResult, setImportResult] = useState<{ imported: number; skipped: number; failed: number; message: string } | null>(null) const { data: feedsData, isLoading } = useQuery({ queryKey: ['feeds', filtroActivo, filtroCategoria, filtroPais], queryFn: () => apiService.getFeeds({ activo: filtroActivo || undefined, categoria_id: filtroCategoria || undefined, pais_id: filtroPais || undefined, }), }) const { data: categorias } = useQuery({ queryKey: ['categories'], queryFn: apiService.getCategories, }) const { data: paises } = useQuery({ queryKey: ['countries'], queryFn: apiService.getCountries, }) const createFeed = useMutation({ mutationFn: apiService.createFeed, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['feeds'] }) setShowAddForm(false) setNewFeed({ nombre: '', url: '', descripcion: '', categoria_id: '', pais_id: '', idioma: '' }) }, }) const deleteFeed = useMutation({ mutationFn: apiService.deleteFeed, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['feeds'] }) }, }) const toggleFeed = useMutation({ mutationFn: apiService.toggleFeed, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['feeds'] }) }, }) const reactivateFeed = useMutation({ mutationFn: apiService.reactivateFeed, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['feeds'] }) }, }) const importFeeds = useMutation({ mutationFn: apiService.importFeeds, onSuccess: (data) => { setImportResult(data) queryClient.invalidateQueries({ queryKey: ['feeds'] }) }, onError: (error: any) => { const message = error?.response?.status === 401 ? 'Debes iniciar sesión para importar archivos' : error?.response?.data?.error || 'Error al importar el archivo' setImportResult({ imported: 0, skipped: 0, failed: 0, message }) }, }) const handleExport = async () => { try { const blob = await apiService.exportFeeds({ activo: filtroActivo || undefined, categoria_id: filtroCategoria || undefined, pais_id: filtroPais || undefined, }) const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = 'feeds_export.csv' document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) } catch (error) { console.error('Export failed:', error) } } const handleImport = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { importFeeds.mutate(file) if (fileInputRef.current) { fileInputRef.current.value = '' } } } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() createFeed.mutate({ nombre: newFeed.nombre, url: newFeed.url, descripcion: newFeed.descripcion || undefined, categoria_id: newFeed.categoria_id ? parseInt(newFeed.categoria_id) : undefined, pais_id: newFeed.pais_id ? parseInt(newFeed.pais_id) : undefined, idioma: newFeed.idioma || undefined, }) } return (

Feeds RSS

{importResult && (
0 || importResult.message.includes('Error') || importResult.message.includes('Debes') ? 'bg-red-50 border-red-200' : 'bg-green-50 border-green-200'}`}>

0 || importResult.message.includes('Error') || importResult.message.includes('Debes') ? 'text-red-800' : 'text-green-800'}> {importResult.message}

{importResult.imported > 0 && (

Importados: {importResult.imported}, Omitidos: {importResult.skipped}, Errores: {importResult.failed}

)}
)}
{showAddForm && (

Nuevo Feed

setNewFeed({ ...newFeed, nombre: e.target.value })} className="input" required /> setNewFeed({ ...newFeed, url: e.target.value })} className="input" required />
setNewFeed({ ...newFeed, descripcion: e.target.value })} className="input" />
setNewFeed({ ...newFeed, idioma: e.target.value })} className="input" />
)}
Total: {feedsData?.total || 0} feeds
{isLoading ? (
) : (
{feedsData?.feeds?.map((feed) => ( ))}
Nombre URL Idioma Categoría País Estado Noticias Errores Acciones
{feed.nombre}
{feed.descripcion && (
{feed.descripcion}
)}
Ver {feed.idioma || '-'} {feed.categoria || '-'} {feed.pais || '-'} {feed.activo ? 'Activo' : 'Inactivo'} {feed.noticias_count || 0} {feed.fallos && feed.fallos > 0 ? ( {feed.fallos} ) : ( - )}
{feed.fallos && feed.fallos > 0 && ( )}
)}
) }