Tighten mobile Ways/dock/chat/date sheets, add social loading/error boundaries, and mark test8 as the next branch. Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
"use client";
|
|
|
|
import { useMemo, useSyncExternalStore } from "react";
|
|
import { MOCK_ROSTER, type SocialRosterMember } from "./rosterMock";
|
|
import { loadJson, saveJson } from "./storage";
|
|
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: "",
|
|
};
|
|
|
|
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,
|
|
): 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);
|
|
return {
|
|
id: SELF_MEMBER_ID,
|
|
name,
|
|
title: profile.title.trim() || "游民",
|
|
city: profile.city.trim() || "探索中",
|
|
tags: tags.length ? tags : ["新朋友"],
|
|
avatar: `https://api.dicebear.com/7.x/initials/svg?seed=${encodeURIComponent(name)}`,
|
|
bio: profile.bio.trim() || "刚写好介绍,想认识同频的人。",
|
|
vibe: "刚出现",
|
|
online: true,
|
|
lookingFor: "想认识同频游民",
|
|
gender: "f",
|
|
};
|
|
}
|
|
|
|
/** Seed roster + optional self card when user opted into discovery. */
|
|
export function useDiscoveryRoster(): SocialRosterMember[] {
|
|
const published = useSyncExternalStore(
|
|
subscribeSocialStorage,
|
|
() => loadJson<boolean>(ROSTER_PUBLISH_KEY, false),
|
|
() => false,
|
|
);
|
|
// getSnapshot must be referentially stable: JSON.parse creates a new object
|
|
// on every read once a local profile exists, causing a React render loop.
|
|
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;
|
|
return Object.fromEntries(Object.entries(EMPTY).map(([key, fallback]) => [
|
|
key, typeof raw?.[key as keyof SocialRosterProfile] === "string" ? raw[key as keyof SocialRosterProfile] : fallback,
|
|
])) as SocialRosterProfile;
|
|
}, [profileSnapshot]);
|
|
return useMemo(() => {
|
|
const self = published ? profileToMember(profile) : null;
|
|
if (!self) return MOCK_ROSTER;
|
|
return [self, ...MOCK_ROSTER.filter((m) => m.id !== SELF_MEMBER_ID)];
|
|
}, [published, profile]);
|
|
}
|