import React, { useState } from 'react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { CheckCircle, XCircle, ExternalLink, Calendar, Tag, FileText, RefreshCw } from 'lucide-react';
import { apiClient, type Article } from '../lib/api';
const Articles: React.FC = () => {
const [page, setPage] = useState(1);
const [isRefreshing, setIsRefreshing] = useState(false);
const queryClient = useQueryClient();
const { data, isLoading, error } = useQuery({
queryKey: ['articles', page],
queryFn: () => apiClient.getArticles(page),
});
const approveMutation = useMutation({
mutationFn: (articleId: number) => apiClient.approveArticle(articleId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['articles'] });
},
});
const rejectMutation = useMutation({
mutationFn: (articleId: number) => apiClient.rejectArticle(articleId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['articles'] });
},
});
const refreshMutation = useMutation({
mutationFn: () => apiClient.refreshArticles(),
onSuccess: () => {
// Keep the button in "refreshing" state for 10 seconds
setIsRefreshing(true);
// Refresh the articles list after 10 seconds
setTimeout(() => {
queryClient.invalidateQueries({ queryKey: ['articles'] });
setIsRefreshing(false);
}, 10000);
},
onError: () => {
// Reset the refreshing state on error
setIsRefreshing(false);
},
});
const handleApprove = (articleId: number) => {
approveMutation.mutate(articleId);
};
const handleReject = (articleId: number) => {
rejectMutation.mutate(articleId);
};
const handleRefresh = () => {
refreshMutation.mutate();
};
const getStatusBadge = (status: string) => {
switch (status) {
case 'approved':
return (
Failed to load articles
Manage and review articles from your feeds
{settings?.publishing_approvals_enabled && ({article.description || 'No description available'}
No articles have been fetched yet.
Showing{' '} {pagination.from} to{' '} {pagination.to} of{' '} {pagination.total} results