File size: 12,636 Bytes
f201243
 
8158a5c
f201243
 
 
3c7508e
 
 
 
 
f201243
 
3c7508e
 
f201243
 
3c7508e
f201243
 
 
8158a5c
f201243
 
 
3c7508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f201243
 
8158a5c
 
 
 
 
 
 
 
 
 
 
 
f201243
 
8158a5c
f201243
8158a5c
 
3c7508e
8158a5c
f201243
 
 
 
 
 
 
 
 
addcf34
 
f201243
 
 
8158a5c
f201243
8158a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f201243
 
3c7508e
8158a5c
 
 
 
3c7508e
f201243
 
8158a5c
f201243
 
 
 
 
8158a5c
 
 
3c7508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f201243
3c7508e
 
 
 
 
 
f201243
3c7508e
f201243
 
3c7508e
 
 
 
 
 
 
 
 
f201243
3c7508e
 
 
 
 
 
 
 
 
 
f201243
3c7508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f201243
 
 
3c7508e
 
 
 
 
 
 
f201243
3c7508e
 
 
 
 
f201243
3c7508e
 
 
 
 
 
 
 
 
 
 
f201243
 
 
 
3c7508e
f201243
3c7508e
 
 
 
 
 
 
 
 
 
 
 
 
f201243
 
3c7508e
 
 
 
8158a5c
f201243
 
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
"use client";

import React, { useState, useEffect } from "react";
import { Card, CardContent } from "@/components/ui/Card";
import { ProgressBar } from "@/components/ui/ProgressBar";
import { LoadingSpinner } from "@/components/ui/LoadingSpinner";
import {
  Sparkles,
  Image as ImageIcon,
  Database,
  CheckCircle2,
  AlertCircle,
  Wand2,
  Zap,
  X
} from "lucide-react";
import type { GenerationProgress } from "@/types";
import { useGenerationStore } from "@/store/generationStore";

interface GenerationProgressProps {
  progress: GenerationProgress;
  generationStartTime?: number | null;
}

const STEPS = [
  {
    key: "copy", label: "Crafting Copy", icon: Sparkles, color: "from-blue-500 to-cyan-500", messages: [
      "Brainstorming compelling headlines...",
      "Writing persuasive ad copy...",
      "Polishing the perfect message...",
      "Adding psychological triggers...",
    ]
  },
  {
    key: "image", label: "Generating Images", icon: ImageIcon, color: "from-cyan-500 to-pink-500", messages: [
      "Creating stunning visuals...",
      "Bringing your vision to life...",
      "Rendering high-quality images...",
      "Adding creative flair...",
      "Generation may take a while...",
      "Almost there!",
      "Working on the perfect image...",
      "This is worth the wait...",
      "Crafting something amazing...",
      "Just a few more moments...",
    ]
  },
  {
    key: "saving", label: "Saving", icon: Database, color: "from-pink-500 to-purple-500", messages: [
      "Storing your creative...",
      "Securing your masterpiece...",
      "Almost done...",
      "Finalizing everything...",
    ]
  },
] as const;

// Engaging messages for when stuck at high progress
const ENGAGING_MESSAGES = [
  "Generation may take a while, but great things are worth waiting for!",
  "Almost there! We're putting the finishing touches on your ad.",
  "Hang tight! We're creating something amazing for you.",
  "This is taking a bit longer, but we're ensuring top quality!",
  "Just a few more moments... Your ad is almost ready!",
  "We're working hard to make this perfect for you!",
  "Great things take time - we're crafting your masterpiece!",
  "Almost done! We're making sure everything is just right.",
] as const;

export const GenerationProgressComponent: React.FC<GenerationProgressProps> = ({
  progress,
  generationStartTime,
}) => {
  const [currentMessageIndex, setCurrentMessageIndex] = useState(0);
  const [elapsedTime, setElapsedTime] = useState(0);
  const reset = useGenerationStore((state) => state.reset);

  const stepProgress = {
    idle: 0,
    copy: 33,
    image: 66,
    saving: 90,
    complete: 100,
    error: 0,
  };

  // Use nullish coalescing (??) instead of || to properly handle 0 as a valid progress value
  const currentProgress = progress.progress ?? stepProgress[progress.step];
  const currentStepIndex = STEPS.findIndex(s => s.key === progress.step);
  const isComplete = progress.step === "complete";
  const isError = progress.step === "error";
  const isStuckAtHighProgress = currentProgress >= 85 && !isComplete && !isError;

  // Calculate elapsed time
  useEffect(() => {
    if (generationStartTime && !isComplete && !isError) {
      const interval = setInterval(() => {
        setElapsedTime(Math.floor((Date.now() - generationStartTime) / 1000));
      }, 1000);
      return () => clearInterval(interval);
    }
  }, [generationStartTime, isComplete, isError]);

  // Rotate messages when stuck at high progress
  useEffect(() => {
    if (isStuckAtHighProgress) {
      const interval = setInterval(() => {
        setCurrentMessageIndex((prev) => (prev + 1) % ENGAGING_MESSAGES.length);
      }, 5000); // Change message every 5 seconds
      return () => clearInterval(interval);
    }
  }, [isStuckAtHighProgress]);

  // Rotate step messages periodically
  useEffect(() => {
    if (!isComplete && !isError && !isStuckAtHighProgress) {
      const step = STEPS.find(s => s.key === progress.step);
      if (step && step.messages.length > 1) {
        const interval = setInterval(() => {
          setCurrentMessageIndex((prev) => (prev + 1) % step.messages.length);
        }, 4000); // Change message every 4 seconds
        return () => clearInterval(interval);
      }
    }
  }, [progress.step, isComplete, isError, isStuckAtHighProgress]);

  // Get message for current step
  const getStepMessage = () => {
    if (progress.message) return progress.message;

    // If stuck at high progress, show engaging messages
    if (isStuckAtHighProgress) {
      return ENGAGING_MESSAGES[currentMessageIndex];
    }

    const step = STEPS.find(s => s.key === progress.step);
    if (step && step.messages.length > 0) {
      return step.messages[currentMessageIndex % step.messages.length];
    }
    return "Processing...";
  };

  return (
    <div className="sticky top-20 z-30 mb-6 animate-scale-in">
      <Card variant="glass" className="overflow-hidden shadow-xl border-2 border-blue-200/50 backdrop-blur-xl">
        <CardContent className="pt-6">
          <div className="space-y-6">
            {/* Header with animated icon */}
            <div className="flex items-center justify-between">
              <div className="flex items-center space-x-4">
                {isComplete ? (
                  <div className="relative">
                    <div className="absolute inset-0 bg-green-500 rounded-full animate-ping opacity-75"></div>
                    <CheckCircle2 className="h-8 w-8 text-green-500 relative z-10" />
                  </div>
                ) : isError ? (
                  <AlertCircle className="h-8 w-8 text-red-500 animate-pulse" />
                ) : (
                  <div className="relative">
                    <div className="absolute inset-0 bg-gradient-to-r from-blue-500 to-cyan-500 rounded-full animate-ping opacity-20"></div>
                    <div className="relative bg-gradient-to-r from-blue-500 to-cyan-500 rounded-full p-2">
                      <Wand2 className="h-5 w-5 text-white animate-spin-slow" />
                    </div>
                  </div>
                )}
                <div>
                  <h3 className="text-lg font-bold bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
                    {isComplete ? "Generation Complete!" : isError ? "Generation Failed" : "Creating Your Ad"}
                  </h3>
                  <p className="text-sm text-gray-600 mt-0.5 transition-all duration-500">
                    {isComplete ? "Your ad is ready!" : isError ? "Something went wrong" : getStepMessage()}
                  </p>
                  {elapsedTime > 30 && !isComplete && !isError && (
                    <p className="text-xs text-gray-500 mt-1">
                      {Math.floor(elapsedTime / 60)}m {elapsedTime % 60}s elapsed
                    </p>
                  )}
                </div>
              </div>
              {progress.estimatedTimeRemaining && !isComplete && !isError && (
                <div className="text-right">
                  <div className="flex items-center space-x-1 text-sm font-semibold text-gray-700">
                    <Zap className="h-4 w-4 text-yellow-500 animate-pulse" />
                    <span>~{Math.ceil(progress.estimatedTimeRemaining)}s</span>
                  </div>
                  <p className="text-xs text-gray-500">remaining</p>
                </div>
              )}
              {!isComplete && !isError && (
                <button
                  onClick={() => reset()}
                  className="p-2 hover:bg-red-50 text-gray-400 hover:text-red-500 rounded-full transition-colors group"
                  title="Cancel Generation"
                >
                  <X className="h-5 w-5 group-hover:rotate-90 transition-transform duration-300" />
                </button>
              )}
            </div>

            {/* Step Indicators */}
            {!isComplete && !isError && (
              <div className="flex items-center justify-between relative">
                {/* Progress line */}
                <div className="absolute top-5 left-0 right-0 h-0.5 bg-gray-200 -z-10">
                  <div
                    className="h-full bg-gradient-to-r from-blue-500 via-cyan-500 to-pink-500 transition-all duration-500 ease-out"
                    style={{ width: `${currentProgress}%` }}
                  />
                </div>

                {STEPS.map((step, index) => {
                  const StepIcon = step.icon;
                  const isActive = progress.step === step.key;
                  const isCompleted = currentStepIndex > index;
                  const isUpcoming = currentStepIndex < index;

                  return (
                    <div key={step.key} className="flex flex-col items-center flex-1">
                      <div className={`relative w-10 h-10 rounded-full flex items-center justify-center transition-all duration-300 ${isActive
                          ? `bg-gradient-to-r ${step.color} shadow-lg scale-110 animate-pulse`
                          : isCompleted
                            ? "bg-gradient-to-r from-green-500 to-emerald-500 shadow-md"
                            : "bg-gray-200"
                        }`}>
                        {isActive ? (
                          <div className="text-white">
                            <LoadingSpinner size="sm" />
                          </div>
                        ) : isCompleted ? (
                          <CheckCircle2 className="h-5 w-5 text-white" />
                        ) : (
                          <StepIcon className={`h-5 w-5 ${isUpcoming ? "text-gray-400" : "text-white"}`} />
                        )}
                        {isActive && (
                          <div className={`absolute inset-0 rounded-full bg-gradient-to-r ${step.color} animate-ping opacity-75`}></div>
                        )}
                      </div>
                      <p className={`text-xs font-medium mt-2 text-center ${isActive
                          ? "text-gray-900 font-bold"
                          : isCompleted
                            ? "text-green-600"
                            : "text-gray-400"
                        }`}>
                        {step.label}
                      </p>
                    </div>
                  );
                })}
              </div>
            )}

            {/* Progress Bar */}
            <div className="space-y-2">
              <div className="flex justify-between items-center">
                <span className="text-sm font-semibold text-gray-700">Overall Progress</span>
                <span className="text-sm font-bold bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
                  {Math.round(currentProgress)}%
                </span>
              </div>
              <ProgressBar
                progress={currentProgress}
                showPercentage={false}
              />
            </div>

            {/* Success State */}
            {isComplete && (
              <div className="mt-4 p-4 bg-gradient-to-r from-green-50 to-emerald-50 border-2 border-green-200 rounded-xl animate-scale-in">
                <div className="flex items-center space-x-3">
                  <CheckCircle2 className="h-6 w-6 text-green-600 flex-shrink-0" />
                  <div>
                    <p className="text-sm font-semibold text-green-900">
                      Ad generated successfully!
                    </p>
                    <p className="text-xs text-green-700 mt-0.5">
                      Your creative is ready to use
                    </p>
                  </div>
                </div>
              </div>
            )}

            {/* Error State */}
            {isError && (
              <div className="mt-4 p-4 bg-gradient-to-r from-red-50 to-pink-50 border-2 border-red-200 rounded-xl animate-scale-in">
                <div className="flex items-center space-x-3">
                  <AlertCircle className="h-6 w-6 text-red-600 flex-shrink-0" />
                  <div>
                    <p className="text-sm font-semibold text-red-900">
                      Generation failed
                    </p>
                    <p className="text-xs text-red-700 mt-0.5">
                      {progress.message || "An error occurred. Please try again."}
                    </p>
                  </div>
                </div>
              </div>
            )}
          </div>
        </CardContent>
      </Card>
    </div>
  );
};