import { format, isSameDay, parseISO } from "date-fns"; import { fr } from "date-fns/locale"; import { Edit, Trash2, ChevronDown, ChevronUp, Eye, Image, Smartphone } from "lucide-react"; import { SiFacebook, SiInstagram } from "react-icons/si"; import { Button } from "@/components/ui/button"; import { useState } from "react"; interface CalendarListViewProps { scheduledPosts: any[]; onEditPost: (post: any) => void; onDeletePost: (postId: string) => void; onPreviewPost: (post: any) => void; } export default function CalendarListView({ scheduledPosts, onEditPost, onDeletePost, onPreviewPost }: CalendarListViewProps) { const [expandedDates, setExpandedDates] = useState>(new Set()); const getPostTypeIcon = (postType: string) => { if (postType === 'feed') { return ; } else if (postType === 'story') { return ; } else if (postType === 'both') { return (
); } return null; }; // Group posts by date const groupedPosts = scheduledPosts.reduce((acc: { [key: string]: any[] }, post) => { if (!post.scheduledAt) return acc; const date = format(parseISO(post.scheduledAt), "yyyy-MM-dd"); if (!acc[date]) { acc[date] = []; } acc[date].push(post); return acc; }, {}); // Sort dates - put today first, then future dates in ascending order, then past dates const today = format(new Date(), "yyyy-MM-dd"); const sortedDates = Object.keys(groupedPosts).sort((a, b) => { // Today always comes first if (a === today && b !== today) return -1; if (b === today && a !== today) return 1; // Both are not today - separate future and past dates const aIsFuture = a > today; const bIsFuture = b > today; // Both are future: ascending chronological order (soonest first) if (aIsFuture && bIsFuture) { return a.localeCompare(b); } // Both are past: ascending chronological order (most recent past first) if (!aIsFuture && !bIsFuture) { return b.localeCompare(a); } // One is future, one is past: future comes first return aIsFuture ? -1 : 1; }); const toggleDate = (date: string) => { const newExpanded = new Set(expandedDates); if (newExpanded.has(date)) { newExpanded.delete(date); } else { newExpanded.add(date); } setExpandedDates(newExpanded); }; const isToday = (dateStr: string) => { return isSameDay(parseISO(dateStr), new Date()); }; return (
{sortedDates.length === 0 ? (
Aucune publication programmée
) : ( sortedDates.map((dateStr) => { const posts = groupedPosts[dateStr]; const isExpanded = expandedDates.has(dateStr); const dateObj = parseISO(dateStr); const isDateToday = isToday(dateStr); return (
{isExpanded && (
{posts.map((post: any) => { const time = post.scheduledAt ? new Date(post.scheduledAt).toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit', }) : ''; const isPending = !post.publishedAt; const isPublished = !!post.publishedAt; const pageName = post.page?.pageName || 'Page inconnue'; const platform = post.page?.platform || 'facebook'; const PlatformIcon = platform === 'instagram' ? SiInstagram : SiFacebook; return (
{getPostTypeIcon(post.postType)} {time} {isPending ? 'Programmé' : 'Publié'}
{pageName}
{isPending && ( <> )}
); })}
)}
); }) )}
); }