File size: 11,309 Bytes
addcf34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
"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<EditCopyModalProps> = ({
isOpen,
onClose,
adId,
ad,
field,
fieldLabel,
currentValue,
onSuccess,
}) => {
const [mode, setMode] = useState<EditMode>("manual");
const [editedValue, setEditedValue] = useState(currentValue);
const [aiSuggestion, setAiSuggestion] = useState("");
const [userSuggestion, setUserSuggestion] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(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 (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-sm animate-fade-in">
<Card className="w-full max-w-2xl max-h-[90vh] overflow-y-auto shadow-2xl border-2 border-blue-200 animate-scale-in">
<CardContent className="p-6">
{/* Header */}
<div className="flex items-center justify-between mb-6">
<div className="flex items-center gap-3">
<div className="p-2 bg-gradient-to-r from-blue-500 to-cyan-500 rounded-lg">
<Edit3 className="h-5 w-5 text-white" />
</div>
<div>
<h2 className="text-xl font-bold bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
Edit {fieldLabel}
</h2>
<p className="text-sm text-gray-500">Update your ad copy</p>
</div>
</div>
<button
onClick={onClose}
className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
>
<X className="h-5 w-5 text-gray-500" />
</button>
</div>
{/* Mode Toggle */}
<div className="flex gap-2 mb-6 p-1 bg-gray-100 rounded-lg">
<button
onClick={() => setMode("manual")}
className={`flex-1 flex items-center justify-center gap-2 px-4 py-2 rounded-md font-medium transition-all ${
mode === "manual"
? "bg-white text-blue-600 shadow-sm"
: "text-gray-600 hover:text-gray-900"
}`}
>
<Edit3 className="h-4 w-4" />
Manual Edit
</button>
<button
onClick={() => setMode("ai")}
className={`flex-1 flex items-center justify-center gap-2 px-4 py-2 rounded-md font-medium transition-all ${
mode === "ai"
? "bg-white text-blue-600 shadow-sm"
: "text-gray-600 hover:text-gray-900"
}`}
>
<Wand2 className="h-4 w-4" />
AI Edit
</button>
</div>
{/* Manual Edit Mode */}
{mode === "manual" && (
<div className="space-y-4">
<div>
<label className="block text-sm font-semibold text-gray-700 mb-2">
{fieldLabel}
</label>
<textarea
value={editedValue}
onChange={(e) => setEditedValue(e.target.value)}
className="w-full px-4 py-3 rounded-xl border-2 border-gray-300 bg-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all resize-none"
rows={field === "body_story" ? 8 : field === "description" ? 4 : 3}
placeholder={`Enter your ${fieldLabel.toLowerCase()}...`}
/>
</div>
{error && (
<div className="flex items-center gap-2 p-3 bg-red-50 border border-red-200 rounded-lg text-red-600">
<AlertCircle className="h-4 w-4" />
<span className="text-sm">{error}</span>
</div>
)}
{success && (
<div className="flex items-center gap-2 p-3 bg-green-50 border border-green-200 rounded-lg text-green-600">
<CheckCircle2 className="h-4 w-4" />
<span className="text-sm">Successfully updated!</span>
</div>
)}
<div className="flex gap-3">
<Button
onClick={handleManualSave}
isLoading={isLoading}
variant="primary"
className="flex-1"
>
Save Changes
</Button>
<Button
onClick={onClose}
variant="ghost"
disabled={isLoading}
>
Cancel
</Button>
</div>
</div>
)}
{/* AI Edit Mode */}
{mode === "ai" && (
<div className="space-y-4">
<div>
<label className="block text-sm font-semibold text-gray-700 mb-2">
Your Suggestion (Optional)
</label>
<textarea
value={userSuggestion}
onChange={(e) => setUserSuggestion(e.target.value)}
className="w-full px-4 py-3 rounded-xl border-2 border-gray-300 bg-white focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-all resize-none"
rows={3}
placeholder="e.g., Make it more emotional, Add urgency, Make it shorter, Focus on benefits..."
/>
<p className="text-xs text-gray-500 mt-1">
Tell AI how you'd like the {fieldLabel.toLowerCase()} improved
</p>
</div>
<div>
<label className="block text-sm font-semibold text-gray-700 mb-2">
Current {fieldLabel}
</label>
<div className="px-4 py-3 rounded-xl border-2 border-gray-200 bg-gray-50 text-gray-700">
{currentValue || <span className="text-gray-400">No content</span>}
</div>
</div>
<Button
onClick={handleAIGenerate}
isLoading={isLoading}
variant="primary"
className="w-full"
disabled={isLoading}
>
<Wand2 className="h-4 w-4 mr-2" />
{isLoading ? "Generating..." : "Generate AI Suggestion"}
</Button>
{aiSuggestion && (
<div className="space-y-3">
<div>
<label className="block text-sm font-semibold text-gray-700 mb-2">
AI Suggestion
</label>
<div className="px-4 py-3 rounded-xl border-2 border-blue-200 bg-blue-50 text-gray-700 whitespace-pre-line">
{aiSuggestion}
</div>
</div>
{error && (
<div className="flex items-center gap-2 p-3 bg-red-50 border border-red-200 rounded-lg text-red-600">
<AlertCircle className="h-4 w-4" />
<span className="text-sm">{error}</span>
</div>
)}
{success && (
<div className="flex items-center gap-2 p-3 bg-green-50 border border-green-200 rounded-lg text-green-600">
<CheckCircle2 className="h-4 w-4" />
<span className="text-sm">Successfully updated!</span>
</div>
)}
<div className="flex gap-3">
<Button
onClick={handleAIApply}
isLoading={isLoading}
variant="primary"
className="flex-1"
>
Apply AI Suggestion
</Button>
<Button
onClick={() => {
setAiSuggestion("");
setUserSuggestion("");
}}
variant="ghost"
disabled={isLoading}
>
Regenerate
</Button>
<Button
onClick={onClose}
variant="ghost"
disabled={isLoading}
>
Cancel
</Button>
</div>
</div>
)}
</div>
)}
</CardContent>
</Card>
</div>
);
};
|