import { useEffect, useMemo, useRef, useState } from 'react'; import { getAllCheckins, getAllConnections, getAllContacts, getAllFeedback, getActiveEvent, getLeaderboard, } from '../lib/api'; import type { Checkin, Connection, Contact, Event, Feedback, LeaderboardRow } from '../lib/types'; import { Avatar, Empty, Spinner } from '../components/ui'; import { useSilentRefresh } from '../lib/useSilentRefresh'; import type { View } from '../App'; export function Dashboard({ onNavigate }: { onNavigate: (v: View) => void }) { const [event, setEvent] = useState(null); const [loaded, setLoaded] = useState(false); const [contacts, setContacts] = useState([]); const [checkins, setCheckins] = useState([]); const [connections, setConnections] = useState([]); const [feedback, setFeedback] = useState([]); const [lb, setLb] = useState([]); const [error, setError] = useState(null); const cancelledRef = useRef(false); async function load(silent = false) { try { const ev = await getActiveEvent(); if (cancelledRef.current) return; setEvent(ev); if (!ev) { setLoaded(true); return; } const [c, ck, conn, f, l] = await Promise.all([ getAllContacts(), getAllCheckins(), getAllConnections(), getAllFeedback(), getLeaderboard(), ]); if (cancelledRef.current) return; setContacts(c); setCheckins(ck.filter((x) => x.event_id === ev.id)); // `auto` rows are the staff auto-pairings (migration 20261020120000): // background bookkeeping, not real activity โ€” keep them out of the feed. setConnections(conn.filter((x) => x.event_id === ev.id && !x.auto)); setFeedback(f.filter((x) => x.event_id === ev.id)); setLb(l.filter((x) => x.event_id === ev.id)); setLoaded(true); } catch (e) { // A failed background tick must not replace a good dashboard with the error // screen โ€” this view renders `if (error) return `. if (!silent && !cancelledRef.current) setError(e instanceof Error ? e.message : 'Failed to load'); } } useEffect(() => { cancelledRef.current = false; void load(); return () => { cancelledRef.current = true; }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Live counters: arrivals, connections and the leaderboard move as the event // runs, without anyone pressing reload. useSilentRefresh(() => load(true), { enabled: loaded }); const nameOf = useMemo(() => { const m = new Map(contacts.map((c) => [c.email, c.full_name])); return (email: string) => m.get(email) ?? email; }, [contacts]); if (error) return ; if (!loaded) return (
); if (!event) { return (

Dashboard

Live overview of the event.
); } const top = lb.slice(0, 5); return (

Dashboard

Live overview of the active event.
{event.name}
{fmtDate(event.start_date)} {event.end_date !== event.start_date ? ` โ€“ ${fmtDate(event.end_date)}` : ''} {event.location ? ` ยท ${event.location}` : ''}
โ— Active
๐ŸŽŸ๏ธ {event.num_registrations} Registrations
โœ… {event.num_checked_in_registrations} Checked in
๐Ÿ—“๏ธ {event.num_sessions} Sessions
๐ŸŽค {event.num_speakers} Speakers
โญ {event.avg_feedback != null ? event.avg_feedback.toFixed(1) : 'โ€”'} Avg. feedback

๐Ÿ† Top points

{top.length === 0 ? ( ) : (
{top.map((r, i) => (
{i + 1}
{r.full_name}
{r.points}
))}
)}

โšก Recent activity

); } function ActivityFeed({ checkins, connections, feedback, nameOf, limit, }: { checkins: Checkin[]; connections: Connection[]; feedback: Feedback[]; nameOf: (email: string) => string; limit: number; }) { type Item = { at: string; icon: string; text: string }; const name = (email: string) => nameOf(email); const items: Item[] = [ ...checkins.map((c) => ({ at: c.created_at, icon: 'โœ…', text: `${name(c.contact_email)} checked in`, })), ...connections .filter((c) => c.status === 'accepted') .map((c) => ({ at: c.resolved_at ?? c.created_at, icon: '๐Ÿค', text: `${name(c.from_email)} connected with ${name(c.to_email)}`, })), ...feedback.map((f) => ({ at: f.created_at, icon: 'โญ', text: `${name(f.contact_email)} rated a session ${f.rating}โ˜…`, })), ] .sort((a, b) => (a.at < b.at ? 1 : -1)) .slice(0, limit); if (items.length === 0) return ; return (
{items.map((it, i) => (
{it.icon}
{it.text}
{new Date(it.at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
))}
); } function fmtDate(d: string): string { return new Date(d + 'T00:00:00').toLocaleDateString([], { weekday: 'short', month: 'short', day: 'numeric' }); }