File size: 14,084 Bytes
062a991 | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | "use client";
import { useEffect, useMemo, useState } from "react";
import Link from "next/link";
import { Building2, MapPin, Pencil, Plus, UserPlus, X } from "lucide-react";
import { Avatar } from "./Avatar";
import { currentUser, people } from "@/lib/demo-data";
import { useCurrentUser } from "@/lib/client-session";
type ProfilePerson = {
id: string;
name: string;
username: string;
headline: string;
about: string;
location: string;
avatarUrl?: string | null;
};
type Experience = {
id: string;
companyName: string;
title: string;
location: string;
description: string;
startDate: string;
endDate?: string | null;
current: boolean;
};
const demoExperiences: Experience[] = [
{ id: "demo-northstar", companyName: "Northstar Systems", title: "Product Design Lead", location: "San Francisco Bay Area · Hybrid", description: "Leading design for workflow infrastructure used by operations teams around the world.", startDate: "2022-01", current: true },
{ id: "demo-pattern", companyName: "Pattern Digital", title: "Senior Product Designer", location: "Oakland, California", description: "", startDate: "2019-01", endDate: "2022-01", current: false },
];
function initialPerson(username: string): ProfilePerson {
const match = username === currentUser.username ? currentUser : people.find((item) => item.username === username) ?? people[0];
return {
id: match.id,
name: match.name,
username: match.username,
headline: match.headline,
about: "I design products and teams around clarity. My work focuses on complex professional tools, service design, and the operating systems that help multidisciplinary groups make good decisions together.",
location: username === currentUser.username ? currentUser.location : "New York City Metropolitan Area",
avatarUrl: match.avatar,
};
}
export function ProfileView({ username }: { username: string }) {
const sessionUser = useCurrentUser();
const [person, setPerson] = useState<ProfilePerson>(() => initialPerson(username));
const [experienceRows, setExperienceRows] = useState<Experience[]>(demoExperiences);
const [connectionStatusById, setConnectionStatusById] = useState<Record<string, "pending" | "accepted">>({});
const [connectingId, setConnectingId] = useState<string | null>(null);
const [connectionError, setConnectionError] = useState("");
const [profileOpen, setProfileOpen] = useState(false);
const [experienceOpen, setExperienceOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [formError, setFormError] = useState("");
const [profileDraft, setProfileDraft] = useState({ name: person.name, headline: person.headline, location: person.location, about: person.about });
const [experienceDraft, setExperienceDraft] = useState({ title: "", companyName: "", location: "", startDate: "2026-08", description: "" });
const mine = username === sessionUser.username;
const suggestionPeople = useMemo(() => people.filter((item) => item.id !== person.id), [person.id]);
useEffect(() => {
let active = true;
void fetch(`/api/profile/${encodeURIComponent(username)}`, { cache: "no-store" })
.then(async (response) => {
const body = await response.json() as { user?: ProfilePerson, experiences?: Experience[] };
if (!active || !response.ok || !body.user) return;
setPerson(body.user);
setProfileDraft({ name: body.user.name, headline: body.user.headline, location: body.user.location, about: body.user.about });
setExperienceRows(body.experiences ?? []);
})
.catch(() => undefined);
return () => { active = false; };
}, [username]);
async function requestConnection(recipientId: string) {
if (connectingId || connectionStatusById[recipientId]) return;
setConnectingId(recipientId);
setConnectionError("");
try {
const response = await fetch("/api/connections", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ recipientId }) });
const result = await response.json().catch(() => ({})) as { connection?: { id?: string, status?: "pending" | "accepted" | "declined" }, error?: string };
if (!response.ok || !result.connection?.id) throw new Error(result.error ?? "Unable to send this connection request.");
const status = result.connection.status === "accepted" ? "accepted" : "pending";
setConnectionStatusById((items) => ({ ...items, [recipientId]: status }));
} catch (caught) {
setConnectionError(caught instanceof Error ? caught.message : "Unable to send this connection request.");
} finally {
setConnectingId(null);
}
}
function connectionLabel(recipientId: string) {
if (connectingId === recipientId) return "Sending…";
if (connectionStatusById[recipientId] === "accepted") return "Connected";
if (connectionStatusById[recipientId] === "pending") return "Pending";
return "Connect";
}
function connectionDisabled(recipientId: string) {
return Boolean(connectingId === recipientId || connectionStatusById[recipientId]);
}
async function saveProfile() {
if (saving) return;
setSaving(true);
setFormError("");
try {
const response = await fetch("/api/profile", { method: "PATCH", headers: { "content-type": "application/json" }, body: JSON.stringify(profileDraft) });
const result = await response.json().catch(() => ({})) as { user?: ProfilePerson, error?: string };
if (!response.ok || !result.user) throw new Error(result.error ?? "Unable to save your profile.");
setPerson(result.user);
setProfileOpen(false);
} catch (caught) {
setFormError(caught instanceof Error ? caught.message : "Unable to save your profile.");
} finally {
setSaving(false);
}
}
async function addExperience() {
if (saving) return;
setSaving(true);
setFormError("");
try {
const response = await fetch("/api/profile/experiences", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ ...experienceDraft, current: true }) });
const result = await response.json().catch(() => ({})) as { experience?: Experience, error?: string };
if (!response.ok || !result.experience) throw new Error(result.error ?? "Unable to add this experience.");
setExperienceRows((items) => [result.experience!, ...items]);
setExperienceDraft({ title: "", companyName: "", location: "", startDate: "2026-08", description: "" });
setExperienceOpen(false);
} catch (caught) {
setFormError(caught instanceof Error ? caught.message : "Unable to add this experience.");
} finally {
setSaving(false);
}
}
return (
<div className="profile-layout">
<div className="profile-main">
<section className="surface profile-hero">
<div className="profile-cover"><img src="/media/profile-cover.jpg" alt="" /></div>
<div className="profile-intro">
<Avatar name={person.name} src={person.avatarUrl ?? undefined} size="xl" />
{mine && <button className="icon-button profile-edit" aria-label="Edit intro" onClick={() => { setFormError(""); setProfileOpen(true); }}><Pencil size={19} /></button>}
<h1>{person.name}</h1>
<p>{person.headline || "Add a headline to tell your network what you do."}</p>
<span><MapPin size={15} /> {person.location || "Add your location"} · <a href="#">Contact info</a></span>
<b>482 connections</b>
<div className="profile-actions">
{mine ? <><button className="button button-primary">Open to</button><button className="button button-outline" onClick={() => setExperienceOpen(true)}>Add profile section</button><button className="button button-quiet">More</button></> :
<><button className="button button-primary" disabled={connectionDisabled(person.id)} onClick={() => requestConnection(person.id)}><UserPlus size={17} /> {connectionLabel(person.id)}</button><Link prefetch={false} className="button button-outline" href="/messages">Message</Link><button className="button button-quiet">More</button></>}
</div>
{connectionError && <div className="form-error" role="alert">{connectionError}</div>}
</div>
</section>
{mine && <section className="surface profile-section analytics-section"><header><div><h2>Analytics</h2><span>Private to you</span></div></header><div><article><b>84 profile views</b><p>Discover who viewed your profile.</p></article><article><b>1,204 post impressions</b><p>See who's engaging with your posts.</p></article><article><b>39 search appearances</b><p>See how often you appear in search.</p></article></div></section>}
<section className="surface profile-section"><header><div><h2>About</h2></div>{mine && <button className="icon-button" aria-label="Edit about" onClick={() => setProfileOpen(true)}><Pencil size={19} /></button>}</header><p>{person.about || "Add a summary about your experience and interests."}</p></section>
<section className="surface profile-section">
<header><div><h2>Experience</h2></div>{mine && <button className="icon-button" aria-label="Add experience" onClick={() => { setFormError(""); setExperienceOpen(true); }}><Plus size={20} /></button>}</header>
{experienceRows.length ? experienceRows.map((experience) => <article className="experience-row" key={experience.id}><span className="company-logo">{experience.companyName.slice(0, 2).toUpperCase()}</span><div><h3>{experience.title}</h3><b>{experience.companyName}{experience.current ? " · Full-time" : ""}</b><span>{experience.startDate} - {experience.current ? "Present" : experience.endDate}</span><span>{experience.location}</span>{experience.description && <p>{experience.description}</p>}</div></article>) : <div className="profile-empty"><p>Add a role so people can understand your professional experience.</p>{mine && <button className="button button-outline" onClick={() => setExperienceOpen(true)}>Add experience</button>}</div>}
</section>
<section className="surface profile-section"><header><div><h2>Skills</h2></div>{mine && <button className="button button-outline">Demonstrate skills</button>}</header><div className="skill-row"><b>Product strategy</b><span><Building2 size={16} /> Northstar Systems</span><span>26 endorsements</span></div><div className="skill-row"><b>Interaction design</b><span><Building2 size={16} /> Northstar Systems and Pattern Digital</span><span>41 endorsements</span></div></section>
</div>
<aside className="profile-right"><section className="surface language-card"><div><b>Profile language</b>{mine && <Pencil size={17} />}</div><span>English</span><hr /><div><b>Public profile & URL</b>{mine && <Pencil size={17} />}</div><span>linkedin.local/in/{username}</span></section><section className="surface suggestions"><h2>People also viewed</h2>{suggestionPeople.map((item) => <article key={item.id}><Avatar name={item.name} src={item.avatar} /><div><b>{item.name}</b><span>{item.headline}</span><button className="button button-quiet" disabled={connectionDisabled(item.id)} onClick={() => requestConnection(item.id)}>{connectionLabel(item.id)}</button></div></article>)}</section></aside>
{profileOpen && <div className="dialog-backdrop" role="presentation"><section className="surface profile-dialog" aria-label="Edit profile" role="dialog"><header><h2>Edit intro</h2><button className="icon-button" aria-label="Close" onClick={() => setProfileOpen(false)}><X size={20} /></button></header><label>Full name<input value={profileDraft.name} onChange={(event) => setProfileDraft({ ...profileDraft, name: event.target.value })} /></label><label>Headline<input value={profileDraft.headline} onChange={(event) => setProfileDraft({ ...profileDraft, headline: event.target.value })} /></label><label>Location<input value={profileDraft.location} onChange={(event) => setProfileDraft({ ...profileDraft, location: event.target.value })} /></label><label>About<textarea value={profileDraft.about} onChange={(event) => setProfileDraft({ ...profileDraft, about: event.target.value })} /></label>{formError && <div className="form-error" role="alert">{formError}</div>}<footer><button className="button button-quiet" onClick={() => setProfileOpen(false)}>Cancel</button><button className="button button-primary" disabled={saving} onClick={saveProfile}>{saving ? "Saving…" : "Save"}</button></footer></section></div>}
{experienceOpen && <div className="dialog-backdrop" role="presentation"><section className="surface profile-dialog" aria-label="Add experience" role="dialog"><header><h2>Add experience</h2><button className="icon-button" aria-label="Close" onClick={() => setExperienceOpen(false)}><X size={20} /></button></header><label>Title<input value={experienceDraft.title} onChange={(event) => setExperienceDraft({ ...experienceDraft, title: event.target.value })} /></label><label>Company name<input value={experienceDraft.companyName} onChange={(event) => setExperienceDraft({ ...experienceDraft, companyName: event.target.value })} /></label><label>Location<input value={experienceDraft.location} onChange={(event) => setExperienceDraft({ ...experienceDraft, location: event.target.value })} /></label><label>Start month<input type="month" value={experienceDraft.startDate} onChange={(event) => setExperienceDraft({ ...experienceDraft, startDate: event.target.value })} /></label><label>Description<textarea value={experienceDraft.description} onChange={(event) => setExperienceDraft({ ...experienceDraft, description: event.target.value })} /></label>{formError && <div className="form-error" role="alert">{formError}</div>}<footer><button className="button button-quiet" onClick={() => setExperienceOpen(false)}>Cancel</button><button className="button button-primary" disabled={saving} onClick={addExperience}>{saving ? "Adding…" : "Add experience"}</button></footer></section></div>}
</div>
);
}
|