- Talk networking: per-event talk_networking_bonus awarded when two people who shared a checked-in talk accept a connection (accept_connection re-created, shared_talk flag, get_talk_cohort RPC, points_log category check widened); Convivo Networking sub-tab inside the Connect screen (Home quick action deep-links in; Connect button reflects existing connection state and is disabled for sent/connected/declined); admin Events view bonus input (0-10). - Reports: sidecar POST /export (CSV / multi-sheet XLSX / PDF report via new pdfkit dep); APPE Admin Reports view (any event: on-screen summary, session feedback summary, top-5, CSV/Excel/PDF downloads); Activity session-feedback per-session aggregate header. - Also carried: @appe.at admin trigger + default password, bulk contact delete, session level/area + event-level feedback, agenda level/area filters, admin password self-service, schedule level/area fields. - 18/18 RPC tests; both apps build; exports verified end-to-end.
220 lines
8.3 KiB
TypeScript
220 lines
8.3 KiB
TypeScript
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<string[]>([]);
|
||
const [cohorts, setCohorts] = useState<Record<string, TalkCohortMember[]>>({});
|
||
const [loadingCohorts, setLoadingCohorts] = useState(false);
|
||
const [connecting, setConnecting] = useState<string | null>(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<Connection[]>([]);
|
||
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<string, TalkCohortMember[]> = {};
|
||
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 (
|
||
<div className="center" style={{ flex: 1 }}>
|
||
<Spinner />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 (
|
||
<div data-testid="networking-panel">
|
||
<div className="muted small" style={{ marginBottom: 14 }}>
|
||
People who were in the same talk as you.
|
||
{bonus > 0 ? (
|
||
<> Connect with them to earn <b>+{bonus} pts</b> (shared talk).</>
|
||
) : (
|
||
<> Connect with people you met at a talk.</>
|
||
)}
|
||
</div>
|
||
|
||
{myTalks.length === 0 ? (
|
||
<Empty icon="🗣" text="You haven’t been checked into a talk yet. Get checked in by staff to see who else was there." />
|
||
) : (
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
||
{myTalks.map((talk) => {
|
||
const members = (cohorts[talk.id] ?? []).filter((m) => m.email !== email);
|
||
return (
|
||
<div className="card" key={talk.id} data-testid={`networking-talk-${talk.id}`}>
|
||
<div className="h2" style={{ marginBottom: 2 }}>{talk.title}</div>
|
||
<div className="muted small" style={{ marginBottom: 10 }}>
|
||
{talk.room ? `${talk.room} · ` : ''}
|
||
{talk.speakers ? `🎤 ${talk.speakers}` : ''}
|
||
</div>
|
||
{loadingCohorts && (
|
||
<div className="center" style={{ padding: 8 }}>
|
||
<Spinner />
|
||
</div>
|
||
)}
|
||
{!loadingCohorts && members.length === 0 && (
|
||
<div className="muted small">No one else was checked in here yet.</div>
|
||
)}
|
||
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
|
||
{members.map((m) => (
|
||
<div className="conn" key={m.email} data-testid={`networking-member-${m.email}`}>
|
||
<Avatar name={m.full_name} id={m.email} size={40} />
|
||
<div className="grow">
|
||
<div className="name">{m.full_name}</div>
|
||
{m.registration_type && (
|
||
<div className="meta">{capWords(m.registration_type)}</div>
|
||
)}
|
||
</div>
|
||
{(() => {
|
||
const s = connState(m.email);
|
||
const disabled = s.kind !== 'connect' || connecting === m.email;
|
||
return (
|
||
<button
|
||
className={
|
||
s.kind === 'connected'
|
||
? 'btn small'
|
||
: s.kind === 'connect'
|
||
? 'btn primary small'
|
||
: 'btn ghost small'
|
||
}
|
||
data-testid={`networking-connect-${m.email}`}
|
||
disabled={disabled}
|
||
onClick={() => void connect(m)}
|
||
>
|
||
{connecting === m.email ? '…' : s.label}
|
||
</button>
|
||
);
|
||
})()}
|
||
</div>
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|