nomadro/src/modules/social/components/ResourceLibrary.tsx
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 02:25:05 +08:00

474 lines
16 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 Link from "next/link";
import { useRef, useState, useSyncExternalStore, type FormEvent } from "react";
import {
ArrowUpRight,
BookOpen,
Film,
MessageCircle,
PenLine,
Play,
Send,
Users,
} from "lucide-react";
import SiteShell from "./SiteShell";
import {
BookmarkButton,
DemoBadge,
EmptyState,
PageIntro,
SearchField,
ResultSummary,
ShareButton,
SocialDialog,
} from "./SocialUI";
import { useSocialConfig } from "./SocialExperience";
import { SEED_EBOOKS, SEED_VIDEOS } from "@social/lib/seed";
import { useLocalState } from "@social/lib/useLocalState";
import { socialPath } from "@social/lib/socialPath";
import { socialApi } from "@social/lib/api";
import { socialTx } from "@social/lib/socialTx";
type Kind = "videos" | "date" | "ebook";
const COPY = {
videos: { title: "共创 · 视频", accent: "大家拍的" },
date: { title: "共创 · 访谈", accent: "听听大家" },
ebook: { title: "共创 · 小册", accent: "大家写的" },
};
function subscribeSelection(callback: () => void) {
window.addEventListener("hashchange", callback);
window.addEventListener("popstate", callback);
return () => {
window.removeEventListener("hashchange", callback);
window.removeEventListener("popstate", callback);
};
}
const readSelection = () => location.hash.slice(1);
const emptySelection = () => "";
function setSelection(id: string | null) {
const url = new URL(location.href);
url.hash = id || "";
history.replaceState(history.state, "", url);
window.dispatchEvent(new Event("hashchange"));
}
function safeSource(value?: string) {
if (!value) return undefined;
try {
const url = new URL(value, "https://nomadro.cn");
return url.protocol === "https:" || url.protocol === "http:"
? value
: undefined;
} catch {
return undefined;
}
}
function ResourcePlayer({ source }: { source: string }) {
const [failed, setFailed] = useState(false);
return (
<>
<video
className="social-resource-player"
controls
playsInline
preload="metadata"
src={source}
onError={() => setFailed(true)}
onLoadedMetadata={() => setFailed(false)}
/>
{failed && (
<p role="status" className="social-status warn">
{socialTx("视频暂时播不了,过一会儿再试试。")}
</p>
)}
</>
);
}
function ContributeForm({
kind,
onClose,
}: {
kind: Kind;
onClose: () => void;
}) {
const books = kind === "ebook";
const [draft, setDraft] = useLocalState(`cocreate-draft:${kind}`, {
name: "",
contact: "",
title: "",
note: "",
});
// v2 only records real API acknowledgements; v1 was a local-only timer.
const [sent, setSent] = useLocalState(`cocreate-delivered-v2:${kind}`, false);
const [busy, setBusy] = useState(false);
const [status, setStatus] = useState("");
const lock = useRef(false);
const update = (key: keyof typeof draft, value: string) => {
setDraft((c) => ({ ...c, [key]: value }));
setSent(false);
setStatus("");
};
const submit = async (event: FormEvent) => {
event.preventDefault();
if (lock.current || !draft.name.trim() || !draft.contact.trim()) return;
lock.current = true;
setBusy(true);
try {
await socialApi.postFeedback({
kind: "建议",
text: `【共创${books ? "小册" : "视频 / 访谈"}投稿】\n称呼:${draft.name.trim()}\n主题:${draft.title.trim() || "待讨论"}\n${draft.note.trim()}`,
contact: draft.contact.trim(),
});
setSent(true);
} catch {
setStatus("暂时未发送,草稿已保留在当前设备。请重试提交。");
} finally {
setBusy(false);
lock.current = false;
}
};
if (sent) {
return (
<div className="social-apply-done">
<span className="social-apply-done-mark" aria-hidden>
<PenLine size={20} />
</span>
<strong>{socialTx("共创意向已提交")}</strong>
<p>
{socialTx("已发送给社区运营。请保留联系方式,方便后续沟通。")}
</p>
<div className="social-actions">
<Link className="social-btn primary" href={socialPath("/team")}>
<Users size={14} />
{socialTx("去找搭子")}
</Link>
<button type="button" className="social-btn" onClick={onClose}>
{socialTx("关闭")}
</button>
<button type="button" className="social-btn" onClick={() => { setSent(false); setDraft({ name: "", contact: "", title: "", note: "" }); }}>{socialTx("再写一个主题")}</button>
</div>
</div>
);
}
return (
<div className="social-apply">
<div className="social-apply-hero">
<span className="social-apply-kind">{books ? "小册" : "视频 / 访谈"}</span>
<strong>{socialTx("想一起做内容?")}</strong>
<p className="social-role-brief">
{socialTx("写个主题或素材想法就行。不用一次写完,先表达兴趣。")}
</p>
</div>
<form
className="social-form social-apply-form"
onSubmit={(event) => void submit(event)}
>
<fieldset disabled={busy} className="social-fieldset">
<label>
{socialTx("怎么称呼你")}
<input
required
maxLength={64}
placeholder={socialTx("昵称")}
value={draft.name}
onChange={(e) => update("name", e.target.value)}
/>
</label>
<label>
{socialTx("联系方式")}
<input
required
maxLength={120}
placeholder={socialTx("邮箱 / 微信…")}
value={draft.contact}
onChange={(e) => update("contact", e.target.value)}
/>
</label>
<label>
{socialTx("主题 / 暂定标题")}
<input
maxLength={120}
placeholder={books ? "想写的小册方向" : "想拍 / 想聊的话题"}
value={draft.title}
onChange={(e) => update("title", e.target.value)}
/>
</label>
<label>
{socialTx("一句话介绍")}
<textarea
rows={4}
maxLength={1200}
placeholder={socialTx("你有什么素材、经验,或想找谁一起做…")}
value={draft.note}
onChange={(e) => update("note", e.target.value)}
/>
</label>
</fieldset>
<div className="social-actions">
<button
type="submit"
className="social-btn primary"
disabled={busy || !draft.name.trim() || !draft.contact.trim()}
>
<Send size={14} />
{busy ? "提交中…" : status ? "重试提交" : "提交意向"}
</button>
<button type="button" className="social-btn" onClick={onClose}>
{socialTx("取消")}
</button>
</div>
{status && <p role="status" className="social-status warn">{status}</p>}
<small className="social-muted">
{socialTx("草稿离线也能写。提交不等于立刻上线,只是先把想法放进来。")}
</small>
</form>
</div>
);
}
export default function ResourceLibrary({ kind }: { kind: Kind }) {
const [query, setQuery] = useState("");
const [savedOnly, setSavedOnly] = useState(false);
const selected = useSyncExternalStore(subscribeSelection, readSelection, emptySelection);
const [contributeOpen, setContributeOpen] = useState(false);
const config = useSocialConfig();
const [saved] = useLocalState<string[]>(`saved-${kind}`, []);
const books = kind === "ebook";
const items = books
? SEED_EBOOKS.map((book) => ({
...book,
summary: book.blurb,
city: "共创",
coverEmoji: "✦",
detail: `${book.chapters} 章 · ${book.minutes} 分钟`,
}))
: SEED_VIDEOS.map((video) => ({
...video,
detail: `${video.city} · ${video.minutes} 分钟`,
}));
const shown = items.filter(
(item) =>
(!savedOnly || saved.includes(item.id)) &&
`${item.title} ${item.summary} ${item.city}`
.toLowerCase()
.includes(query.toLowerCase().trim()),
);
const active = items.find((item) => item.id === selected);
const source =
active && safeSource(config.copy.socialVideoSources?.[active.id]);
return (
<SiteShell
title={books ? "共创 · 小册" : kind === "date" ? "共创 · 访谈" : "共创 · 视频"}
>
<PageIntro {...COPY[kind]}>
<div className="social-actions social-cocreate-switch">
<Link
className="social-btn"
href={socialPath("/team")}
>
<Users size={14} />
{socialTx("找搭子")}
</Link>
<Link
className={`social-btn${books ? " primary" : ""}`}
href={socialPath("/ebook")}
>
{socialTx("小册")}
</Link>
<Link
className={`social-btn${!books && kind === "videos" ? " primary" : ""}`}
href={socialPath("/videos")}
>
{socialTx("视频")}
</Link>
<DemoBadge>
{books ? "先看看有哪些" : "有的可以看,有的先看封面"}
</DemoBadge>
</div>
</PageIntro>
<p className="social-muted social-event-lead reveal">
{socialTx("小册和视频都算「共创」——先逛逛现成的,也可以提交你想做的主题。")}
</p>
<div className="social-toolbar reveal">
<SearchField
value={query}
onChange={setQuery}
placeholder={books ? "书名 / 主题" : "城市 / 关键词"}
/>
<div className="social-filters">
<button
type="button"
className={!savedOnly ? "active" : ""}
aria-pressed={!savedOnly}
onClick={() => setSavedOnly(false)}
>
{socialTx("全部")}
</button>
<button
type="button"
className={savedOnly ? "active" : ""}
aria-pressed={savedOnly}
onClick={() => setSavedOnly(true)}
>
收藏 · {saved.length}
</button>
<button
type="button"
className="social-btn primary social-contribute-btn"
onClick={() => setContributeOpen(true)}
>
<PenLine size={14} />
{socialTx("我想投稿")}
</button>
</div>
</div>
<ResultSummary count={shown.length} unit={books ? "本小册" : "个视频"} filtered={Boolean(query || savedOnly)} onReset={() => { setQuery(""); setSavedOnly(false); }} />
<div
className={`social-grid social-library-grid ${books ? "is-books" : ""}`}
>
{shown.map((item, index) => (
<article
id={item.id}
className="social-card social-library-card social-panel-fx reveal"
key={item.id}
style={{ ["--stagger" as string]: `${index * 70}ms` }}
>
<button
type="button"
className={`social-library-art library-tone-${index % 3}`}
onClick={() => setSelection(item.id)}
aria-label={`查看${item.title}`}
>
<span className="social-film-grain" />
<span className="social-library-number">
{String(index + 1).padStart(2, "0")}
</span>
{books ? (
<div className="social-book-cover">
<small>{socialTx("共创")}</small>
<BookOpen size={27} strokeWidth={1} />
<strong>{item.title}</strong>
<span>{socialTx("阅读")}</span>
</div>
) : (
<>
<span className="social-film-orbit" />
<span className="social-film-scene">{item.coverEmoji}</span>
<span className="social-film-play">
{safeSource(config.copy.socialVideoSources?.[item.id]) ? (
<Play size={19} />
) : (
<Film size={19} />
)}
</span>
<small>
{safeSource(config.copy.socialVideoSources?.[item.id])
? "可以看"
: "先看封面"}
</small>
</>
)}
</button>
<div className="social-card-top">
<span className="social-library-meta">{item.detail}</span>
<BookmarkButton id={item.id} collection={`saved-${kind}`} />
</div>
<h2>{item.title}</h2>
<p>{item.summary}</p>
<div className="social-card-actions">
<button
type="button"
className="social-btn"
onClick={() => setSelection(item.id)}
>
{socialTx("打开")}
<ArrowUpRight size={14} />
</button>
<ShareButton
path={`/social/${kind}#${item.id}`}
title={item.title}
/>
</div>
</article>
))}
{!shown.length && (
<EmptyState title={socialTx("没有内容")} description="换关键词,或提交你想做的主题。">
<button
type="button"
className="social-btn"
onClick={() => {
setQuery("");
setSavedOnly(false);
}}
>
{socialTx("重置筛选")}
</button>
<button
type="button"
className="social-btn primary"
onClick={() => setContributeOpen(true)}
>
<PenLine size={14} />
{socialTx("我想投稿")}
</button>
</EmptyState>
)}
</div>
{active && (
<SocialDialog title={active.title} onClose={() => setSelection(null)}>
<div className="social-tags">
<span>{active.detail}</span>
<span>{books ? "共创小册" : source ? "可播放" : "先看封面"}</span>
</div>
{!books && source && <ResourcePlayer key={source} source={source} />}
<p>{active.summary}</p>
{!books && !source && (
<p className="social-status warn">
{socialTx("视频还没放出来,可以先收藏,或去聊天室聊聊。")}
</p>
)}
{books && <p>{socialTx("先看看简介;想读全文可以去书库。")}</p>}
<div className="social-actions">
{books && (
<Link className="social-btn primary" href="/book">
{socialTx("打开书库")} <BookOpen size={15} />
</Link>
)}
<Link
className={`social-btn${books ? "" : " primary"}`}
href={socialPath(
`/chat?intro=${encodeURIComponent(`看到「${active.title}」,想聊聊。`)}`,
)}
>
<MessageCircle size={15} />
{socialTx("去聊天室聊聊")}
</Link>
<BookmarkButton id={active.id} collection={`saved-${kind}`} />
</div>
</SocialDialog>
)}
{contributeOpen && (
<SocialDialog title={socialTx("共创意向")} onClose={() => setContributeOpen(false)}>
<ContributeForm kind={kind} onClose={() => setContributeOpen(false)} />
</SocialDialog>
)}
</SiteShell>
);
}
export function VideosLibrary() {
return <ResourceLibrary kind="videos" />;
}
export function StoriesLibrary() {
return <ResourceLibrary kind="date" />;
}
export function BooksLibrary() {
return <ResourceLibrary kind="ebook" />;
}