"use client"; import React, { useState, useEffect } from "react"; import { X, Wand2, Edit3, Sparkles, Loader2, CheckCircle2, AlertCircle } from "lucide-react"; import { editAdCopy } from "@/lib/api/endpoints"; import type { AdCreativeDB } from "@/types/api"; import { Button } from "@/components/ui/Button"; import { Input } from "@/components/ui/Input"; import { Card, CardContent } from "@/components/ui/Card"; interface EditCopyModalProps { isOpen: boolean; onClose: () => void; adId: string; ad?: AdCreativeDB | null; field: "title" | "headline" | "primary_text" | "description" | "body_story" | "cta"; fieldLabel: string; currentValue: string; onSuccess?: () => void; } type EditMode = "manual" | "ai"; export const EditCopyModal: React.FC = ({ isOpen, onClose, adId, ad, field, fieldLabel, currentValue, onSuccess, }) => { const [mode, setMode] = useState("manual"); const [editedValue, setEditedValue] = useState(currentValue); const [aiSuggestion, setAiSuggestion] = useState(""); const [userSuggestion, setUserSuggestion] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); useEffect(() => { if (isOpen) { setEditedValue(currentValue); setAiSuggestion(""); setUserSuggestion(""); setError(null); setSuccess(false); setMode("manual"); } }, [isOpen, currentValue]); const handleManualSave = async () => { if (!editedValue.trim()) { setError("This field cannot be empty"); return; } setIsLoading(true); setError(null); try { await editAdCopy({ ad_id: adId, field, value: editedValue, mode: "manual", }); setSuccess(true); setTimeout(() => { onSuccess?.(); onClose(); }, 1000); } catch (err: any) { setError(err.message || "Failed to update. Please try again."); } finally { setIsLoading(false); } }; const handleAIGenerate = async () => { setIsLoading(true); setError(null); try { const result = await editAdCopy({ ad_id: adId, field, value: currentValue, mode: "ai", user_suggestion: userSuggestion || undefined, }); setAiSuggestion(result.edited_value || ""); } catch (err: any) { setError(err.message || "Failed to generate AI suggestion. Please try again."); } finally { setIsLoading(false); } }; const handleAIApply = async () => { if (!aiSuggestion.trim()) { setError("No AI suggestion available"); return; } setIsLoading(true); setError(null); try { await editAdCopy({ ad_id: adId, field, value: aiSuggestion, mode: "manual", }); setSuccess(true); setTimeout(() => { onSuccess?.(); onClose(); }, 1000); } catch (err: any) { setError(err.message || "Failed to apply AI suggestion. Please try again."); } finally { setIsLoading(false); } }; if (!isOpen) return null; return (
{/* Header */}

Edit {fieldLabel}

Update your ad copy

{/* Mode Toggle */}
{/* Manual Edit Mode */} {mode === "manual" && (