import { useQuery } from "@tanstack/react-query"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Facebook, Instagram, Clock, CheckCircle2, XCircle, Eye } from "lucide-react"; 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; }; export default function RecentPublications() { const [previewModalOpen, setPreviewModalOpen] = useState(false); const [previewData, setPreviewData] = useState<{ postText: string; mediaIds: string[]; allMedia: Media[] }>({ postText: '', mediaIds: [], allMedia: [] }); const { toast } = useToast(); 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", }); } }; 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' ? ( ) : ( )}
{/* Content */}

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

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

{scheduledPost.error ? ( ) : ( )}
{scheduledPost.postType === 'feed' ? 'Publication' : scheduledPost.postType === 'story' ? 'Story' : scheduledPost.postType} • {format(new Date(scheduledPost.scheduledAt!), "d MMM yyyy 'à' HH:mm", { locale: fr })} {scheduledPost.error && ( <> • Erreur )}
))}
)} {}} isPublishing={false} readOnly={true} /> ); }