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

MoveDial.tsx

87 lines3.2 KB

1import { useMemo } from "react"
2import type { CSSProperties } from "react"
3import { REFILL_INTERVAL_MS, useQuota } from "../../contexts/quota-context"
4import { useTeam } from "../../contexts/team-context"
5import { useMoveCounterShake } from "../../hooks/useMoveCounterShake"
6import { TEAM_COLORS } from "../../../shared/teams"
7import "./MoveDial.css"
8​
9/** One rounded arc per move, with a small gap between adjacent segments. */
10function segmentPath(index: number, max: number): string {
11 const point = (degrees: number) => {
12 const radians = (degrees * Math.PI) / 180
13 return `${22 + 18 * Math.cos(radians)} ${22 + 18 * Math.sin(radians)}`
14 }
15 const start = -90 + (index * 360) / max + 5
16 const end = -90 + ((index + 1) * 360) / max - 5
17 return `M ${point(start)} A 18 18 0 0 1 ${point(end)}`
19​
20/** Shared move HUD for the pencil cursor and the fixed board overlay. */
21export function MoveDial({
22 className = "",
23 testId = "move-dial",
24}: {
25 className?: string
26 testId?: string
27}) {
28 const { team } = useTeam()
29 const { remaining, max, nextRefillAt, emptyMoveAttempts } = useQuota()
30 const ref = useMoveCounterShake(emptyMoveAttempts)
31 const teamColor = TEAM_COLORS[team].primary
32 const segments = useMemo(() => Array.from({ length: max }, (_, i) => segmentPath(i, max)), [max])
33​
34 // Spending and box bonuses move the sweep to a different segment without
35 // restarting it. Only a new refill deadline starts a new animation.
36 const chargeStyle = useMemo(() => {
37 const duration = Math.max(0, (nextRefillAt ?? Date.now()) - Date.now())
38 return {
39 "--charge-start": Math.min(1, duration / REFILL_INTERVAL_MS),
40 "--charge-duration": `${Math.max(1, duration)}ms`,
41 } as CSSProperties
42 }, [nextRefillAt])
43​
44 return (
45 <div
46 ref={ref}
47 className={`move-dial ${className}`}
48 data-testid={testId}
49 data-empty={remaining === 0}
50 role="img"
51 aria-label={`${remaining} of ${max} moves available${remaining === 0 ? ", recharging" : ""}`}
52 style={{ "--ammo-color": remaining === 0 ? "#E53935" : teamColor } as CSSProperties}
53 >
54 <svg className="move-dial-ring" viewBox="0 0 44 44" fill="none" aria-hidden="true">
55 <g data-testid={`${testId}-segments`}>
56 {segments.map((path, i) => (
57 <g key={i}>
58 <path d={path} className="move-dial-track" />
59 <path
60 d={path}
61 pathLength="1"
62 className={`move-dial-segment${i < remaining ? " move-dial-segment--filled" : ""}`}
63 data-filled={i < remaining}
64 />
65 </g>
66 ))}
67 </g>
68 {nextRefillAt !== null && remaining < max && (
69 <path
70 key={nextRefillAt}
71 d={segments[remaining]}
72 pathLength="1"
73 className="move-dial-charge"
74 data-testid={`${testId}-charge`}
75 style={chargeStyle}
76 />
77 )}
78 </svg>
79 <div className="move-dial-readout" aria-hidden="true">
80 <span className="move-dial-count" data-testid={`${testId}-count`}>
81 {remaining}
82 </span>
83 <span className="move-dial-label">{remaining === 0 ? "Refill" : "Moves"}</span>
84 </div>
85 </div>
86 )