Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

BonusToast.tsx

68 lines1.8 KB

1import { useEffect, useRef, useState } from "react"
2import { useQuota } from "../../contexts/quota-context"
3import { useTeam } from "../../contexts/team-context"
4import { QUOTA_BONUS_DOUBLE, QUOTA_BONUS_SINGLE } from "../../lib/config"
5import "./BonusToast.css"
6​
7interface ToastInstance {
8 id: number
9 text: string
10 x: number
11 y: number
13​
14export function BonusToast() {
15 const { bonusCount, bonusPosition, clearBonus } = useQuota()
16 const { team } = useTeam()
17 const [toasts, setToasts] = useState<Array<ToastInstance>>([])
18 const nextIdRef = useRef(0)
19​
20 // Add a new toast when bonus is earned
21 // bonusCount is the number of boxes claimed (1 or 2)
22 // We display the actual token bonus from config
23 useEffect(() => {
24 if (bonusCount > 0 && bonusPosition) {
25 const tokenBonus = bonusCount >= 2 ? QUOTA_BONUS_DOUBLE : QUOTA_BONUS_SINGLE
26 const id = nextIdRef.current++
27​
28 setToasts((prev) => [
29 ...prev,
30 {
31 id,
32 text: `+${tokenBonus} Bonus!`,
33 x: bonusPosition.x,
34 y: bonusPosition.y,
35 },
36 ])
37​
38 // Clear this toast after animation completes
39 setTimeout(() => {
40 setToasts((prev) => prev.filter((t) => t.id !== id))
41 }, 1500)
42​
43 // Clear the bonus state so new bonuses can be detected
44 clearBonus()
45 }
46 }, [bonusCount, bonusPosition, clearBonus])
47​
48 if (toasts.length === 0) return null
49​
50 return (
51 <>
52 {toasts.map((toast) => (
53 <div
54 key={toast.id}
55 className={`bonus-toast bonus-toast-${team.toLowerCase()}`}
56 style={{
57 left: toast.x,
58 // Leave space above the move so the newly filled box stays visible.
59 top: toast.y - 24,
60 }}
61 data-testid="bonus-toast"
62 >
63 {toast.text}
64 </div>
65 ))}
66 </>
67 )