AgPerry's picture
Add files using upload-large-folder tool
9d4a3b0 verified
Raw History Blame Contribute Delete
6.66 kB
"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>
);
}