File size: 6,661 Bytes
9d4a3b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | "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<PendingInvitation[]>([]);
const [suggestions, setSuggestions] = useState<NetworkMember[]>(fallbackSuggestions);
const [sent, setSent] = useState<string[]>([]);
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 (
<div className="network-layout">
<aside className="surface network-manage">
<h2>Manage my network</h2>
<a href="#"><Users size={20} /> Connections <b>482</b></a>
<a href="#"><UserPlus size={20} /> Following & followers</a>
<a href="#"><UserCheck size={20} /> Groups</a>
</aside>
<div className="network-content">
<section className="surface invitations-card">
<header><h1>Invitations</h1><button>See all {pending}</button></header>
{actionError && <div className="form-error" role="alert">{actionError}</div>}
{loading && <div className="empty-state"><Users size={34} /><h3>Loading your network</h3><p>Checking invitations and suggested members.</p></div>}
{!loading && pending > 0 ? (
<>
{invitations.map((invitation) => (
<article key={invitation.connectionId}>
<Avatar name={invitation.member.name} src={invitation.member.avatarUrl ?? undefined} size="lg" />
<div><b>{invitation.member.name}</b><span>{invitation.member.headline}</span><small>{invitation.member.mutual ?? 0} mutual connections</small></div>
<button className="button button-quiet" disabled={busyId === invitation.connectionId} onClick={() => { void respondToInvitation(invitation.connectionId, "declined"); }}>Ignore</button>
<button className="button button-outline" disabled={busyId === invitation.connectionId} onClick={() => { void respondToInvitation(invitation.connectionId, "accepted"); }}>Accept</button>
</article>
))}
</>
) : !loading && <div className="empty-state"><UserCheck size={34} /><h3>You’re all caught up</h3><p>New invitations will appear here.</p></div>}
</section>
<section className="surface people-grid-section">
<header><h2>People you may know</h2><button>See all</button></header>
<div className="people-grid">
{suggestions.map((person) => (
<article className="person-card" key={person.id}>
<div className="person-cover" />
<Avatar name={person.name} src={person.avatarUrl ?? undefined} size="xl" />
<b>{person.name}</b><p>{person.headline}</p><small>{person.mutual ?? 0} mutual connections</small>
<button className={`button ${sent.includes(person.id) ? "button-quiet" : "button-outline"}`} disabled={busyId === person.id || sent.includes(person.id)} onClick={() => { void connect(person.id); }}>
{busyId === person.id ? "Sending…" : sent.includes(person.id) ? <><UserCheck size={17} /> Pending</> : <><UserPlus size={17} /> Connect</>}
</button>
</article>
))}
</div>
</section>
</div>
</div>
);
}
|