import { useState } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { useLocation } from "wouter"; import { Send, Sparkles, Image as ImageIcon, Calendar } from "lucide-react"; 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 [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 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", }); }; return (
{sidebarOpen && (
setSidebarOpen(false)} /> )}
setSidebarOpen(!sidebarOpen)} />

Nouvelle publication

Créez et planifiez une nouvelle publication

Contenu Informations du produit ou message