import { useCallback, useEffect, useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { acceptConnection, getLeaderboard, getMyConnections, getPublicContacts, rejectConnection } from '../lib/api'; import { usePolling } from '../lib/usePolling'; import { capWords } from '../lib/time'; import type { Connection, PublicContact } from '../lib/types'; import { Avatar, Empty, Sheet, Spinner } from '../components/ui'; import { useToast } from '../context/ToastContext'; import { NetworkingPanel } from './Networking'; export function Connections({ initialTab = 'mine', }: { initialTab?: 'mine' | 'req' | 'networking'; }) { const { contact } = useAuth(); const [tab, setTab] = useState<'mine' | 'req' | 'networking'>(initialTab); // A deep-link (e.g. the Home "Networking" quick action) re-targets the tab. useEffect(() => { setTab(initialTab); }, [initialTab]); const [conns, setConns] = useState([]); const [people, setPeople] = useState>(new Map()); const [points, setPoints] = useState>(new Map()); const [detail, setDetail] = useState<{ email: string; status: Connection['status'] } | null>(null); const [loading, setLoading] = useState(true); const { toast } = useToast(); const load = useCallback(async () => { try { const [c, p, lb] = await Promise.all([getMyConnections(), getPublicContacts(), getLeaderboard()]); setConns(c); setPeople(new Map(p.map((x) => [x.email, x]))); setPoints(new Map(lb.map((x) => [x.contact_email, x.points]))); } catch { /* non-fatal */ } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); // Live-ish updates: poll every 5s while the tab is visible. usePolling(() => void load(), 5000, !loading); if (!contact) return null; const me = contact.email; const nameOf = (email: string) => people.get(email)?.full_name ?? '…'; const typeOf = (email: string) => people.get(email)?.registration_type ?? null; const pointsOf = (email: string) => points.get(email) ?? 0; const accepted = conns.filter((c) => c.status === 'accepted'); const incoming = conns.filter((c) => c.status === 'pending' && c.to_email === me); const outgoing = conns.filter((c) => c.status === 'pending' && c.from_email === me); async function resolve(id: string, accept: boolean) { try { if (accept) await acceptConnection(id); else await rejectConnection(id); toast(accept ? 'Connected! +5 pts 🤝' : 'Request declined', accept ? 'ok' : ''); window.dispatchEvent(new Event('connections-changed')); await load(); } catch (e) { toast(e instanceof Error ? e.message : 'Could not update request', 'err'); } } if (loading) { return (
); } return (
{tab === 'networking' ? 'Networking' : 'Connections'}
{tab === 'mine' && (accepted.length === 0 ? ( ) : ( accepted.map((c) => { const otherEmail = c.from_email === me ? c.to_email : c.from_email; return (
setDetail({ email: otherEmail, status: c.status })}>
{nameOf(otherEmail)}
{typeOf(otherEmail) && (
{capWords(typeOf(otherEmail)!)}
)}
Connected
); }) ))} {tab === 'req' && (
{incoming.length === 0 && outgoing.length === 0 && ( )} {incoming.length > 0 && (
Incoming
{incoming.map((c) => (
setDetail({ email: c.from_email, status: c.status })}>
{nameOf(c.from_email)}
{typeOf(c.from_email) && (
{capWords(typeOf(c.from_email)!)}
)}
))}
)} {outgoing.length > 0 && (
Sent
{outgoing.map((c) => (
setDetail({ email: c.to_email, status: c.status })}>
{nameOf(c.to_email)}
{typeOf(c.to_email) && (
{capWords(typeOf(c.to_email)!)}
)}
Pending
))}
)}
)} {tab === 'networking' && } {detail && (() => { const p = people.get(detail.email); const t = p?.registration_type ?? null; return ( setDetail(null)}>
{p?.full_name ?? '…'}
{t && {capWords(t)}} ⚡ {pointsOf(detail.email)} pts
{p?.linkedin_url && ( {p.linkedin_url.replace(/^https?:\/\//, '')} )} {p?.show_email &&
{detail.email}
}
{detail.status === 'accepted' ? 'Connected' : detail.status === 'pending' ? 'Request pending' : 'Request declined'}
); })()}
); }