Files
Convivo/apps/convivo/src/screens/Leaderboard.tsx
T
7605c9bbf4 Redesign Phases 3-6: multi-event model, APPE Admin, Convivo App behaviors, APPE branding
- Phase 3: new migration 20260824000000_redesign.sql (contacts/events/registrations/sponsors, slot types, session speakers + feedback/material), seed rewrite
- Phase 4: APPE Admin rebrand + Contacts/Registrations/Sponsors/Schedule/Activity/Events views
- Phase 5: Convivo App (24h login, 5s polling, QR type badge, red dot, speaker material)
- Phase 6: APPE favicon + PWA icons for both apps
- Docs: schema/rpcs/rls/seed/screens/api-client/admin-app/architecture/next-steps/README/roadmap updated

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-08-23 02:53:16 +02:00

108 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useState } from 'react';
import { useAuth } from '../context/AuthContext';
import { supabase } from '../lib/supabase';
import { getLeaderboard } from '../lib/api';
import type { LeaderboardRow } from '../lib/types';
import { Avatar, Spinner } from '../components/ui';
export function Leaderboard() {
const { contact } = useAuth();
const [rows, setRows] = useState<LeaderboardRow[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
let active = true;
(async () => {
try {
const data = await getLeaderboard();
if (active) setRows(data);
} catch {
/* non-fatal */
} finally {
if (active) setLoading(false);
}
})();
// Live updates: re-fetch on any leaderboard change (points are synced
// transactionally by the RPCs, so the table is always consistent).
const channel = supabase
.channel('leaderboard-live')
.on('postgres_changes', { event: '*', schema: 'public', table: 'leaderboard' }, () => {
getLeaderboard().then((d) => active && setRows(d)).catch(() => {});
})
.subscribe();
return () => {
active = false;
supabase.removeChannel(channel);
};
}, []);
if (loading) {
return (
<section className="screen">
<div className="center" style={{ flex: 1 }}>
<Spinner />
</div>
</section>
);
}
const me = contact?.email;
const top3 = rows.slice(0, 3);
const rest = rows.slice(3);
const myRank = rows.findIndex((r) => r.contact_email === me) + 1;
return (
<section className="screen">
<div className="row between">
<div>
<div className="h1">Leaderboard</div>
<div className="muted small">All attendees · by points</div>
</div>
<span className="pill green">● Live</span>
</div>
{top3.length > 0 && (
<div className="podium">
{[1, 0, 2].map((idx) => {
const r = top3[idx];
if (!r) return <div key={idx} style={{ width: 96 }} />;
const cls = idx === 0 ? 'b1' : idx === 1 ? 'b2' : 'b3';
return (
<div className="pod" key={r.contact_email}>
<div className={`bar ${cls}`}>{r.points}</div>
<div className="pname">{r.full_name.split(' ')[0]}</div>
<div className="ppoints">#{idx + 1}</div>
</div>
);
})}
</div>
)}
<div style={{ display: 'flex', flexDirection: 'column' }}>
{rest.map((r, i) => {
const rank = i + 4;
const isMe = r.contact_email === me;
return (
<div className={`lb-row ${isMe ? 'me' : ''}`} key={r.contact_email}>
<span className="rank">{rank}</span>
<Avatar name={r.full_name} id={r.contact_email} size={38} fs={13} />
<div className="grow">
<div style={{ fontWeight: 700, fontSize: 14 }}>{r.full_name} {isMe && <span className="muted small">(you)</span>}</div>
</div>
<span className="pts">{r.points}</span>
</div>
);
})}
</div>
{myRank > 0 && (
<div className="muted small" style={{ textAlign: 'center', marginTop: 6 }}>
You’re ranked <b>#{myRank}</b> of {rows.length}
</div>
)}
</section>
);
}