nomadro/src/modules/social/components/AttractiveRoster.tsx
Eric b06a14b6aa Ship social admin nested menus with list/detail CRUD, soft-archive UGC, and PocketBase-ready stores.
admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 16:41:43 +08:00

327 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,
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";
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 }) {
const roster = useDiscoveryRoster();
const likes = useRosterLikes();
const [filter, setFilter] = useState<FilterId>("all");
const [highlightId, setHighlightId] = useState<string | null>(null);
const [burst, setBurst] = useState(false);
const burstTimer = useRef<ReturnType<typeof setTimeout> | 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 (
<section
className="section section-compact"
id="people"
aria-label={socialTx("在线游民")}
>
<div className="container">
<div className="section-header reveal">
<SectionSpark label="people" />
<span className="section-tag">
<span>{socialTx("看看谁在这儿")}</span>
<span>{socialTx("点开认识")}</span>
</span>
<h2>
<span className="social-title-emoji" aria-hidden>
👋
</span>
{socialTx("想见的人")}
</h2>
<p>
{socialTx("点开卡片,看看对方在做什么、想认识谁。")}
{socialTx("现在是示例介绍,方便你先逛逛")}
{roster.some((m) => m.id === SELF_MEMBER_ID)
? socialTx("(也包含你写好的介绍)")
: ""}
</p>
</div>
<div className="social-people-bar reveal">
<div className="social-people-bar-avs" aria-hidden>
{online.slice(0, 6).map((m) => (
<SocialAvatar key={m.id} src={m.avatar} size={30} lazy />
))}
</div>
<p>
<strong className="social-count-pulse">{online.length}</strong>{" "}
{socialTx("位现在在线")}
<span> · {socialTx("示例数据")}</span>
</p>
<button
type="button"
className="btn btn-primary btn-sm social-btn-pulse"
onClick={onJoin}
>
{socialTx("加入社区认识")}
</button>
</div>
<div
className="social-people-filters reveal"
role="tablist"
aria-label={socialTx("筛选")}
>
{FILTERS.map((f, index) => (
<button
key={f.id}
type="button"
role="tab"
id={`${filterId}-${f.id}`}
aria-controls={`${filterId}-panel`}
aria-selected={filter === f.id}
tabIndex={filter === f.id ? 0 : -1}
className={`social-people-filter${filter === f.id ? " active" : ""}`}
onClick={() => {
setFilter(f.id);
setHighlightId(null);
}}
onKeyDown={(event) => {
const key = event.key;
if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(key))
return;
event.preventDefault();
const nextIndex =
key === "Home"
? 0
: key === "End"
? FILTERS.length - 1
: (index +
(key === "ArrowRight" ? 1 : -1) +
FILTERS.length) %
FILTERS.length;
const next = FILTERS[nextIndex];
setFilter(next.id);
setHighlightId(null);
document.getElementById(`${filterId}-${next.id}`)?.focus();
}}
>
<span aria-hidden>{f.emoji}</span> {socialTx(f.label)}
</button>
))}
</div>
{list.length === 0 ? (
<div className="social-people-empty reveal">
<p>{emptyCopy}</p>
<div className="social-section-actions">
{(filter === "city" && !myCity) || filter === "all" ? (
<Link href={socialPath("/roster")} className="btn btn-primary">
{socialTx("去完善介绍")}
</Link>
) : null}
<button type="button" className="btn btn-ghost" onClick={onJoin}>
{socialTx("加入社区")}
</button>
<button
type="button"
className="btn btn-ghost"
onClick={() => setFilter("all")}
>
{socialTx("看全部")}
</button>
</div>
</div>
) : (
<div
className="social-people-grid"
id={`${filterId}-panel`}
role="tabpanel"
aria-labelledby={`${filterId}-${filter}`}
>
{list.map((m, i) => (
<Link
key={m.id}
href={socialPath(`/people/${m.id}`)}
className={`social-people-chip social-chip-pop is-rich${m.online ? " is-online" : ""}${likes[m.id] ? " is-liked" : ""}${highlightId === m.id ? " is-active" : ""}`}
style={{ ["--stagger" as string]: `${Math.min(i, 12) * 40}ms` }}
onClick={() => setHighlightId(m.id)}
>
<span className="social-people-chip-ava">
<SocialAvatar src={m.avatar} size={52} lazy />
{m.online ? <i aria-hidden /> : null}
{likes[m.id] ? (
<em className="social-chip-heart" aria-hidden>
♥
</em>
) : null}
</span>
<strong>
{m.name}
{m.id === SELF_MEMBER_ID ? ` · ${socialTx("我")}` : ""}
</strong>
<span className="connection-member-role">
{m.title}
{m.vibe ? ` · ${m.vibe.split("·")[0].trim()}` : ""}
</span>
<em>
{m.city.split("·")[0].trim()}
{m.tags[0] ? ` · ${m.tags[0]}` : ""}
</em>
<span className="social-people-chip-want" title={m.lookingFor}>
{m.lookingFor}
</span>
<span
role="button"
tabIndex={0}
className="social-people-chip-like"
aria-label={
likes[m.id] ? socialTx("取消关注") : socialTx("关注")
}
onClick={(e) => toggleLike(e, m)}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") toggleLike(e, m);
}}
>
{likes[m.id] ? "♥" : "♡"}
</span>
</Link>
))}
</div>
)}
{burst ? (
<span className="social-burst social-burst-float" aria-hidden>
♥
</span>
) : null}
<p className="social-people-hint">
{socialTx("👆 点卡片看看详情,也可以先点爱心关注")}
</p>
<div className="social-section-actions reveal">
<Link href={socialPath("/people")} className="btn btn-primary">
{socialTx("看看更多人")}
</Link>
<Link href={socialPath("/date")} className="btn btn-ghost">
{socialTx("去左右滑认识")}
</Link>
<Link href={socialPath("/chat")} className="btn btn-ghost">
{socialTx("进群组聊天")}
</Link>
<button type="button" className="btn btn-ghost" onClick={onJoin}>
{socialTx("加入社区")}
</button>
</div>
</div>
</section>
);
}