"use client"; import { useEffect, useState } from "react"; import { UserCheck, UserPlus, Users } from "lucide-react"; import { Avatar } from "./Avatar"; import { people } from "@/lib/demo-data"; type NetworkMember = { id: string; name: string; username: string; headline: string; avatarUrl?: string | null; mutual?: number; }; type PendingInvitation = { connectionId: string; member: NetworkMember; }; type ConnectionsPayload = { pending?: Array<{ connection: { id: string }, requester: NetworkMember }>; suggestions?: NetworkMember[]; error?: string; }; const fallbackSuggestions: NetworkMember[] = people.map((person) => ({ id: person.id, name: person.name, username: person.username, headline: person.headline, avatarUrl: person.avatar, mutual: person.mutual, })); export function NetworkView() { const [invitations, setInvitations] = useState([]); const [suggestions, setSuggestions] = useState(fallbackSuggestions); const [sent, setSent] = useState([]); const [busyId, setBusyId] = useState(""); const [actionError, setActionError] = useState(""); const [loading, setLoading] = useState(true); const pending = invitations.length; useEffect(() => { let cancelled = false; async function loadNetwork() { setActionError(""); try { const response = await fetch("/api/connections", { cache: "no-store" }); const result = await response.json().catch(() => ({})) as ConnectionsPayload; if (!response.ok) throw new Error(result.error ?? "Unable to load network data."); if (cancelled) return; setInvitations((result.pending ?? []).map((item) => ({ connectionId: item.connection.id, member: item.requester }))); setSuggestions(result.suggestions?.length ? result.suggestions : fallbackSuggestions); } catch (caught) { if (!cancelled) setActionError(caught instanceof Error ? caught.message : "Unable to load network data."); } finally { if (!cancelled) setLoading(false); } } void loadNetwork(); return () => { cancelled = true; }; }, []); async function connect(id: string) { if (sent.includes(id) || busyId) return; setBusyId(id); setActionError(""); try { const response = await fetch("/api/connections", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ recipientId: id }) }); const result = await response.json().catch(() => ({})) as { connection?: { id?: string }, error?: string }; if (!response.ok || !result.connection?.id) throw new Error(result.error ?? "Unable to send this invitation."); setSent((items) => items.includes(id) ? items : [...items, id]); } catch (caught) { setActionError(caught instanceof Error ? caught.message : "Unable to send this invitation."); } finally { setBusyId(""); } } async function respondToInvitation(id: string, status: "accepted" | "declined") { if (busyId) return; setBusyId(id); setActionError(""); try { const response = await fetch("/api/connections", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify({ id, status }) }); const result = await response.json().catch(() => ({})) as { connection?: { id?: string }, error?: string }; if (!response.ok || !result.connection?.id) throw new Error(result.error ?? "Unable to update this invitation."); setInvitations((items) => items.filter((invitation) => invitation.connectionId !== id)); } catch (caught) { setActionError(caught instanceof Error ? caught.message : "Unable to update this invitation."); } finally { setBusyId(""); } } return (

Invitations

{actionError &&
{actionError}
} {loading &&

Loading your network

Checking invitations and suggested members.

} {!loading && pending > 0 ? ( <> {invitations.map((invitation) => (
{invitation.member.name}{invitation.member.headline}{invitation.member.mutual ?? 0} mutual connections
))} ) : !loading &&

You’re all caught up

New invitations will appear here.

}

People you may know

{suggestions.map((person) => (
{person.name}

{person.headline}

{person.mutual ?? 0} mutual connections
))}
); }