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>
395 lines
14 KiB
TypeScript
395 lines
14 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import Link from "next/link";
|
||
import { MIROTALK_URL, SEED_CHANNELS } from "@social/lib/seed";
|
||
import { loadJson, saveJson } from "@social/lib/storage";
|
||
import { socialPath } from "@social/lib/socialPath";
|
||
import { resolveSocialCatalog } from "@social/lib/resolveCatalog";
|
||
import { useSocialChat } from "@social/lib/useSocialChat";
|
||
import { useSocialConfig } from "./SocialExperience";
|
||
import { SocialMobileDock } from "./SocialNavigation";
|
||
import SocialAvatar from "./SocialAvatar";
|
||
import { MOCK_ROSTER } from "@social/lib/rosterMock";
|
||
import { EmptyState } from "./SocialUI";
|
||
|
||
type Layout = "split" | "video" | "chat";
|
||
|
||
/** Origins allowed by MiroTalk ALLOWED_EMBED_ORIGINS. */
|
||
const EMBED_OK = new Set([
|
||
"https://nomadro.cn",
|
||
"https://www.nomadro.cn",
|
||
"https://nomadweb.nomadro.com",
|
||
"https://social.nomadro.cn",
|
||
"http://localhost:3000",
|
||
"http://127.0.0.1:3000",
|
||
"http://localhost:3001",
|
||
"http://127.0.0.1:3001",
|
||
]);
|
||
|
||
function joinUrl(room: string, name: string) {
|
||
const q = new URLSearchParams({
|
||
room,
|
||
name,
|
||
notify: "0",
|
||
chat: "0",
|
||
audio: "0",
|
||
video: "0",
|
||
});
|
||
return `${MIROTALK_URL.replace(/\/$/, "")}/join?${q.toString()}`;
|
||
}
|
||
|
||
function canEmbedHere() {
|
||
if (typeof window === "undefined") return true;
|
||
return EMBED_OK.has(window.location.origin);
|
||
}
|
||
|
||
function warmMirotalkOrigin() {
|
||
try {
|
||
const origin = new URL(MIROTALK_URL).origin;
|
||
for (const rel of ["dns-prefetch", "preconnect"] as const) {
|
||
if (document.querySelector(`link[rel="${rel}"][href="${origin}"]`))
|
||
continue;
|
||
const link = document.createElement("link");
|
||
link.rel = rel;
|
||
link.href = origin;
|
||
if (rel === "preconnect") link.crossOrigin = "anonymous";
|
||
document.head.appendChild(link);
|
||
}
|
||
void fetch(`${origin}/js/nomadro-speed.js`, {
|
||
mode: "no-cors",
|
||
credentials: "omit",
|
||
cache: "force-cache",
|
||
}).catch(() => undefined);
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
|
||
function defaultLayout(): Layout {
|
||
if (
|
||
typeof window !== "undefined" &&
|
||
window.matchMedia("(max-width: 768px)").matches
|
||
) {
|
||
return "video";
|
||
}
|
||
return "split";
|
||
}
|
||
|
||
export default function MeetupLiveClient({ meetupId }: { meetupId: string }) {
|
||
const config = useSocialConfig();
|
||
const meetups = useMemo(
|
||
() => resolveSocialCatalog(config.copy.socialCatalogs).meetups,
|
||
[config.copy.socialCatalogs],
|
||
);
|
||
const meetup =
|
||
meetups.find((m) => m.id === meetupId) ||
|
||
{
|
||
id: meetupId,
|
||
title: "开视频见面",
|
||
city: "Online",
|
||
date: "",
|
||
time: "",
|
||
mode: "online" as const,
|
||
mirotalkRoom: meetupId,
|
||
channelId: "meetup-live",
|
||
};
|
||
const channel =
|
||
SEED_CHANNELS.find((c) => c.id === meetup.channelId) || SEED_CHANNELS[0];
|
||
const chat = useSocialChat(channel.id);
|
||
|
||
const [layout, setLayout] = useState<Layout>("split");
|
||
const [joinName, setJoinName] = useState("");
|
||
const [videoReady, setVideoReady] = useState(false);
|
||
const [embedBlocked, setEmbedBlocked] = useState(false);
|
||
const [iframeLoading, setIframeLoading] = useState(true);
|
||
const [frameKey, setFrameKey] = useState(0);
|
||
const [loadSlow, setLoadSlow] = useState(false);
|
||
const bottomRef = useRef<HTMLDivElement>(null);
|
||
|
||
const videoSrc = useMemo(
|
||
() => joinUrl(meetup.mirotalkRoom, joinName || "游民"),
|
||
[meetup.mirotalkRoom, joinName],
|
||
);
|
||
const preferEmbed = canEmbedHere() && !embedBlocked;
|
||
|
||
useEffect(() => {
|
||
setLayout(defaultLayout());
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
const name = (loadJson("nick", "游民") || "游民").trim() || "游民";
|
||
chat.setNick(name);
|
||
setJoinName(name);
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps -- mount once
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (!videoReady || !iframeLoading) return;
|
||
const timer = setTimeout(() => setLoadSlow(true), 15000);
|
||
return () => clearTimeout(timer);
|
||
}, [videoReady, iframeLoading, frameKey]);
|
||
|
||
useEffect(() => {
|
||
const thread = bottomRef.current?.parentElement;
|
||
if (thread) thread.scrollTop = thread.scrollHeight;
|
||
}, [chat.messages]);
|
||
|
||
const startVideo = useCallback(
|
||
(forceReload = false) => {
|
||
const name = chat.nick.trim() || "游民";
|
||
saveJson("nick", name);
|
||
warmMirotalkOrigin();
|
||
setJoinName(name);
|
||
if (!preferEmbed) {
|
||
window.location.assign(joinUrl(meetup.mirotalkRoom, name));
|
||
return;
|
||
}
|
||
setIframeLoading(true);
|
||
setLoadSlow(false);
|
||
if (forceReload && videoReady) {
|
||
setFrameKey((key) => key + 1);
|
||
} else {
|
||
setVideoReady(true);
|
||
}
|
||
},
|
||
[chat.nick, meetup.mirotalkRoom, preferEmbed, videoReady],
|
||
);
|
||
|
||
const send = async () => {
|
||
if (!chat.draft.trim() || chat.sending) return;
|
||
await chat.send(chat.draft);
|
||
};
|
||
|
||
if (!meetups.some((item) => item.id === meetupId)) {
|
||
return <div className="social-root social-live-root social-meetup-live"><EmptyState title="没有找到这个场次" description="场次可能已下线,请返回选择其他活动。"><Link className="social-btn primary" href={socialPath("/meetup")}>查看全部场次</Link></EmptyState><SocialMobileDock /></div>;
|
||
}
|
||
|
||
return (
|
||
<div className="social-root social-live-root social-skin social-meetup-live">
|
||
<header className="social-live-bar">
|
||
<div className="social-live-bar-main">
|
||
<Link href={socialPath("/meetup")} className="social-nav-back">
|
||
← 回去看场次
|
||
</Link>
|
||
<div>
|
||
<strong>{meetup.title}</strong>
|
||
<p className="social-muted">
|
||
{meetup.date} {meetup.time} · {channel.name}
|
||
</p>
|
||
</div>
|
||
<span
|
||
className={`social-live-conn ${chat.status === "offline" ? "is-off" : chat.status === "online" ? "is-on" : ""}`}
|
||
role="status"
|
||
>
|
||
{chat.status === "online"
|
||
? "聊天已接通"
|
||
: chat.status === "offline"
|
||
? "聊天暂时断了"
|
||
: chat.status === "preview"
|
||
? "先看看示例"
|
||
: "正在连上…"}
|
||
</span>
|
||
</div>
|
||
<div className="social-live-bar-tools">
|
||
{videoReady && <div className="social-actions">
|
||
<button type="button" className="social-btn" onClick={() => { setVideoReady(false); setLoadSlow(false); }}>离开视频</button>
|
||
<button type="button" className="social-btn" onClick={() => startVideo(true)}>重新连接</button>
|
||
<a className="social-btn" href={videoSrc} target="_blank" rel="noopener noreferrer">新窗口打开</a>
|
||
</div>}
|
||
<label className="social-live-nick">
|
||
<span className="social-muted">昵称</span>
|
||
<input
|
||
value={chat.nick}
|
||
onChange={(e) => chat.setNick(e.target.value)}
|
||
onBlur={() => saveJson("nick", chat.nick.trim() || "游民")}
|
||
maxLength={24}
|
||
aria-label="显示名"
|
||
/>
|
||
</label>
|
||
{videoReady &&
|
||
preferEmbed &&
|
||
chat.nick.trim() &&
|
||
chat.nick.trim() !== joinName ? (
|
||
<button
|
||
type="button"
|
||
className="social-btn"
|
||
onClick={() => startVideo(true)}
|
||
title="用新名字重新进入视频"
|
||
>
|
||
换名字重进
|
||
</button>
|
||
) : null}
|
||
<div className="social-live-tabs" role="tablist" aria-label="怎么看">
|
||
{(["split", "video", "chat"] as const).map((m) => (
|
||
<button
|
||
key={m}
|
||
type="button"
|
||
role="tab"
|
||
aria-selected={layout === m}
|
||
tabIndex={layout === m ? 0 : -1}
|
||
onKeyDown={(event) => {
|
||
const modes = ["split", "video", "chat"] as const;
|
||
const index = modes.indexOf(m);
|
||
const next = event.key === "ArrowRight" ? (index + 1) % 3 : event.key === "ArrowLeft" ? (index + 2) % 3 : event.key === "Home" ? 0 : event.key === "End" ? 2 : -1;
|
||
if (next < 0) return;
|
||
event.preventDefault();
|
||
setLayout(modes[next]);
|
||
event.currentTarget.parentElement?.querySelectorAll<HTMLButtonElement>("button")[next]?.focus();
|
||
}}
|
||
className={`social-live-tab${layout === m ? " active" : ""}`}
|
||
onClick={() => setLayout(m)}
|
||
>
|
||
{m === "split" ? "一起看" : m === "video" ? "只看视频" : "只看聊天"}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<div className={`social-live-stage layout-${layout}`}>
|
||
{(layout === "split" || layout === "video") && (
|
||
<section className="social-live-video" aria-label="开视频">
|
||
{!videoReady ? (
|
||
<div className="social-live-gate">
|
||
<div className="social-live-radar" aria-hidden>
|
||
<i />
|
||
<i />
|
||
<i />
|
||
<span>LIVE</span>
|
||
</div>
|
||
<h2>群组 · 语音视频</h2>
|
||
<p>
|
||
{preferEmbed
|
||
? "先听群里聊几句也行;麦克风和摄像头先关着,进去后再打开。"
|
||
: "这里暂时不能直接嵌视频,点下面会跳转到语音视频页。"}
|
||
</p>
|
||
<div className="social-meetup-faces social-live-faces" aria-hidden>
|
||
{MOCK_ROSTER.filter((m) => m.online)
|
||
.slice(0, 5)
|
||
.map((m) => (
|
||
<SocialAvatar key={m.id} src={m.avatar} size={28} lazy />
|
||
))}
|
||
<em>示例成员 · 可先进入了解</em>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
className="social-btn primary"
|
||
onClick={() => startVideo()}
|
||
>
|
||
进入聊聊
|
||
</button>
|
||
<p className="social-muted">
|
||
「想参加」还不等于已经约好 · 随时可以离开回去看别的场
|
||
</p>
|
||
</div>
|
||
) : preferEmbed ? (
|
||
<div className="social-live-frame-wrap">
|
||
{iframeLoading && (
|
||
<div
|
||
className="social-live-iframe-overlay social-live-iframe-overlay--thin"
|
||
aria-live="polite"
|
||
>
|
||
<div className="social-live-iframe-mark" aria-hidden>
|
||
N
|
||
</div>
|
||
<p>{loadSlow ? "连接较慢,可以重试或在新窗口打开" : "接通中…"}</p>
|
||
{loadSlow && <button type="button" className="social-btn" onClick={() => startVideo(true)}>重试连接</button>}
|
||
</div>
|
||
)}
|
||
<iframe
|
||
key={frameKey}
|
||
title={`${meetup.title} video`}
|
||
src={videoSrc}
|
||
className="social-live-iframe"
|
||
allow="camera; microphone; display-capture; autoplay; fullscreen; clipboard-write"
|
||
referrerPolicy="no-referrer-when-downgrade"
|
||
loading="eager"
|
||
onLoad={() => { setIframeLoading(false); setLoadSlow(false); }}
|
||
onError={() => {
|
||
setEmbedBlocked(true);
|
||
setIframeLoading(false);
|
||
}}
|
||
/>
|
||
</div>
|
||
) : (
|
||
<div className="social-live-gate">
|
||
<h2>本页跳转进房</h2>
|
||
<p>不新开标签;浏览器返回即可回到游民社区。</p>
|
||
<button
|
||
type="button"
|
||
className="social-btn primary"
|
||
onClick={() => window.location.assign(videoSrc)}
|
||
>
|
||
立即进入
|
||
</button>
|
||
</div>
|
||
)}
|
||
</section>
|
||
)}
|
||
|
||
{(layout === "split" || layout === "chat") && (
|
||
<section className="social-live-chat" aria-label="文字频道">
|
||
<div className="social-live-chat-head">
|
||
<strong>{channel.name}</strong>
|
||
<span className="social-muted">{channel.topic}</span>
|
||
</div>
|
||
<div className="social-chat-thread social-live-thread">
|
||
{!chat.messages.length ? (
|
||
<div className="social-live-empty">
|
||
<strong>文字频道还很安静</strong>
|
||
<p>先打个招呼,或问问今晚谁在听。</p>
|
||
</div>
|
||
) : null}
|
||
{chat.messages.map((m) => (
|
||
<div
|
||
key={m.id}
|
||
className={`social-bubble${m.mine ? " mine" : ""}`}
|
||
>
|
||
<header>
|
||
{m.author}
|
||
<time suppressHydrationWarning>
|
||
{new Date(m.at).toLocaleTimeString([], {
|
||
hour: "2-digit",
|
||
minute: "2-digit",
|
||
})}
|
||
</time>
|
||
{m.delivery === "demo" ? <span>示例</span> : null}
|
||
{m.delivery === "local" ? <span>先留在你这边</span> : null}
|
||
</header>
|
||
<p>{m.text}</p>
|
||
</div>
|
||
))}
|
||
<div ref={bottomRef} />
|
||
</div>
|
||
<div className="social-compose">
|
||
<input
|
||
value={chat.draft}
|
||
onChange={(e) => chat.setDraft(e.target.value)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing && e.keyCode !== 229) {
|
||
e.preventDefault();
|
||
void send();
|
||
}
|
||
}}
|
||
placeholder="发到活动文字频道…"
|
||
aria-label="消息内容"
|
||
maxLength={2000}
|
||
/>
|
||
<button
|
||
type="button"
|
||
className="social-btn primary"
|
||
disabled={!chat.draft.trim() || chat.sending}
|
||
onClick={() => void send()}
|
||
>
|
||
发送
|
||
</button>
|
||
</div>
|
||
</section>
|
||
)}
|
||
</div>
|
||
<SocialMobileDock />
|
||
</div>
|
||
);
|
||
}
|