Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence. Co-authored-by: Cursor <cursoragent@cursor.com>
246 lines
7.8 KiB
TypeScript
246 lines
7.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
import { useAudienceModule } from "~/modules/audience";
|
|
import { useRemoteConfig } from "~/lib/use-remote-config";
|
|
import { socialApi } from "./api";
|
|
import { MOCK_ROSTER, type SocialRosterMember } from "./rosterMock";
|
|
import { mapRosterAvatarPack } from "./rosterAvatars";
|
|
import {
|
|
discoveryItemToMember,
|
|
sortDiscoveryMembers,
|
|
} from "./rankDiscovery";
|
|
import { isFaceOk } from "./livenessGate";
|
|
import { loadJson, saveJson } from "./storage";
|
|
import {
|
|
getSocialLive,
|
|
socialLiveDiscoveryMs,
|
|
} from "./socialLive";
|
|
import { socialTrack } from "./socialTrack";
|
|
import type { SocialRosterProfile } from "./types";
|
|
|
|
export const ROSTER_PUBLISH_KEY = "roster-publish";
|
|
export const ROSTER_PUBLISH_CHANGED = "social-roster-publish-changed";
|
|
export const SELF_MEMBER_ID = "self-local";
|
|
|
|
const EMPTY: SocialRosterProfile = {
|
|
name: "",
|
|
title: "",
|
|
city: "",
|
|
phone: "",
|
|
linktree: "",
|
|
skills: "",
|
|
bio: "",
|
|
avatar: "",
|
|
intents: "",
|
|
availability: "",
|
|
status: "",
|
|
lookingFor: "",
|
|
gender: "",
|
|
};
|
|
|
|
const STORAGE_CHANGE = "nomadro-social-storage";
|
|
|
|
function subscribeSocialStorage(cb: () => void) {
|
|
window.addEventListener("storage", cb);
|
|
window.addEventListener(STORAGE_CHANGE, cb);
|
|
window.addEventListener(ROSTER_PUBLISH_CHANGED, cb);
|
|
return () => {
|
|
window.removeEventListener("storage", cb);
|
|
window.removeEventListener(STORAGE_CHANGE, cb);
|
|
window.removeEventListener(ROSTER_PUBLISH_CHANGED, cb);
|
|
};
|
|
}
|
|
|
|
export function setRosterPublished(published: boolean) {
|
|
saveJson(ROSTER_PUBLISH_KEY, published);
|
|
window.dispatchEvent(new Event(STORAGE_CHANGE));
|
|
window.dispatchEvent(new Event(ROSTER_PUBLISH_CHANGED));
|
|
}
|
|
|
|
export function profileToMember(
|
|
profile: SocialRosterProfile,
|
|
_avatarPack: "cn" | "intl" = "intl",
|
|
): SocialRosterMember | null {
|
|
const name = profile.name.trim();
|
|
if (!name) return null;
|
|
const tags = profile.skills
|
|
.split(/[,,、]/)
|
|
.map((t) => t.trim())
|
|
.filter(Boolean)
|
|
.slice(0, 6);
|
|
const own = (profile.avatar || "").trim();
|
|
const ownOk =
|
|
own.startsWith("https://") ||
|
|
own.startsWith("http://") ||
|
|
own.startsWith("/api/social/");
|
|
// Only real uploaded avatar + local face flag count for dating / public strips
|
|
const faceOk = ownOk && isFaceOk();
|
|
// Never publish no-face album URL (or initials/dicebear) onto home/modules
|
|
const avatar = faceOk
|
|
? own
|
|
: "";
|
|
const status = (profile.status || "online").trim();
|
|
const online = status === "online" || status === "here" || status === "available";
|
|
return {
|
|
id: SELF_MEMBER_ID,
|
|
name,
|
|
title: profile.title.trim() || "游民",
|
|
city: profile.city.trim() || "探索中",
|
|
tags: tags.length ? tags : ["新朋友"],
|
|
avatar,
|
|
bio: profile.bio.trim() || "刚写好介绍,想认识同频的人。",
|
|
vibe: status === "here" ? "同城想出来" : online ? "在线" : "刚出现",
|
|
online,
|
|
status,
|
|
lookingFor:
|
|
(profile.lookingFor || "").trim() ||
|
|
(profile.intents || "").replace(/,/g, " · ") ||
|
|
"想认识同频游民",
|
|
gender: profile.gender === "m" ? "m" : "f",
|
|
availability: (profile.availability || "").trim() || undefined,
|
|
faceOk,
|
|
};
|
|
}
|
|
|
|
function readLocalProfile(): SocialRosterProfile {
|
|
const raw = loadJson<Partial<SocialRosterProfile>>("roster", EMPTY);
|
|
const next = { ...EMPTY };
|
|
(Object.keys(EMPTY) as (keyof SocialRosterProfile)[]).forEach((key) => {
|
|
const val = raw?.[key];
|
|
if (typeof val === "string") next[key] = val;
|
|
});
|
|
return next;
|
|
}
|
|
|
|
/**
|
|
* Discovery list: live API (same-city / online / tags) → merge mock fallback →
|
|
* optional self card. Ranked for local viewer profile.
|
|
*
|
|
* requireFace: dating feed — hide profiles without a detected face in avatar.
|
|
*/
|
|
export function useDiscoveryRoster(_opts?: {
|
|
requireFace?: boolean;
|
|
}): SocialRosterMember[] {
|
|
const { bundle } = useAudienceModule();
|
|
const { config } = useRemoteConfig();
|
|
const live = getSocialLive(config);
|
|
// Public discovery: always hide no-face (admin switch cannot turn this off).
|
|
const requireFace = true;
|
|
void _opts;
|
|
const pack = bundle.module.social.rosterAvatarPack;
|
|
const published = useSyncExternalStore(
|
|
subscribeSocialStorage,
|
|
() => loadJson<boolean>(ROSTER_PUBLISH_KEY, false),
|
|
() => false,
|
|
);
|
|
const profileSnapshot = useSyncExternalStore(
|
|
subscribeSocialStorage,
|
|
() => JSON.stringify(loadJson<SocialRosterProfile>("roster", EMPTY)),
|
|
() => JSON.stringify(EMPTY),
|
|
);
|
|
const profile = useMemo(() => {
|
|
const raw = JSON.parse(profileSnapshot) as Partial<SocialRosterProfile> | null;
|
|
const next = { ...EMPTY };
|
|
(Object.keys(EMPTY) as (keyof SocialRosterProfile)[]).forEach((key) => {
|
|
const val = raw?.[key];
|
|
if (typeof val === "string") next[key] = val;
|
|
});
|
|
return next;
|
|
}, [profileSnapshot]);
|
|
|
|
const [remote, setRemote] = useState<SocialRosterMember[] | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
let timer: ReturnType<typeof setInterval> | null = null;
|
|
|
|
const pull = () => {
|
|
void socialApi
|
|
.discoveryRoster({ limit: 48, requireFace })
|
|
.then((res) => {
|
|
if (cancelled) return;
|
|
if (!res?.items?.length) {
|
|
setRemote([]);
|
|
return;
|
|
}
|
|
setRemote(res.items.map(discoveryItemToMember));
|
|
socialTrack(live, "social_discovery_refresh", {
|
|
count: res.items.length,
|
|
requireFace: requireFace ? 1 : 0,
|
|
});
|
|
})
|
|
.catch(() => {
|
|
/* offline → keep mock / last good */
|
|
});
|
|
};
|
|
|
|
pull();
|
|
if (live.discoveryAutoRefresh) {
|
|
timer = setInterval(pull, socialLiveDiscoveryMs(live));
|
|
}
|
|
const onVis = () => {
|
|
if (!document.hidden) pull();
|
|
};
|
|
const onOnline = () => pull();
|
|
document.addEventListener("visibilitychange", onVis);
|
|
window.addEventListener("online", onOnline);
|
|
return () => {
|
|
cancelled = true;
|
|
if (timer) clearInterval(timer);
|
|
document.removeEventListener("visibilitychange", onVis);
|
|
window.removeEventListener("online", onOnline);
|
|
};
|
|
}, [
|
|
profile.city,
|
|
profile.skills,
|
|
profile.intents,
|
|
requireFace,
|
|
live.discoveryAutoRefresh,
|
|
live.discoveryRefreshSec,
|
|
]);
|
|
|
|
const seed = useMemo(
|
|
() => mapRosterAvatarPack(MOCK_ROSTER, pack),
|
|
[pack],
|
|
);
|
|
|
|
return useMemo(() => {
|
|
const live = remote || [];
|
|
const liveIds = new Set(live.map((m) => m.id));
|
|
// Prefer real people; pad with mock so empty cities still feel alive
|
|
// Dating: mock portraits count as face-ok; never pad with no-face remotes
|
|
const mocks = seed
|
|
.filter((m) => !liveIds.has(m.id))
|
|
.map((m) => ({ ...m, faceOk: true })); // stock portraits only
|
|
const merged = [
|
|
...live,
|
|
...(requireFace ? mocks.filter((m) => m.faceOk === true) : mocks),
|
|
];
|
|
const ranked = sortDiscoveryMembers(merged, profile).filter((m) => {
|
|
if (!requireFace) return true;
|
|
const av = (m.avatar || "").trim();
|
|
if (m.faceOk !== true) return false;
|
|
if (!av) return false;
|
|
if (av.includes("nomadro.webp") || av.startsWith("data:")) return false;
|
|
return (
|
|
av.startsWith("https://") ||
|
|
av.startsWith("http://") ||
|
|
av.startsWith("/api/social/")
|
|
);
|
|
});
|
|
// Dating never shows self — even if published / faceOk wrong in PB
|
|
if (requireFace) {
|
|
return ranked.filter((m) => m.id !== SELF_MEMBER_ID);
|
|
}
|
|
const self = published ? profileToMember(profile, pack) : null;
|
|
if (!self) return ranked;
|
|
return [self, ...ranked.filter((m) => m.id !== SELF_MEMBER_ID)];
|
|
}, [published, profile, seed, pack, remote, requireFace]);
|
|
}
|
|
|
|
/** One-shot helper for non-hook call sites. */
|
|
export function getLocalViewerProfile(): SocialRosterProfile {
|
|
return readLocalProfile();
|
|
}
|