import { useCallback, useEffect, useMemo, useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { useEvent } from '../lib/useEvent'; import { createConnectionRequest, getMyCheckins, getMyConnections, getTalkCohort, } from '../lib/api'; import { capWords } from '../lib/time'; import type { Connection, TalkCohortMember } from '../lib/types'; import { Avatar, Empty, Spinner } from '../components/ui'; import { useToast } from '../context/ToastContext'; /** * Networking around talks (an embeddable panel, shown as a tab inside the * Connect screen). For every talk the user was checked into, list the other * attendees who were also in that talk (the "talk cohort") so the user can * make a connection. Accepting a connection with a shared talk earns the * per-event talk networking bonus (see `events.talk_networking_bonus`). */ export function NetworkingPanel() { const { contact } = useAuth(); const { event, sessionById, loading } = useEvent(); const [myCheckins, setMyCheckins] = useState([]); const [cohorts, setCohorts] = useState>({}); const [loadingCohorts, setLoadingCohorts] = useState(false); const [connecting, setConnecting] = useState(null); // The user's existing connections, so a Connect button can show the current // state ("Sent" / "Connected" / …) instead of allowing duplicate requests. const [conns, setConns] = useState([]); const { toast } = useToast(); const email = contact?.email ?? ''; const loadConns = useCallback(async () => { try { setConns(await getMyConnections()); } catch { /* non-fatal */ } }, []); useEffect(() => { if (email) void loadConns(); }, [email, loadConns]); // Keep the connection states fresh (a peer may accept while the panel is open). useEffect(() => { window.addEventListener('connections-changed', loadConns); return () => window.removeEventListener('connections-changed', loadConns); }, [loadConns]); /** The existing connection to a given person (any status), or null. */ const connWith = (theirEmail: string) => conns.find((c) => (c.from_email === email && c.to_email === theirEmail) || (c.from_email === theirEmail && c.to_email === email)) ?? null; // Load the sessions the user is checked into, then each talk's cohort. useEffect(() => { if (!email) return; let active = true; (async () => { try { const ids = await getMyCheckins(email); if (!active) return; setMyCheckins(ids); if (ids.length === 0) return; setLoadingCohorts(true); const results = await Promise.all( ids.map(async (sid): Promise<[string, TalkCohortMember[]]> => { try { return [sid, await getTalkCohort(sid)]; } catch { return [sid, []]; } }), ); if (!active) return; const map: Record = {}; for (const [sid, list] of results) map[sid] = list; setCohorts(map); } catch { /* non-fatal */ } finally { if (active) setLoadingCohorts(false); } })(); return () => { active = false; }; }, [email]); // Only show talks that actually exist in the agenda (check-ins for a // service session or a removed session are skipped). const myTalks = useMemo( () => myCheckins.filter((sid) => sessionById.has(sid)).map((sid) => sessionById.get(sid)!), [myCheckins, sessionById], ); if (loading || !contact) { return (
); } async function connect(member: TalkCohortMember) { if (!member.qr_token) { toast('No QR token for this attendee', 'err'); return; } // Never allow a duplicate request: if a connection already exists (any // status), the button is disabled — this is the backstop. if (connWith(member.email)) return; setConnecting(member.email); try { const conn = await createConnectionRequest(member.qr_token); if (conn.status === 'accepted') toast(`Connected with ${member.full_name}! 🤝`, 'ok'); else if (conn.status === 'pending') toast(`Request sent to ${member.full_name}`, 'ok'); else toast('Request already resolved'); window.dispatchEvent(new Event('connections-changed')); await loadConns(); } catch (e) { toast(e instanceof Error ? e.message : 'Could not send request', 'err'); } finally { setConnecting(null); } } /** The Connect button state for a person: 'connect' | 'sent' | 'connected' | 'requested' | 'declined'. */ function connState(theirEmail: string): { label: string; kind: string } { const c = connWith(theirEmail); if (!c) return { label: 'Connect', kind: 'connect' }; if (c.status === 'accepted') return { label: 'Connected', kind: 'connected' }; if (c.status === 'pending') { return c.from_email === email ? { label: 'Sent', kind: 'sent' } : { label: 'Requested', kind: 'requested' }; } return { label: 'Declined', kind: 'declined' }; } const bonus = event?.talk_networking_bonus ?? 0; return (
People who were in the same talk as you. {bonus > 0 ? ( <> Connect with them to earn +{bonus} pts (shared talk). ) : ( <> Connect with people you met at a talk. )}
{myTalks.length === 0 ? ( ) : (
{myTalks.map((talk) => { const members = (cohorts[talk.id] ?? []).filter((m) => m.email !== email); return (
{talk.title}
{talk.room ? `${talk.room} · ` : ''} {talk.speakers ? `🎤 ${talk.speakers}` : ''}
{loadingCohorts && (
)} {!loadingCohorts && members.length === 0 && (
No one else was checked in here yet.
)}
{members.map((m) => (
{m.full_name}
{m.registration_type && (
{capWords(m.registration_type)}
)}
{(() => { const s = connState(m.email); const disabled = s.kind !== 'connect' || connecting === m.email; return ( ); })()}
))}
); })}
)}
); }