feat: add admin raffle with confetti and top-3/staff exclusion

This commit is contained in:
Lago committed 2026-10-15 21:39:57 +02:00
1 parent 5117a25372
commit 3e2f8ff0ed
14 files changed
+680 -2

No files matched your search

+19
View File
@@ -9,12 +9,14 @@
"version": "0.1.0",
"dependencies": {
"@supabase/supabase-js": "^2.45.4",
"canvas-confetti": "1.9.3",
"lucide-react": "0.468.0",
"qrcode.react": "^4.0.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/canvas-confetti": "1.6.4",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
@@ -1298,6 +1300,13 @@
"@babel/types": "^7.28.2"
}
},
"node_modules/@types/canvas-confetti": {
"version": "1.6.4",
"resolved": "https://registry.npmjs.org/@types/canvas-confetti/-/canvas-confetti-1.6.4.tgz",
"integrity": "sha512-fNyZ/Fdw/Y92X0vv7B+BD6ysHL4xVU5dJcgzgxLdGbn8O3PezZNIJpml44lKM0nsGur+o/6+NZbZeNTt00U1uA==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/estree": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",
@@ -1422,6 +1431,16 @@
],
"license": "CC-BY-4.0"
},
"node_modules/canvas-confetti": {
"version": "1.9.3",
"resolved": "https://registry.npmjs.org/canvas-confetti/-/canvas-confetti-1.9.3.tgz",
"integrity": "sha512-rFfTURMvmVEX1gyXFgn5QMn81bYk70qa0HLzcIOSVEyl57n6o9ItHeBtUSWdvKAPY0xlvBHno4/v3QPrT83q9g==",
"license": "ISC",
"funding": {
"type": "donate",
"url": "https://www.paypal.me/kirilvatev"
}
},
"node_modules/convert-source-map": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz",
+2
View File
@@ -10,12 +10,14 @@
},
"dependencies": {
"@supabase/supabase-js": "^2.45.4",
"canvas-confetti": "1.9.3",
"lucide-react": "0.468.0",
"qrcode.react": "^4.0.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/canvas-confetti": "1.6.4",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
+4
View File
@@ -8,6 +8,7 @@ import { Registrations } from './views/Registrations';
import { Sponsors } from './views/Sponsors';
import { Schedule } from './views/Schedule';
import { Activity } from './views/Activity';
import { Raffle } from './views/Raffle';
import { Events } from './views/Events';
import { Interfaces } from './views/Interfaces';
import { Reports } from './views/Reports';
@@ -22,6 +23,7 @@ export type View =
| 'sponsors'
| 'schedule'
| 'activity'
| 'raffle'
| 'events'
| 'interfaces'
| 'reports'
@@ -34,6 +36,7 @@ const NAV: { id: View; label: string; ico: string }[] = [
{ id: 'sponsors', label: 'Sponsors', ico: '🤝' },
{ id: 'schedule', label: 'Schedule', ico: '🗓️' },
{ id: 'activity', label: 'Activity', ico: '⚡' },
{ id: 'raffle', label: 'Raffle', ico: '🎯' },
{ id: 'events', label: 'Events', ico: '📅' },
{ id: 'interfaces', label: 'Interfaces', ico: '🔌' },
{ id: 'reports', label: 'Reports', ico: '📤' },
@@ -104,6 +107,7 @@ function Shell() {
{view === 'sponsors' && <Sponsors />}
{view === 'schedule' && <Schedule />}
{view === 'activity' && <Activity />}
{view === 'raffle' && <Raffle />}
{view === 'events' && <Events />}
{view === 'interfaces' && <Interfaces />}
{view === 'reports' && <Reports />}
+27
View File
@@ -14,6 +14,8 @@ import type {
PointsLogRow,
PretixOptions,
PretixSyncResult,
RafflePoolEntry,
RaffleWinner,
Registration,
ResetQrResult,
Session,
@@ -349,6 +351,31 @@ export async function adjustPoints(contactEmail: string, delta: number, reason:
await rpc('adjust_points', { p_contact_email: contactEmail, p_delta: delta, p_reason: reason });
}
// ---- Raffle ------------------------------------------------------------------
/** Eligible attendees for the active event: excludes staff/Admin, the
* leaderboard top 3, and everyone already drawn in the current run. */
export async function getRafflePool(): Promise<RafflePoolEntry[]> {
return rpc<RafflePoolEntry[]>('get_raffle_pool', {});
}
/** Winners already drawn in the current run (oldest first). */
export async function getRaffleWinners(): Promise<RaffleWinner[]> {
return all<RaffleWinner>('raffle_draws', { col: 'draw_number' });
}
/** Draw one random eligible attendee and record them as the next winner. */
export async function drawRaffleWinner(): Promise<RaffleWinner> {
const rows = await rpc<Omit<RaffleWinner, 'drawn_at'>[]>('draw_raffle_winner', {});
if (!rows[0]) throw new ApiError('No eligible attendees left to draw');
return { ...rows[0], drawn_at: new Date().toISOString() };
}
/** Clear the current run's winners so a new run can begin. */
export async function resetRaffle(): Promise<void> {
await rpc('reset_raffle', {});
}
// ---- Sessions: speakers, material, AI feedback -------------------------------
export async function setSessionSpeakers(sessionId: string, emails: string[]): Promise<void> {
+13
View File
@@ -330,3 +330,16 @@ export interface ResetQrResult {
email: string;
qr_token: string;
}
/** An eligible attendee returned by `get_raffle_pool` (admin raffle). */
export interface RafflePoolEntry {
contact_email: string;
full_name: string;
points: number;
}
/** A winner recorded by `draw_raffle_winner` / read from `raffle_draws`. */
export interface RaffleWinner extends RafflePoolEntry {
draw_number: number;
drawn_at: string;
}
+14
View File
@@ -12,6 +12,20 @@
@media (max-width: 760px) {
.seg.activity-tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.raffle { display: grid; gap: 20px; }
.raffle-stage { display: grid; gap: 18px; justify-items: center; text-align: center; padding: 32px 20px; }
.raffle-name {
min-height: 1.4em; width: 100%; max-width: 640px; font-size: clamp(24px, 5vw, 44px); font-weight: 700;
color: var(--text); overflow-wrap: anywhere;
}
.raffle-controls { display: flex; align-items: flex-end; justify-content: center; gap: 12px; flex-wrap: wrap; }
.raffle-picks { text-align: left; }
.raffle-picks input { width: 120px; }
.raffle-spin-icon { animation: raffle-rotate 0.9s linear infinite; }
@keyframes raffle-rotate { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
.raffle-spin-icon { animation: none; }
}
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
--bg: #f0f2f6; --surface: #ffffff; --surface-2: #f4f5f9;
+206
View File
@@ -0,0 +1,206 @@
import { useEffect, useRef, useState } from 'react';
import confetti from 'canvas-confetti';
import { RotateCcw, RotateCw } from 'lucide-react';
import {
drawRaffleWinner,
getActiveEvent,
getRafflePool,
getRaffleWinners,
resetRaffle,
} from '../lib/api';
import type { Event, RafflePoolEntry, RaffleWinner } from '../lib/types';
import { Empty, Spinner } from '../components/ui';
const DEFAULT_PICKS = 5;
// Roulette timing: names flash fast, then decelerate onto the server's winner.
const SPIN_STEPS = 22;
const SPIN_MIN_MS = 45;
const SPIN_MAX_MS = 520;
// Celebrate each drawn winner with a confetti burst (skipped for reduced motion).
function celebrate() {
confetti({ particleCount: 160, spread: 80, startVelocity: 45, origin: { y: 0.6 }, disableForReducedMotion: true });
confetti({ particleCount: 60, angle: 60, spread: 60, origin: { x: 0, y: 0.7 }, disableForReducedMotion: true });
confetti({ particleCount: 60, angle: 120, spread: 60, origin: { x: 1, y: 0.7 }, disableForReducedMotion: true });
}
export function Raffle() {
const [event, setEvent] = useState<Event | null>(null);
const [pool, setPool] = useState<RafflePoolEntry[]>([]);
const [winners, setWinners] = useState<RaffleWinner[]>([]);
const [picks, setPicks] = useState(DEFAULT_PICKS);
const [loaded, setLoaded] = useState(false);
const [spinning, setSpinning] = useState(false);
const [currentName, setCurrentName] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const running = useRef(false);
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => {
let active = true;
(async () => {
try {
const ev = await getActiveEvent();
if (!active) return;
setEvent(ev);
if (!ev) { setLoaded(true); return; }
const [entries, drawn] = await Promise.all([getRafflePool(), getRaffleWinners()]);
if (!active) return;
setPool(entries);
setWinners(drawn);
setLoaded(true);
} catch (cause) {
if (active) setError(cause instanceof Error ? cause.message : 'Failed to load raffle');
}
})();
return () => { active = false; if (timer.current) clearTimeout(timer.current); };
}, []);
const total = winners.length + pool.length;
const remainingForRun = Math.max(0, picks - winners.length);
const canDraw = loaded && !spinning && !!event && winners.length < picks && pool.length > 0;
function finish(winner: RaffleWinner) {
setWinners((current) => [...current, winner]);
setPool((current) => current.filter((entry) => entry.contact_email !== winner.contact_email));
setCurrentName(winner.full_name);
running.current = false;
setSpinning(false);
celebrate();
}
function animateTo(winner: RaffleWinner) {
const others = pool.filter((entry) => entry.contact_email !== winner.contact_email).map((entry) => entry.full_name);
const sequence: string[] = [];
for (let step = 0; step < SPIN_STEPS; step += 1) {
sequence.push(others.length > 0 ? others[Math.floor(Math.random() * others.length)] : winner.full_name);
}
sequence.push(winner.full_name);
let index = 0;
const tick = () => {
setCurrentName(sequence[index]);
index += 1;
if (index < sequence.length) {
const progress = index / sequence.length;
timer.current = setTimeout(tick, SPIN_MIN_MS + Math.pow(progress, 2.6) * (SPIN_MAX_MS - SPIN_MIN_MS));
} else {
finish(winner);
}
};
tick();
}
async function draw() {
if (running.current) return;
running.current = true;
setSpinning(true);
setError(null);
try {
const winner = await drawRaffleWinner();
animateTo(winner);
} catch (cause) {
setError(cause instanceof Error ? cause.message : 'Draw failed');
running.current = false;
setSpinning(false);
}
}
async function reset() {
if (spinning) return;
setError(null);
try {
await resetRaffle();
const [entries, drawn] = await Promise.all([getRafflePool(), getRaffleWinners()]);
setPool(entries);
setWinners(drawn);
setCurrentName(null);
} catch (cause) {
setError(cause instanceof Error ? cause.message : 'Reset failed');
}
}
if (error && !loaded) return <Empty icon="⚠️" text={error} />;
if (!loaded) return <div className="loading-screen"><Spinner /></div>;
return (
<div>
<div className="page-head">
<div>
<h1>Raffle</h1>
<div className="sub">
{event ? `${event.name} · ${total} eligible attendees` : 'No active event. Set an event active in the Events view first.'}
</div>
</div>
</div>
{!event ? (
<div className="card"><Empty icon="🎟️" text="No active event. Set an event active in the Events view first." /></div>
) : (
<div className="raffle">
<section className="raffle-stage card" aria-label="Raffle roulette">
<div className="raffle-name" role="status" aria-live="polite">
{currentName ?? 'Press Spin to draw a winner'}
</div>
<div className="raffle-controls">
<label className="field raffle-picks">
<span>Picks per run</span>
<input
className="input"
type="number"
min={1}
max={Math.max(1, total)}
value={picks}
disabled={spinning || winners.length > 0}
onChange={(e) => setPicks(Math.max(1, Number(e.target.value) || 1))}
/>
</label>
<button className="btn primary" onClick={draw} disabled={!canDraw}>
<RotateCw size={16} aria-hidden="true" className={spinning ? 'raffle-spin-icon' : undefined} />
{spinning ? 'Spinning...' : winners.length >= picks ? 'Run complete' : pool.length === 0 ? 'No attendees left' : 'Spin'}
</button>
<button className="btn" onClick={reset} disabled={spinning || winners.length === 0}>
<RotateCcw size={16} aria-hidden="true" />New run
</button>
</div>
<p className="muted small" role="status">
Winners this run: {winners.length} of {picks}
{winners.length < picks && pool.length === 0 && ' · no eligible attendees left'}
</p>
{error && <p className="feedback-error" role="alert">{error}</p>}
<p className="muted small">
Excludes staff/Admin and the leaderboard top 3. Everyone can win only once per run.
</p>
</section>
<section className="card" aria-label="Raffle winners">
<div className="feedback-summary-head">
<h2 className="h2">Winners</h2>
<span className="muted small">{remainingForRun > 0 ? `${remainingForRun} to go` : 'Run complete'}</span>
</div>
{winners.length === 0 ? (
<Empty icon="🏆" text="No winners yet. Spin the roulette to draw the first attendee." />
) : (
<div className="table-wrap">
<table className="data">
<thead>
<tr><th>#</th><th>Attendee</th><th>Points</th><th>When</th></tr>
</thead>
<tbody>
{winners.map((w) => (
<tr key={w.contact_email}>
<td className="muted">{w.draw_number}</td>
<td style={{ fontWeight: 600 }}>{w.full_name}</td>
<td>{w.points}</td>
<td className="muted">{new Date(w.drawn_at).toLocaleTimeString()}</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</section>
</div>
)}
</div>
);
}