"use client"; import Image from "next/image"; import { Dispatch, SetStateAction, useState } from "react"; import Confetti from "react-confetti"; import { useWindowSize } from "react-use"; interface IProps { setWinners: Dispatch>; } const SpinWheel = ({ setWinners }: IProps) => { const [isSpinning, setIsSpinning] = useState(false); const [isCountingDown, setIsCountingDown] = useState(false); const [countdown, setCountdown] = useState(5); const [rotation, setRotation] = useState(0); const [showConfetti, setShowConfetti] = useState(false); const { width, height } = useWindowSize(); const triggerConfetti = () => { setShowConfetti(true); setTimeout(() => { setShowConfetti(false); setCountdown(5); // Reset countdown after confetti }, 6000); // Hide confetti after 6 seconds }; const startCountdown = () => { setIsCountingDown(true); // Start countdown state let currentCountdown = 5; const countdownInterval = setInterval(() => { setCountdown(currentCountdown--); // Update countdown if (currentCountdown < 0) { clearInterval(countdownInterval); // Clear countdown interval setIsCountingDown(false); // End countdown spinWheel(); // Trigger spin after countdown } }, 1000); }; const spinWheel = () => { if (isSpinning) return; setIsSpinning(true); // Start spinning const totalSpin = rotation + 360 * 10 + Math.random() * 360; // 10 full rotations + random offset setRotation(totalSpin); // Update rotation to a new value setTimeout(() => { setIsSpinning(false); setRotation((prev) => prev % 360); // Normalize the rotation setWinners((prev) => [...prev, 1]); triggerConfetti(); // Show confetti after spinning }, 5000); // Spin duration }; const handleSpinClick = () => { if (!isSpinning && !isCountingDown) { startCountdown(); // Start the countdown first } }; return (
{showConfetti && (
)}
{/* Wheel triangle */} wheel {/* Wheel */}
wheel
{/* Countdown */}
{countdown}
{/* Spin Button */}
); }; export default SpinWheel;