nomadro/src/modules/social/components/ProfileStudio.tsx
Eric 35e5f863fe Retitle social IA and float feedback as a corner mate.
Move 想见的人 to 一起做事/找搭子, rename gigs to 招聘, hide skill swap and the motion toggle, keep coffeechat online-backed, and add a non-dismissive feedback orb.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-09 20:33:03 +08:00

333 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useEffect, useRef, useState, type FormEvent } from "react";
import Link from "next/link";
import { ArrowUpRight, Check, Globe2, MapPin, Sparkles } from "lucide-react";
import SiteShell from "./SiteShell";
import SocialAvatar from "./SocialAvatar";
import { PageIntro } from "./SocialUI";
import { socialApi } from "@social/lib/api";
import { useLocalState } from "@social/lib/useLocalState";
import { loadJson } from "@social/lib/storage";
import {
ROSTER_PUBLISH_KEY,
setRosterPublished,
} from "@social/lib/discoveryRoster";
import { socialPath } from "@social/lib/socialPath";
import type { SocialRosterProfile } from "@social/lib/types";
import { socialTx } from "@social/lib/socialTx";
import TripEditor from "./TripEditor";
const EMPTY: SocialRosterProfile = {
name: "",
title: "",
city: "",
phone: "",
linktree: "",
skills: "",
bio: "",
avatar: "",
intents: "",
availability: "",
status: "",
lookingFor: "",
gender: "",
};
const FIELDS = [
{
key: "name",
label: "怎么称呼你",
placeholder: "一个让人记得住的名字",
required: true,
max: 40,
},
{
key: "title",
label: "你正在做什么",
placeholder: "独立开发者 / 设计师 / 自由撰稿人…",
max: 80,
},
{
key: "city",
label: "此刻在哪座城市",
placeholder: "清迈、里斯本,或者正在路上",
max: 80,
},
{
key: "skills",
label: "擅长什么,喜欢什么",
placeholder: "设计, 摄影, 冲浪, 开源项目…",
max: 200,
},
{
key: "linktree",
label: "让别人更了解你的链接(选填)",
placeholder: "https://你的作品集或社交主页",
max: 400,
},
{
key: "phone",
label: "联系方式(选填)",
placeholder: "仅在你愿意提供时填写",
max: 80,
},
] as const;
export default function ProfileStudio() {
const [profile, setProfile] = useLocalState<SocialRosterProfile>(
"roster",
EMPTY,
);
const [published, setPublished] = useLocalState<boolean>(
ROSTER_PUBLISH_KEY,
false,
);
const [, setNick] = useLocalState("nick", "游民");
const [sync, setSync] = useLocalState("roster-sync", "draft");
const [status, setStatus] = useState("");
const [busy, setBusy] = useState(false);
const edited = useRef(false),
lock = useRef(false);
useEffect(() => {
let active = true;
void socialApi
.getRoster()
.then((response) => {
if (!active || edited.current) return;
// An unsent local edit takes precedence over an older server profile.
if (
response.item &&
!loadJson<SocialRosterProfile>("roster", EMPTY).updatedAt
) {
const next = { ...EMPTY };
for (const key of Object.keys(EMPTY) as (keyof typeof EMPTY)[]) {
next[key] = response.item[key] || "";
}
// Keep a locally verified selfie if server row has no avatar yet
const localAvatar =
loadJson<SocialRosterProfile>("roster", EMPTY).avatar || "";
if (!next.avatar && localAvatar) next.avatar = localAvatar;
setProfile(next);
setSync("synced");
}
})
.catch(() => {
if (active) setStatus("你可以继续编辑。资料草稿会保存在当前设备。 ");
});
return () => {
active = false;
};
// Only load once: later fetches must never overwrite edits.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const update = (key: keyof SocialRosterProfile, value: string) => {
edited.current = true;
setSync("draft");
setProfile((current) => ({
...current,
[key]: value,
updatedAt: new Date().toISOString(),
}));
setStatus("");
};
const complete = [
profile.name,
profile.title,
profile.city,
profile.skills,
profile.bio,
].filter((value) => value.trim()).length;
const submit = async (event: FormEvent) => {
event.preventDefault();
if (lock.current || !profile.name.trim()) return;
if (profile.linktree && !/^https?:\/\//i.test(profile.linktree)) {
setStatus("作品链接请以 https:// 或 http:// 开头。");
return;
}
lock.current = true;
setBusy(true);
const snapshot = { ...profile };
const payload = { ...snapshot, name: snapshot.name.trim() };
setNick(payload.name);
try {
await socialApi.saveRoster(payload);
setSync("synced");
setStatus("介绍已保存。你可以去聊天室跟大家打个招呼。");
} catch {
setSync("draft");
setStatus("先留在你这边了。有网后再点一次保存就行。");
} finally {
lock.current = false;
setBusy(false);
}
};
return (
<SiteShell title={socialTx("我的介绍")}>
<PageIntro title={socialTx("我的介绍")} accent="让别人认识你">
<div className="social-actions">
<Link className="social-btn" href={socialPath("/people")}>
{socialTx("看看大家那边")}
</Link>
<Link className="social-btn" href={socialPath("/date")}>
{socialTx("去左右滑")}
</Link>
</div>
</PageIntro>
<p className="social-muted social-event-lead reveal">
{socialTx("写得越完整,别人越容易想认识你。草稿会先留在本机。")}
</p>
<div className="social-profile-studio">
<TripEditor />
<section className="social-panel social-panel-fx reveal">
<div className="social-section-label">
<span>{sync === "synced" ? "已保存" : "草稿还在你这边"}</span>
<span className="social-profile-meter">{complete}/5</span>
</div>
<form
className="social-form"
onSubmit={(event) => void submit(event)}
aria-busy={busy}
>
<fieldset
disabled={busy}
className="social-form-grid social-fieldset"
>
{FIELDS.map((field) => (
<label key={field.key}>
{field.label}
<input
required={"required" in field && field.required}
type={field.key === "linktree" ? "url" : "text"}
value={profile[field.key]}
maxLength={field.max}
placeholder={field.placeholder}
onChange={(event) => update(field.key, event.target.value)}
/>
</label>
))}
<label className="full">
{socialTx("自我介绍")}
<textarea
rows={4}
maxLength={600}
value={profile.bio}
onChange={(event) => update("bio", event.target.value)}
placeholder={socialTx("在哪、做什么、想认识谁")}
/>
<small>{profile.bio.length}/600</small>
</label>
</fieldset>
<label className="social-publish-toggle">
<input
type="checkbox"
checked={Boolean(published)}
onChange={(event) => {
const next = event.target.checked;
setPublished(next);
setRosterPublished(next);
setStatus(
next
? "已加入当前设备的发现列表,方便预览。不会向其他用户公开发布。"
: "已从当前设备的发现列表隐藏。",
);
}}
/>
<span>{socialTx("在当前设备的「找搭子」中预览我的介绍")}</span>
</label>
{published && complete < 5 ? (
<p className="social-status warn" role="status">
介绍还差 {5 - complete} 项,补全后再出现会更完整。
</p>
) : null}
<button
type="submit"
className="social-btn primary"
disabled={busy || !profile.name.trim()}
>
<Check size={15} />
{busy ? "保存中…" : "保存介绍"}
</button>
{status && (
<p
className={`social-status${sync !== "synced" ? " warn" : ""}`}
role="status"
>
{status}
</p>
)}
</form>
</section>
<section className="social-profile-preview reveal">
<div className="social-section-label">
<span>{socialTx("预览")}</span>
<Sparkles size={16} />
</div>
<div className="social-profile-passport">
<div className="social-passport-pattern" aria-hidden="true">
<Globe2 size={170} strokeWidth={0.5} />
</div>
{profile.avatar?.trim() ? (
<div className="social-profile-face">
<SocialAvatar src={profile.avatar.trim()} size={88} allowPlaceholder />
</div>
) : (
<div className="social-profile-monogram">
{profile.name.trim().slice(0, 1) || "N"}
</div>
)}
<span className="social-page-kicker">{socialTx("先看看效果")}</span>
<h2>{profile.name.trim() || "你的名字"}</h2>
<p>{profile.title || "你在做什么"}</p>
<span className="social-member-city">
<MapPin size={13} />
{profile.city || "还没写城市"}
</span>
<div className="social-tags">
{profile.skills
.split(/[,,、]/)
.filter(Boolean)
.slice(0, 8)
.map((skill, index) => (
<span key={index}>{skill.trim()}</span>
))}
</div>
<p className="social-profile-bio">
{profile.bio || "还没写自我介绍"}
</p>
<footer>
{socialTx("我的介绍")}<span>✦</span>
</footer>
</div>
<div className="social-profile-progress">
<div>
<span>{socialTx("写好了多少")}</span>
<strong>{complete}/5</strong>
</div>
<progress max={5} value={complete} aria-label={socialTx("介绍完成度")} />
<p>{socialTx("名字 · 做什么 · 城市 · 兴趣 · 介绍")}</p>
</div>
<div className="social-actions">
<Link
className="social-btn primary"
href={socialPath(
`/chat?intro=${encodeURIComponent(`大家好,我是${profile.name || "新朋友"},目前在${profile.city || "探索中"}。${profile.bio || "很高兴认识大家!"}`)}`,
)}
>
{socialTx("进群组打招呼")} <ArrowUpRight size={15} />
</Link>
<Link className="social-btn" href={socialPath("/people")}>
{socialTx("看发现页")}
</Link>
<Link className="social-btn" href={socialPath("/date")}>
{socialTx("去认识一下")}
</Link>
<Link className="social-btn" href={socialPath("/meetup")}>
{socialTx("语音视频场")}
</Link>
</div>
</section>
</div>
</SiteShell>
);
}