"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( "roster", EMPTY, ); const [published, setPublished] = useLocalState( 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("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("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 (
{socialTx("看看大家那边")} {socialTx("去左右滑")}

{socialTx("写得越完整,别人越容易想认识你。草稿会先留在本机。")}

{sync === "synced" ? "已保存" : "草稿还在你这边"} {complete}/5
void submit(event)} aria-busy={busy} >
{FIELDS.map((field) => ( ))}