import React, { useEffect, useState, useMemo, useRef } from 'react'; import { motion, useReducedMotion } from 'framer-motion'; import { useAudio } from '../../context/AudioContext'; interface EmberLoaderProps { onLoadingComplete: () => void; } const EmberLoader: React.FC = ({ onLoadingComplete }) => { const [progress, setProgress] = useState(0); const { playTickSound } = useAudio(); const prefersReducedMotion = useReducedMotion(); // Ref to track the last played tick integer to prevent spamming within the same integer frame const lastTickRef = useRef(0); // Configuration const DURATION = 8000; // 8 seconds total loading time useEffect(() => { const startTime = Date.now(); const updateProgress = () => { const now = Date.now(); const elapsed = now - startTime; if (elapsed < DURATION) { // Organic easing: slow start, fast middle, slow end (easeInOutCubic) const t = elapsed / DURATION; const ease = t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; const currentProgress = Math.min(100, Math.round(ease * 100)); // Audio Logic: Play tick if integer value changed if (currentProgress > lastTickRef.current) { playTickSound(); lastTickRef.current = currentProgress; } setProgress(currentProgress); requestAnimationFrame(updateProgress); } else { setProgress(100); setTimeout(onLoadingComplete, 800); } }; const animationHandle = requestAnimationFrame(updateProgress); return () => cancelAnimationFrame(animationHandle); }, [onLoadingComplete, playTickSound]); // --- Micro Ember Drift (Foreground) --- // "Only the tiny chilli particles move. Direction: slow upward + slight random sideways. Speed: 1–2px per second." const particles = useMemo(() => Array.from({ length: 25 }).map((_, i) => ({ id: i, left: Math.random() * 100, // Random horizontal position bottom: Math.random() * 80, // Start mostly lower/middle size: Math.random() * 2 + 1, // 1-3px (Tiny) duration: Math.random() * 4 + 4, // 4-8s duration (Slow) xOffset: (Math.random() - 0.5) * 30, // Slight sideways drift delay: Math.random() * 2, })), []); return ( {/* 1. Clean Base Background (No texture/stripes) */}
{!prefersReducedMotion && ( { (e.currentTarget as HTMLImageElement).style.display = 'none'; }} /> )}
{/* 2. Micro Ember Drift (Foreground) */}
{particles.map((p) => ( ))}
{/* Red Atmospheric Glow (Bottom) */}
{/* Bottom Logic: Doto Fonts / Percentage */} {/* "No loading text" interpreted as removing labels, keeping only the metric */}
{progress.toString().padStart(3, '0')} %
{/* Brand Name - Centered & Adjusted Size (Responsive) */} {/* Must be one line, sync with background */}

C.S. PATTANSHETTI

{/* Elegant divider */}
); }; export default EmberLoader;