import { useEffect, useRef, useMemo } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { useGameStore } from '../../stores/gameStore' import { useThemeStyles } from '../../themes/ThemeProvider' const REACTION_DURATION_MS = 2000 // Auto-remove after 2 seconds export default function ReactionOverlay() { const { reactions, removeReaction } = useGameStore() const { config } = useThemeStyles() const timeoutsRef = useRef>>(new Map()) // Auto-remove reactions after duration useEffect(() => { reactions.forEach((reaction) => { // Only set timeout if we haven't already set one for this reaction if (!timeoutsRef.current.has(reaction.id)) { const timeout = setTimeout(() => { removeReaction(reaction.id) timeoutsRef.current.delete(reaction.id) }, REACTION_DURATION_MS) timeoutsRef.current.set(reaction.id, timeout) } }) // Cleanup removed reactions return () => { timeoutsRef.current.forEach((timeout) => clearTimeout(timeout)) } }, [reactions, removeReaction]) // Pre-calculate random positions for each reaction to avoid recalculation on re-render const reactionPositions = useMemo(() => { const positions = new Map() reactions.forEach((reaction) => { if (!positions.has(reaction.id)) { positions.set(reaction.id, 10 + Math.random() * 80) } }) return positions }, [reactions]) return (
{reactions.map((reaction) => { // Use pre-calculated random horizontal position (10% to 90% of screen width) const randomX = reactionPositions.get(reaction.id) || 50 return ( {/* Emoji */} {reaction.emoji} {/* Player name badge */} {reaction.player_name} ) })}
) }