'use client' import React, { useEffect, useState, useRef } from 'react'; import anime from 'animejs'; interface ProgressBarProps { duration: number; onComplete?: () => void; } export const ProgressBar = ({ duration, onComplete }: ProgressBarProps) => { const [progress, setProgress] = useState(0); const progressInterval = useRef(null); const startTime = useRef(Date.now()); const progressBarRef = useRef(null); const gradientRef = useRef(null); useEffect(() => { // Animate gradient if (progressBarRef.current) { gradientRef.current = anime({ targets: progressBarRef.current, backgroundPosition: ['0% 50%', '100% 50%'], duration: 2000, loop: true, direction: 'alternate', easing: 'linear' }); } const updateProgress = () => { const currentTime = Date.now(); const elapsedTime = currentTime - startTime.current; const calculatedProgress = (elapsedTime / (duration * 1000)) * 100; if (calculatedProgress >= 95) { setProgress(95); if (progressInterval.current) { clearInterval(progressInterval.current); } onComplete?.(); return; } // Slow down progress after 90% if (calculatedProgress > 90) { const remainingProgress = Math.min(99 - progress, 0.1); setProgress(prev => prev + remainingProgress); } else { setProgress(Math.min(calculatedProgress, 90)); } }; progressInterval.current = setInterval(updateProgress, 50); return () => { if (progressInterval.current) { clearInterval(progressInterval.current); } if (gradientRef.current) { gradientRef.current.pause(); } }; }, [duration, onComplete]); return (
{/* Processing... */} {Math.round(progress)}%
); };