- Migration 20261020120000: staff (type staff / Admin / @appe.at) are auto-connected to every participant of the event via connections.auto rows inserted directly as accepted - never through accept_connection, so zero points by construction; backfill + idempotent helper + trigger. - Migration 20261020130000: submit_event_feedback awards +5 on the first submission per participant x event (edits earn 0), gated by _points_eligible; response gains earned column; ACL restored. - Admin: points move from Contacts to Registrations (active-event rows only, in-place save); Dashboard Top points drops emails; Dashboard and Activity hide auto connections. - Attendee: leaderboard masks surnames (Max M.); EventFeedback toast shows +5 pts on paid submissions. - Tests: staff_auto_connections_tests.sql (14 assertions), rpc_tests section 11 (4 assertions). Docs + release record 2026-10-20. - README: document the git-ignored .env provisioning step (missing key = white screen). 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]>
255 lines
8.6 KiB
TypeScript
255 lines
8.6 KiB
TypeScript
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<Event | null>(null);
|
||
const [loaded, setLoaded] = useState(false);
|
||
const [contacts, setContacts] = useState<Contact[]>([]);
|
||
const [checkins, setCheckins] = useState<Checkin[]>([]);
|
||
const [connections, setConnections] = useState<Connection[]>([]);
|
||
const [feedback, setFeedback] = useState<Feedback[]>([]);
|
||
const [lb, setLb] = useState<LeaderboardRow[]>([]);
|
||
const [error, setError] = useState<string | null>(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 <Empty/>`.
|
||
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 <Empty icon="⚠️" text={error} />;
|
||
if (!loaded)
|
||
return (
|
||
<div className="loading-screen">
|
||
<Spinner />
|
||
</div>
|
||
);
|
||
|
||
if (!event) {
|
||
return (
|
||
<div>
|
||
<div className="page-head">
|
||
<div>
|
||
<h1>Dashboard</h1>
|
||
<div className="sub">Live overview of the event.</div>
|
||
</div>
|
||
</div>
|
||
<div className="card">
|
||
<Empty icon="📅" text="No active event. Create one and set it active in the Events view." />
|
||
<div style={{ textAlign: 'center', marginTop: 12 }}>
|
||
<button className="btn primary" onClick={() => onNavigate('events')}>
|
||
Go to Events →
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const top = lb.slice(0, 5);
|
||
|
||
return (
|
||
<div>
|
||
<div className="page-head">
|
||
<div>
|
||
<h1>Dashboard</h1>
|
||
<div className="sub">Live overview of the active event.</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="card event-card">
|
||
<div className="row between" style={{ flexWrap: 'wrap', gap: 12 }}>
|
||
<div>
|
||
<div className="event-name">{event.name}</div>
|
||
<div className="muted small">
|
||
{fmtDate(event.start_date)}
|
||
{event.end_date !== event.start_date ? ` – ${fmtDate(event.end_date)}` : ''}
|
||
{event.location ? ` · ${event.location}` : ''}
|
||
</div>
|
||
</div>
|
||
<span className="pill green">● Active</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="stat-grid">
|
||
<div className="stat">
|
||
<span className="ico">🎟️</span>
|
||
<span className="val">{event.num_registrations}</span>
|
||
<span className="lbl">Registrations</span>
|
||
</div>
|
||
<div className="stat">
|
||
<span className="ico">✅</span>
|
||
<span className="val">{event.num_checked_in_registrations}</span>
|
||
<span className="lbl">Checked in</span>
|
||
</div>
|
||
<div className="stat">
|
||
<span className="ico">🗓️</span>
|
||
<span className="val">{event.num_sessions}</span>
|
||
<span className="lbl">Sessions</span>
|
||
</div>
|
||
<div className="stat">
|
||
<span className="ico">🎤</span>
|
||
<span className="val">{event.num_speakers}</span>
|
||
<span className="lbl">Speakers</span>
|
||
</div>
|
||
<div className="stat accent">
|
||
<span className="ico">⭐</span>
|
||
<span className="val">{event.avg_feedback != null ? event.avg_feedback.toFixed(1) : '—'}</span>
|
||
<span className="lbl">Avg. feedback</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="grid-2">
|
||
<div className="card">
|
||
<div className="row between" style={{ marginBottom: 14 }}>
|
||
<h2 className="h2">🏆 Top points</h2>
|
||
<button className="btn small ghost" onClick={() => onNavigate('registrations')}>
|
||
View all →
|
||
</button>
|
||
</div>
|
||
{top.length === 0 ? (
|
||
<Empty icon="🏆" text="No points yet" />
|
||
) : (
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
|
||
{top.map((r, i) => (
|
||
<div key={r.contact_email} className="row" style={{ gap: 12 }}>
|
||
<span style={{ width: 22, fontWeight: 900, color: 'var(--muted)' }}>{i + 1}</span>
|
||
<Avatar name={r.full_name} id={r.contact_email} size={36} fs={13} />
|
||
<div className="grow">
|
||
<div style={{ fontWeight: 700, fontSize: 14 }}>{r.full_name}</div>
|
||
</div>
|
||
<span className="pts">{r.points}</span>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<div className="card">
|
||
<div className="row between" style={{ marginBottom: 14 }}>
|
||
<h2 className="h2">⚡ Recent activity</h2>
|
||
<button className="btn small ghost" onClick={() => onNavigate('activity')}>
|
||
View all →
|
||
</button>
|
||
</div>
|
||
<ActivityFeed checkins={checkins} connections={connections} feedback={feedback} nameOf={nameOf} limit={6} />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 <Empty icon="⚡" text="No activity yet" />;
|
||
return (
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
|
||
{items.map((it, i) => (
|
||
<div key={i} className="row" style={{ gap: 10 }}>
|
||
<span style={{ fontSize: 18 }}>{it.icon}</span>
|
||
<div className="grow" style={{ fontSize: 13.5 }}>
|
||
{it.text}
|
||
</div>
|
||
<span className="muted small">{new Date(it.at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</span>
|
||
</div>
|
||
))}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function fmtDate(d: string): string {
|
||
return new Date(d + 'T00:00:00').toLocaleDateString([], { weekday: 'short', month: 'short', day: 'numeric' });
|
||
}
|