import { useEffect, useMemo, useState } from 'react'; import { adjustPoints, getAllContacts, updateContact, upsertContact } from '../lib/api'; import type { Contact } from '../lib/types'; import { useToast } from '../context/ToastContext'; import { Avatar, Empty, Modal, Spinner, Toggle } from '../components/ui'; export function Contacts() { const { push } = useToast(); const [contacts, setContacts] = useState(null); const [error, setError] = useState(null); const [query, setQuery] = useState(''); const [editing, setEditing] = useState(null); const [creating, setCreating] = useState(false); const [pointsFor, setPointsFor] = useState(null); async function load() { try { setContacts(await getAllContacts()); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to load'); } } useEffect(() => { load(); }, []); const filtered = useMemo(() => { if (!contacts) return []; const q = query.trim().toLowerCase(); if (!q) return contacts; return contacts.filter( (c) => c.full_name.toLowerCase().includes(q) || c.email.toLowerCase().includes(q) || (c.linkedin_url ?? '').toLowerCase().includes(q), ); }, [contacts, query]); if (error) return ; if (!contacts) return (
); return (

Contacts

{contacts.length} people · manage contacts and points.
setQuery(e.target.value)} />
{filtered.map((c) => ( ))}
Name LinkedIn Visibility Admin Actions
{c.full_name}
{c.email}
{c.linkedin_url ? ( linkedin ) : ( — )}
{c.show_email ? '📧' : '🙈'} {c.show_linkedin ? '💼' : '🙈'}
{c.is_admin ? admin : —}
{filtered.length === 0 &&
}
{editing && ( setEditing(null)} onSaved={async (msg) => { setEditing(null); await load(); push('success', msg); }} /> )} {creating && ( setCreating(false)} onSaved={async (msg) => { setCreating(false); await load(); push('success', msg); }} /> )} {pointsFor && ( setPointsFor(null)} onSaved={async (msg) => { setPointsFor(null); push('success', msg); }} /> )}
); } function EditContactModal({ contact, onClose, onSaved, }: { contact: Contact; onClose: () => void; onSaved: (msg: string) => void; }) { const [name, setName] = useState(contact.full_name); const [linkedin, setLinkedin] = useState(contact.linkedin_url ?? ''); const [showEmail, setShowEmail] = useState(contact.show_email); const [showLinkedin, setShowLinkedin] = useState(contact.show_linkedin); const [isAdmin, setIsAdmin] = useState(contact.is_admin); const [busy, setBusy] = useState(false); async function save() { setBusy(true); try { await updateContact(contact.email, { full_name: name.trim(), linkedin_url: linkedin.trim() || null, show_email: showEmail, show_linkedin: showLinkedin, is_admin: isAdmin, }); onSaved(`Updated ${name.trim()}`); } catch (e) { onSaved(e instanceof Error ? e.message : 'Update failed'); } finally { setBusy(false); } } return ( } >
setName(e.target.value)} />
setLinkedin(e.target.value)} placeholder="—" />
); } function CreateContactModal({ onClose, onSaved, }: { onClose: () => void; onSaved: (msg: string) => void; }) { const [email, setEmail] = useState(''); const [name, setName] = useState(''); const [linkedin, setLinkedin] = useState(''); const [isAdmin, setIsAdmin] = useState(false); const [busy, setBusy] = useState(false); async function save() { if (!email.trim() || !name.trim()) return; setBusy(true); try { await upsertContact({ email: email.trim(), full_name: name.trim(), linkedin_url: linkedin.trim() || null, is_admin: isAdmin, }); onSaved(`Created ${name.trim()}`); } catch (e) { onSaved(e instanceof Error ? e.message : 'Create failed'); } finally { setBusy(false); } } return ( } >
setName(e.target.value)} placeholder="e.g. Ana Silva" />
setEmail(e.target.value)} placeholder="ana@example.com" />
setLinkedin(e.target.value)} placeholder="—" />

A QR token is generated when the contact is registered for an event — it becomes the login password.

); } function AdjustPointsModal({ contact, onClose, onSaved, }: { contact: Contact; onClose: () => void; onSaved: (msg: string) => void; }) { const [delta, setDelta] = useState('10'); const [reason, setReason] = useState(''); const [busy, setBusy] = useState(false); async function save() { const d = parseInt(delta, 10); if (Number.isNaN(d) || d === 0) return; setBusy(true); try { await adjustPoints(contact.email, d, reason.trim() || 'manual adjustment'); onSaved(`${d > 0 ? '+' : ''}${d} points for ${contact.full_name}`); onClose(); } catch (e) { onSaved(e instanceof Error ? e.message : 'Adjustment failed'); } finally { setBusy(false); } } return ( } >
{contact.full_name}
{contact.email}
setDelta(e.target.value)} />
setReason(e.target.value)} placeholder="e.g. prize, correction" />
); }