'use client' import React, { useState, useEffect, useCallback } from 'react' interface CoinAnimationProps { amount: number trigger: number // increment to trigger animation } interface FloatingCoin { id: number x: number delay: number } export function CoinAnimation({ amount, trigger }: CoinAnimationProps) { const [coins, setCoins] = useState([]) const [total, setTotal] = useState(0) const [showBounce, setShowBounce] = useState(false) useEffect(() => { if (trigger === 0) return // Create floating coins const newCoins: FloatingCoin[] = Array.from({ length: Math.min(amount, 5) }, (_, i) => ({ id: Date.now() + i, x: Math.random() * 60 - 30, delay: i * 100, })) setCoins(newCoins) setShowBounce(true) // Update total after animation setTimeout(() => { setTotal((prev) => prev + amount) setShowBounce(false) }, 800) // Clean up coins setTimeout(() => setCoins([]), 1500) }, [trigger, amount]) return (
{/* Coin icon + total */}
🪙 {total}
{/* Floating coins animation */} {coins.map((coin) => ( 🪙 ))}
) } /** Hook to manage coin rewards */ export function useCoinRewards() { const [totalCoins, setTotalCoins] = useState(0) const [triggerCount, setTriggerCount] = useState(0) const [lastReward, setLastReward] = useState(0) const awardCoins = useCallback((amount: number) => { setLastReward(amount) setTriggerCount((c) => c + 1) setTotalCoins((t) => t + amount) }, []) return { totalCoins, triggerCount, lastReward, awardCoins } }