import { useQuery } from "@tanstack/react-query"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Facebook, Instagram, Clock, CheckCircle2, XCircle, Eye, Image as ImageIcon, Smartphone, Clapperboard } from "lucide-react"; import { SiTiktok } from "react-icons/si"; import { format } from "date-fns"; import { fr } from "date-fns/locale"; import type { ScheduledPost, SocialPage, Post, Media } from "@shared/schema"; import { PreviewModal } from "@/components/preview-modal"; import { useState } from "react"; import { useToast } from "@/hooks/use-toast"; import { Button } from "@/components/ui/button"; type ScheduledPostWithRelations = ScheduledPost & { post?: Post; page?: SocialPage; }; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Trash2 } from "lucide-react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { apiRequest } from "@/lib/queryClient"; export default function RecentPublications() { const [previewModalOpen, setPreviewModalOpen] = useState(false); const [previewData, setPreviewData] = useState<{ postText: string; mediaIds: string[]; allMedia: Media[] }>({ postText: '', mediaIds: [], allMedia: [] }); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [postToDelete, setPostToDelete] = useState(null); const { toast } = useToast(); const queryClient = useQueryClient(); const { data: scheduledPosts = [], isLoading } = useQuery({ queryKey: ['/api/scheduled-posts'], }); // Filter and sort attempted posts (scheduled in the past) const now = new Date(); const recentPublished = scheduledPosts .filter(sp => sp.scheduledAt && new Date(sp.scheduledAt) <= now) .sort((a, b) => new Date(b.scheduledAt!).getTime() - new Date(a.scheduledAt!).getTime()) .slice(0, 10); const handlePreviewPost = async (scheduledPost: ScheduledPostWithRelations) => { try { const response = await fetch(`/api/posts/${scheduledPost.postId}`, { credentials: "include", }); if (!response.ok) { throw new Error("Failed to fetch post"); } const postWithMedia = await response.json(); const mediaIds = postWithMedia.media?.map((m: Media) => m.id) || []; const allMedia = postWithMedia.media || []; setPreviewData({ postText: postWithMedia.post.content || '', mediaIds: mediaIds, allMedia: allMedia, }); setPreviewModalOpen(true); } catch (error) { toast({ title: "Erreur", description: "Impossible de charger la prévisualisation", variant: "destructive", }); } }; const deleteReelMutation = useMutation({ mutationFn: async (postId: string) => { // Pour les Reels/Posts, l'ID est le même. La route DELETE /api/reels/:id // gère la suppression du Post sous-jacent. const res = await apiRequest("DELETE", `/api/reels/${postId}`); return await res.json(); }, onSuccess: () => { toast({ title: "Succès", description: "Publication supprimée avec succès", }); queryClient.invalidateQueries({ queryKey: ['/api/scheduled-posts'] }); setDeleteDialogOpen(false); setPostToDelete(null); }, onError: (error: Error) => { toast({ title: "Erreur", description: error.message || "Impossible de supprimer la publication", variant: "destructive", }); }, }); const handleDeleteClick = (postId: string) => { setPostToDelete(postId); setDeleteDialogOpen(true); }; const confirmDelete = () => { if (postToDelete) { deleteReelMutation.mutate(postToDelete); } }; const getPostTypeIcon = (postType: string) => { if (postType === 'feed') { return ; } else if (postType === 'story') { return ; } else if (postType === 'reel') { return ; } else if (postType === 'both') { return (
); } return null; }; return ( Historique des publications Vos 10 dernières publications sur les réseaux sociaux {isLoading ? (
{[1, 2, 3].map((i) => (
))}
) : recentPublished.length === 0 ? (

Aucune publication pour le moment

) : (
{recentPublished.map((scheduledPost) => (
{/* Platform icon */}
{scheduledPost.page?.platform === 'facebook' ? ( ) : scheduledPost.page?.platform === 'tiktok' ? ( ) : ( )}
{/* Content */}

{scheduledPost.page?.pageName || 'Page inconnue'}

{scheduledPost.post?.content ? ( {scheduledPost.post.content} ) : ( Aucun texte )}

{scheduledPost.error ? ( ) : ( )}
{getPostTypeIcon(scheduledPost.postType)} {scheduledPost.postType === 'feed' ? 'Feed' : scheduledPost.postType === 'story' ? 'Story' : scheduledPost.postType === 'reel' ? 'Reel' : 'Feed & Story'} • {format(new Date(scheduledPost.scheduledAt!), "d MMM yyyy 'à' HH:mm", { locale: fr })} {scheduledPost.error && ( <> • Erreur )}
))}
)} { }} isPublishing={false} readOnly={true} /> Êtes-vous sûr ? Cette action est irréversible. Cela supprimera définitivement cette publication de votre historique et des statistiques associées. Annuler { e.preventDefault(); confirmDelete(); }} disabled={deleteReelMutation.isPending} className="bg-destructive text-destructive-foreground hover:bg-destructive/90" > {deleteReelMutation.isPending ? "Suppression..." : "Supprimer"} ); }