"use client"; import { useEffect, useId, useMemo, useRef, useState, useSyncExternalStore, } from "react"; import Link from "next/link"; import type { SocialRosterMember } from "@social/lib/rosterMock"; import { SELF_MEMBER_ID, useDiscoveryRoster, } from "@social/lib/discoveryRoster"; import { useRosterLikes, setRosterLike } from "@social/lib/useRosterLikes"; import { loadJson } from "@social/lib/storage"; import { socialPath } from "@social/lib/socialPath"; import SocialAvatar from "./SocialAvatar"; import SectionSpark from "./SectionSpark"; import { socialTx } from "@social/lib/socialTx"; import { useSocialI18n } from "@social/lib/useSocialI18n"; import { txCityShort, txMember } from "@social/lib/localizeEntity"; const STORAGE_CHANGE = "nomadro-social-storage"; function subscribeRosterCity(cb: () => void) { window.addEventListener("storage", cb); window.addEventListener(STORAGE_CHANGE, cb); return () => { window.removeEventListener("storage", cb); window.removeEventListener(STORAGE_CHANGE, cb); }; } type FilterId = "all" | "online" | "city" | "liked" | "f" | "m"; const FILTERS: { id: FilterId; label: string; emoji: string }[] = [ { id: "all", label: "全部", emoji: "✨" }, { id: "online", label: "在线", emoji: "🟢" }, { id: "city", label: "同城", emoji: "📍" }, { id: "liked", label: "已关注", emoji: "♥" }, { id: "f", label: "女生", emoji: "👩" }, { id: "m", label: "男生", emoji: "👨" }, ]; function cityKey(city: string) { return city.split(/[·,,]/)[0]?.trim().toLowerCase() || ""; } export default function AttractiveRoster({ onJoin }: { onJoin: () => void }) { useSocialI18n(); // 想见的人:无人脸检测通过的头像一律不进列表(含无 faceOk / 空头像) const roster = useDiscoveryRoster({ requireFace: true }); const likes = useRosterLikes(); const [filter, setFilter] = useState("all"); const [highlightId, setHighlightId] = useState(null); const [burst, setBurst] = useState(false); const burstTimer = useRef | null>(null); const filterId = useId(); const profileCity = useSyncExternalStore( subscribeRosterCity, () => loadJson<{ city?: string }>("roster", {}).city || "", () => "", ); const myCity = useMemo(() => { const self = roster.find((m) => m.id === SELF_MEMBER_ID); return cityKey(self?.city || profileCity); }, [roster, profileCity]); useEffect(() => { const selectMember = (event: Event) => { const id: unknown = (event as CustomEvent).detail; if ( typeof id !== "string" || !roster.some((member) => member.id === id) ) return; setFilter("all"); setHighlightId(id); }; window.addEventListener("nomadro-social-select-member", selectMember); return () => window.removeEventListener("nomadro-social-select-member", selectMember); }, [roster]); useEffect( () => () => { if (burstTimer.current) clearTimeout(burstTimer.current); }, [], ); const list = useMemo(() => { return roster.filter((m) => { if (filter === "online") return m.online; if (filter === "f" || filter === "m") return m.gender === filter; if (filter === "liked") return Boolean(likes[m.id]); if (filter === "city") { if (!myCity) return false; return cityKey(m.city) === myCity; } return true; }); }, [filter, roster, likes, myCity]); const online = useMemo(() => roster.filter((m) => m.online), [roster]); const toggleLike = ( event: { preventDefault(): void; stopPropagation(): void }, m: SocialRosterMember, ) => { event.preventDefault(); event.stopPropagation(); setRosterLike(m.id, !likes[m.id]); setBurst(true); setHighlightId(m.id); if (burstTimer.current) clearTimeout(burstTimer.current); burstTimer.current = setTimeout(() => setBurst(false), 700); }; const emptyCopy = filter === "city" && !myCity ? socialTx("先填写你的城市,才能筛同城的人") : filter === "city" ? socialTx("暂时没有同城的人,先完善介绍或换个城市看看") : filter === "liked" ? socialTx("还没有关注的人,点爱心收藏找搭子") : socialTx("这里暂时没人,去写介绍或加入社区吧"); return (
{socialTx("看看谁在这儿")} {socialTx("点开认识")}

👋 {socialTx("找搭子")}

{socialTx("点开卡片,看看对方在做什么、想认识谁。")} {roster.some((m) => m.id === SELF_MEMBER_ID) ? socialTx("(也包含你写好的介绍)") : ""}

{online.slice(0, 6).map((m) => ( ))}

{online.length}{" "} {socialTx("位现在在线")}

{FILTERS.map((f, index) => ( ))}
{list.length === 0 ? (

{emptyCopy}

{(filter === "city" && !myCity) || filter === "all" ? ( {socialTx("去完善介绍")} ) : null}
) : (
{list.map((m, i) => { const d = txMember(m); return ( setHighlightId(m.id)} > {m.online ? : null} {likes[m.id] ? ( ♥ ) : null} {m.name} {m.id === SELF_MEMBER_ID ? ` · ${socialTx("我")}` : ""} {d.title} {d.vibe ? ` · ${d.vibe.split("·")[0].trim()}` : ""} {txCityShort(m.city)} {d.tags[0] ? ` · ${d.tags[0]}` : ""} {d.lookingFor} toggleLike(e, m)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") toggleLike(e, m); }} > {likes[m.id] ? "♥" : "♡"} ); })}
)} {burst ? ( ♥ ) : null}

{socialTx("👆 点卡片看看详情,也可以先点爱心关注")}

{socialTx("看看更多人")} {socialTx("去左右滑认识")} {socialTx("进群组聊天")}
); }