import { useState } from "react"; import { Button } from "@/components/ui/button"; import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, Trash2, Edit } from "lucide-react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { useToast } from "@/hooks/use-toast"; import { apiRequest, queryClient } from "@/lib/queryClient"; import type { ScheduledPost } from "@shared/schema"; import { SiFacebook, SiInstagram } from "react-icons/si"; import EditScheduledPostDialog from "./edit-scheduled-post-dialog"; export default function CalendarView() { const [currentDate, setCurrentDate] = useState(new Date()); const [editDialogOpen, setEditDialogOpen] = useState(false); const [selectedPost, setSelectedPost] = useState(null); const { toast } = useToast(); const { data: scheduledPosts = [] } = useQuery({ queryKey: ["/api/scheduled-posts"], }); const deletePostMutation = useMutation({ mutationFn: (postId: string) => apiRequest('DELETE', `/api/scheduled-posts/${postId}`, {}), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['/api/scheduled-posts'] }); toast({ title: "Publication supprimée", description: "La publication programmée a été supprimée avec succès", }); }, onError: () => { toast({ title: "Erreur", description: "Impossible de supprimer la publication", variant: "destructive", }); }, }); const handleDeletePost = (postId: string, e: React.MouseEvent) => { e.stopPropagation(); if (confirm("Êtes-vous sûr de vouloir supprimer cette publication programmée ?")) { deletePostMutation.mutate(postId); } }; const handleEditPost = (post: any, e: React.MouseEvent) => { e.stopPropagation(); setSelectedPost(post); setEditDialogOpen(true); }; const getPostsForDate = (date: Date) => { return (scheduledPosts || []).filter(post => { if (!post.scheduledAt) return false; const postDate = new Date(post.scheduledAt); return postDate.toDateString() === date.toDateString(); }); }; const goToPreviousMonth = () => { setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1)); }; const goToNextMonth = () => { setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1)); }; const monthNames = [ "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" ]; const daysOfWeek = ["Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"]; const getDaysInMonth = (date: Date) => { const year = date.getFullYear(); const month = date.getMonth(); const firstDay = new Date(year, month, 1); const lastDay = new Date(year, month + 1, 0); const daysInMonth = lastDay.getDate(); const startingDayOfWeek = firstDay.getDay() === 0 ? 6 : firstDay.getDay() - 1; const days = []; for (let i = 0; i < startingDayOfWeek; i++) { const prevMonthDay = new Date(year, month, -startingDayOfWeek + i + 1); days.push({ date: prevMonthDay, isCurrentMonth: false }); } for (let i = 1; i <= daysInMonth; i++) { days.push({ date: new Date(year, month, i), isCurrentMonth: true }); } const remainingDays = 42 - days.length; for (let i = 1; i <= remainingDays; i++) { days.push({ date: new Date(year, month + 1, i), isCurrentMonth: false }); } return days; }; const days = getDaysInMonth(currentDate); const isToday = (date: Date) => { const today = new Date(); return date.toDateString() === today.toDateString(); }; return (

Calendrier

Visualisez vos publications planifiées

{monthNames[currentDate.getMonth()]} {currentDate.getFullYear()}
{daysOfWeek.map((day) => (
{day}
))}
{days.map((day, index) => (
{day.date.getDate()} {isToday(day.date) && ( Aujourd'hui )}
{(() => { if (!day.isCurrentMonth) return null; const postsForDay = getPostsForDate(day.date); if (postsForDay.length === 0) return null; return (
{postsForDay.slice(0, 2).map((post: any, idx) => { 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 (
{time}
{pageName}
{isPending && (
)}
); })} {postsForDay.length > 2 && (
+{postsForDay.length - 2} autre(s)
)}
); })()}
))}
Programmé
Publié
{selectedPost && ( )}
); }