import { useState, useCallback, useRef } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { useLocation } from "wouter"; import { Send, Sparkles, Image as ImageIcon, Calendar, Upload, Camera } from "lucide-react"; import { useDropzone } from "react-dropzone"; import Sidebar from "@/components/sidebar"; import TopBar from "@/components/topbar"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Textarea } from "@/components/ui/textarea"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useToast } from "@/hooks/use-toast"; import { apiRequest, queryClient } from "@/lib/queryClient"; import type { SocialPage, Media } from "@shared/schema"; export default function NewPost() { const [sidebarOpen, setSidebarOpen] = useState(false); const [, navigate] = useLocation(); const { toast } = useToast(); const cameraInputRef = useRef(null); const [productInfo, setProductInfo] = useState(''); const [selectedMedia, setSelectedMedia] = useState(null); const [selectedPages, setSelectedPages] = useState([]); const [scheduledDate, setScheduledDate] = useState(''); const [generatedVariants, setGeneratedVariants] = useState([]); const [postText, setPostText] = useState(''); const [postType, setPostType] = useState<'feed' | 'story' | 'both'>('feed'); const { data: pages = [] } = useQuery({ queryKey: ['/api/pages'], }); const { data: mediaList = [] } = useQuery({ queryKey: ['/api/media'], }); const uploadMutation = useMutation({ mutationFn: async (file: File) => { const formData = new FormData(); formData.append("file", file); const response = await fetch("/api/media/upload", { method: "POST", body: formData, }); if (!response.ok) throw new Error("Upload failed"); return response.json(); }, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["/api/media"] }); setSelectedMedia(data.id); toast({ title: "Succès", description: "Photo téléchargée avec succès", }); }, onError: () => { toast({ title: "Erreur", description: "Impossible de télécharger la photo", variant: "destructive", }); }, }); const onDrop = useCallback((acceptedFiles: File[]) => { if (acceptedFiles.length > 0) { uploadMutation.mutate(acceptedFiles[0]); } }, []); const { getRootProps, getInputProps, isDragActive, open } = useDropzone({ onDrop, accept: { "image/*": [".png", ".jpg", ".jpeg", ".gif"], "video/*": [".mp4", ".mov"], }, maxSize: 52428800, noClick: true, noKeyboard: true, }); const generateTextMutation = useMutation({ mutationFn: async (productInfoText: string) => { const productInfo = { name: productInfoText.match(/Produit:\s*(.+?)(?:\n|$)/i)?.[1] || productInfoText.match(/Nom:\s*(.+?)(?:\n|$)/i)?.[1] || productInfoText, price: productInfoText.match(/Prix:\s*(.+?)(?:\n|$)/i)?.[1] || "", description: productInfoText.match(/Description:\s*(.+?)(?:\n|$)/i)?.[1] || "", features: productInfoText.match(/Caractéristiques:\s*(.+?)(?:\n|$)/i)?.[1]?.split(",") || [], }; const response = await apiRequest('POST', '/api/ai/generate', productInfo); return response.json(); }, onSuccess: (data: any) => { const variants = data.variants || []; setGeneratedVariants(variants); toast({ title: "Texte généré", description: `${variants.length} variations créées`, }); queryClient.invalidateQueries({ queryKey: ['/api/ai/generations'] }); }, onError: () => { toast({ title: "Erreur", description: "Impossible de générer le texte", variant: "destructive", }); }, }); const createPostMutation = useMutation({ mutationFn: (data: any) => apiRequest('POST', '/api/posts', data), onSuccess: () => { toast({ title: "Publication créée", description: "La publication a été créée avec succès", }); navigate('/'); }, onError: () => { toast({ title: "Erreur", description: "Impossible de créer la publication", variant: "destructive", }); }, }); const handleGenerateText = () => { if (!productInfo.trim()) { toast({ title: "Information manquante", description: "Veuillez saisir les informations du produit", variant: "destructive", }); return; } generateTextMutation.mutate(productInfo); }; const handleCreatePost = () => { if (!postText.trim()) { toast({ title: "Contenu requis", description: "Veuillez saisir le contenu de la publication", variant: "destructive", }); return; } if (selectedPages.length === 0) { toast({ title: "Page requise", description: "Veuillez sélectionner au moins une page", variant: "destructive", }); return; } if ((postType === 'story' || postType === 'both') && !selectedMedia) { toast({ title: "Média requis", description: "Les stories nécessitent une image ou vidéo", variant: "destructive", }); return; } createPostMutation.mutate({ content: postText, scheduledFor: scheduledDate || undefined, mediaId: selectedMedia || undefined, pageIds: selectedPages, postType, }); }; const handleUseVariant = (text: string) => { setPostText(text); toast({ title: "Texte sélectionné", description: "Le texte a été ajouté à votre publication", }); }; const handleCameraCapture = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { uploadMutation.mutate(file); } }; return (
{sidebarOpen && (
setSidebarOpen(false)} /> )}
setSidebarOpen(false)} />
setSidebarOpen(!sidebarOpen)} />

Nouvelle publication

Créez et planifiez une nouvelle publication

Média Sélectionnez ou uploadez une image/vidéo
{mediaList.length === 0 ? (

{isDragActive ? "Déposez votre photo ici" : "Aucun média disponible"}

) : (
{mediaList.map((media) => ( ))}
)}
Contenu Informations du produit ou message