"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 = ({ progress, currentIndex = 0, totalCount = 0, generationStartTime, message, }) => { const [currentMessageIndex, setCurrentMessageIndex] = useState(0); const [elapsedTime, setElapsedTime] = useState(0); const [estimatedTimeRemaining, setEstimatedTimeRemaining] = useState(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 (
{/* Header with animated icon */}
{isComplete ? (
) : (
)}

{isComplete ? "Batch Generation Complete!" : "Generating Batch Ads"}

{isComplete ? "All ads are ready!" : getCurrentMessage()}

{elapsedTime > 30 && !isComplete && (

{Math.floor(elapsedTime / 60)}m {elapsedTime % 60}s elapsed

)}
{estimatedTimeRemaining !== null && !isComplete && (
~{estimatedTimeRemaining}s

remaining

)} {!isComplete && ( )}
{/* Batch Stats */} {totalCount > 0 && (

Total Ads

{totalCount}

Current

{currentAdNumber}/{totalCount}

)} {/* Progress Bar */}
Overall Progress {Math.round(clampedProgress)}%
{totalCount > 0 && currentIndex >= 0 && (

{currentAdNumber} of {totalCount} ads completed

)}
{/* Success State */} {isComplete && (

Batch generation completed successfully!

{totalCount > 0 ? `${totalCount} ads are ready!` : "All ads are ready to use"}

)}
); };