File size: 9,766 Bytes
8158a5c
 
 
 
 
3c7508e
 
 
8158a5c
3c7508e
 
8158a5c
3c7508e
8158a5c
 
 
 
 
 
 
 
 
 
4a56a0b
8158a5c
 
 
4a56a0b
 
8158a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c7508e
8158a5c
 
 
 
 
 
 
 
 
 
 
 
3c7508e
8158a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c7508e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8158a5c
3c7508e
 
 
 
 
 
8158a5c
3c7508e
8158a5c
 
3c7508e
 
 
 
 
 
 
 
 
8158a5c
 
3c7508e
 
 
 
 
 
 
 
 
 
 
8158a5c
3c7508e
 
 
 
 
 
 
 
8158a5c
 
3c7508e
8158a5c
3c7508e
 
 
 
 
 
 
 
 
 
 
 
4a56a0b
3c7508e
 
 
 
8158a5c
 
3c7508e
 
 
 
 
 
 
 
 
 
4a56a0b
3c7508e
 
8158a5c
 
3c7508e
 
 
 
8158a5c
 
 
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
"use client";

import React, { useState, useEffect } from "react";
import { Card, CardContent } from "@/components/ui/Card";
import { ProgressBar } from "@/components/ui/ProgressBar";
import {
  Sparkles,
  CheckCircle2,
  Zap,
  Package,
  X
} from "lucide-react";
import { useGenerationStore } from "@/store/generationStore";

interface BatchProgressProps {
  progress: number; // 0-100
  currentIndex?: number; // Current ad being generated (0-based)
  totalCount?: number; // Total number of ads
  generationStartTime?: number | null;
  message?: string;
}

const BATCH_MESSAGES = [
  "Generating your ad set...",
  "Crafting compelling visuals...",
  "Building your ad collection...",
  "Almost there! Finalizing your ads...",
  "Perfecting each creative...",
  "Lining up scroll-stopping ads...",
  "Great things take time - we're crafting perfection!",
] as const;

const ENGAGING_MESSAGES = [
  "Batch generation may take a while, but great things are worth waiting for!",
  "Almost there! We're putting the finishing touches on your batch.",
  "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 batch is almost ready!",
  "We're working hard to make this perfect for you!",
  "Great things take time - we're crafting your batch!",
  "Almost done! We're making sure everything is just right.",
] as const;

export const BatchProgressComponent: React.FC<BatchProgressProps> = ({
  progress,
  currentIndex = 0,
  totalCount = 0,
  generationStartTime,
  message,
}) => {
  const [currentMessageIndex, setCurrentMessageIndex] = useState(0);
  const [elapsedTime, setElapsedTime] = useState(0);
  const [estimatedTimeRemaining, setEstimatedTimeRemaining] = useState<number | null>(null);
  const reset = useGenerationStore((state) => state.reset);

  const clampedProgress = Math.min(100, Math.max(0, progress));
  const isComplete = clampedProgress >= 100;
  const isStuckAtHighProgress = clampedProgress >= 85 && !isComplete;
  const currentAdNumber = currentIndex + 1;

  // Calculate elapsed time
  useEffect(() => {
    if (generationStartTime && !isComplete) {
      const interval = setInterval(() => {
        const elapsed = Math.floor((Date.now() - generationStartTime) / 1000);
        setElapsedTime(elapsed);

        // Estimate time remaining based on progress
        if (clampedProgress > 5 && clampedProgress < 100) {
          const rate = clampedProgress / elapsed; // % per second
          const remaining = (100 - clampedProgress) / rate;
          setEstimatedTimeRemaining(Math.max(0, Math.ceil(remaining)));
        }
      }, 1000);
      return () => clearInterval(interval);
    }
  }, [generationStartTime, isComplete, clampedProgress]);

  // 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 batch messages periodically
  useEffect(() => {
    if (!isComplete && !isStuckAtHighProgress) {
      const interval = setInterval(() => {
        setCurrentMessageIndex((prev) => (prev + 1) % BATCH_MESSAGES.length);
      }, 4000); // Change message every 4 seconds
      return () => clearInterval(interval);
    }
  }, [isComplete, isStuckAtHighProgress]);

  // Get current message
  const getCurrentMessage = () => {
    if (message) return message;
    if (isStuckAtHighProgress) {
      return ENGAGING_MESSAGES[currentMessageIndex];
    }
    if (totalCount > 0 && currentIndex >= 0) {
      return `Generating ad ${currentAdNumber} of ${totalCount}...`;
    }
    return BATCH_MESSAGES[currentMessageIndex];
  };

  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>
                ) : (
                  <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">
                      <Package className="h-5 w-5 text-white animate-pulse" />
                    </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 ? "Batch Generation Complete!" : "Generating Batch Ads"}
                  </h3>
                  <p className="text-sm text-gray-600 mt-0.5 transition-all duration-500">
                    {isComplete ? "All ads are ready!" : getCurrentMessage()}
                  </p>
                  {elapsedTime > 30 && !isComplete && (
                    <p className="text-xs text-gray-500 mt-1">
                      {Math.floor(elapsedTime / 60)}m {elapsedTime % 60}s elapsed
                    </p>
                  )}
                </div>
              </div>
              {estimatedTimeRemaining !== null && !isComplete && (
                <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>~{estimatedTimeRemaining}s</span>
                  </div>
                  <p className="text-xs text-gray-500">remaining</p>
                </div>
              )}
              {!isComplete && (
                <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>

            {/* Batch Stats */}
            {totalCount > 0 && (
              <div className="grid grid-cols-3 gap-4">
                <div className="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-4 border border-blue-200">
                  <div className="flex items-center space-x-2 mb-1">
                    <Package className="h-4 w-4 text-blue-600" />
                    <p className="text-xs font-semibold text-gray-600">Total Ads</p>
                  </div>
                  <p className="text-2xl font-bold bg-gradient-to-r from-blue-600 to-cyan-600 bg-clip-text text-transparent">
                    {totalCount}
                  </p>
                </div>
                <div className="bg-gradient-to-br from-pink-50 to-purple-50 rounded-xl p-4 border border-pink-200">
                  <div className="flex items-center space-x-2 mb-1">
                    <Sparkles className="h-4 w-4 text-pink-600" />
                    <p className="text-xs font-semibold text-gray-600">Current</p>
                  </div>
                  <p className="text-2xl font-bold bg-gradient-to-r from-pink-600 to-purple-600 bg-clip-text text-transparent">
                    {currentAdNumber}/{totalCount}
                  </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(clampedProgress)}%
                </span>
              </div>
              <ProgressBar
                progress={clampedProgress}
                showPercentage={false}
              />
                  {totalCount > 0 && currentIndex >= 0 && (
                <p className="text-xs text-gray-500 text-center mt-2">
                  {currentAdNumber} of {totalCount} ads completed
                </p>
              )}
            </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">
                      Batch generation completed successfully!
                    </p>
                    <p className="text-xs text-green-700 mt-0.5">
                      {totalCount > 0 ? `${totalCount} ads are ready!` : "All ads are ready to use"}
                    </p>
                  </div>
                </div>
              </div>
            )}
          </div>
        </CardContent>
      </Card>
    </div>
  );
};