feat(navigation): 为数字人前台拆分独立页面路由
- 新增对话列表、聊天、角色详情、书架详情及个人资料独立路由 - 导航使用真实链接,补齐历史返回、登录深链与会话草稿恢复 - 搜索筛选由 URL 驱动,修复浏览器返回时丢失筛选的问题 - 补齐路由校验回归、页面加载与异常入口,并同步 README
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"use client";
|
||||
/** 会话列表的水平滑动操作与删除确认;纵向滑动继续交给页面滚动。 */
|
||||
import { useEffect, useId, useRef, useState, type PointerEvent } from "react";
|
||||
import Link from "next/link";
|
||||
import { chatPath } from "@/lib/digital-routes";
|
||||
import { LoaderCircle, Trash2 } from "lucide-react";
|
||||
import type { Card, Summary } from "@/lib/digital";
|
||||
|
||||
@@ -11,7 +13,6 @@ export function ConversationRow({
|
||||
revealed,
|
||||
disabled,
|
||||
onReveal,
|
||||
onOpen,
|
||||
onDelete,
|
||||
}: {
|
||||
summary: Summary;
|
||||
@@ -19,7 +20,6 @@ export function ConversationRow({
|
||||
revealed: boolean;
|
||||
disabled: boolean;
|
||||
onReveal: (id: string | null) => void;
|
||||
onOpen: () => void;
|
||||
onDelete: () => void;
|
||||
}) {
|
||||
const [drag, setDrag] = useState<number | null>(null);
|
||||
@@ -32,8 +32,16 @@ export function ConversationRow({
|
||||
} | null>(null),
|
||||
suppressClick = useRef(false);
|
||||
const distance = 88;
|
||||
function start(e: PointerEvent<HTMLButtonElement>) {
|
||||
if (disabled || e.button !== 0) return;
|
||||
function start(e: PointerEvent<HTMLAnchorElement>) {
|
||||
if (
|
||||
disabled ||
|
||||
e.button !== 0 ||
|
||||
e.metaKey ||
|
||||
e.ctrlKey ||
|
||||
e.shiftKey ||
|
||||
e.altKey
|
||||
)
|
||||
return;
|
||||
suppressClick.current = false;
|
||||
gesture.current = {
|
||||
x: e.clientX,
|
||||
@@ -44,7 +52,7 @@ export function ConversationRow({
|
||||
};
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
}
|
||||
function move(e: PointerEvent<HTMLButtonElement>) {
|
||||
function move(e: PointerEvent<HTMLAnchorElement>) {
|
||||
const g = gesture.current;
|
||||
if (!g) return;
|
||||
const dx = e.clientX - g.x,
|
||||
@@ -57,7 +65,7 @@ export function ConversationRow({
|
||||
g.offset = Math.max(-distance, Math.min(0, g.base + dx));
|
||||
setDrag(g.offset);
|
||||
}
|
||||
function finish(e: PointerEvent<HTMLButtonElement>, cancelled = false) {
|
||||
function finish(e: PointerEvent<HTMLAnchorElement>, cancelled = false) {
|
||||
const g = gesture.current;
|
||||
if (!g) return;
|
||||
gesture.current = null;
|
||||
@@ -85,12 +93,13 @@ export function ConversationRow({
|
||||
<Trash2 size={18} />
|
||||
<span>删除</span>
|
||||
</button>
|
||||
<button
|
||||
<Link
|
||||
href={chatPath(summary.id)}
|
||||
className={`dh-conversation-open ${drag !== null ? "is-dragging" : ""}`}
|
||||
style={{
|
||||
transform: `translateX(${drag ?? (revealed ? -distance : 0)}px)`,
|
||||
}}
|
||||
disabled={disabled}
|
||||
aria-disabled={disabled}
|
||||
onPointerDown={start}
|
||||
onPointerMove={move}
|
||||
onPointerUp={(e) => finish(e)}
|
||||
@@ -102,15 +111,15 @@ export function ConversationRow({
|
||||
}
|
||||
}}
|
||||
onClick={(e) => {
|
||||
if (suppressClick.current && e.detail !== 0) {
|
||||
if (disabled || (suppressClick.current && e.detail !== 0)) {
|
||||
e.preventDefault();
|
||||
suppressClick.current = false;
|
||||
return;
|
||||
}
|
||||
if (revealed) {
|
||||
e.preventDefault();
|
||||
onReveal(null);
|
||||
return;
|
||||
}
|
||||
onOpen();
|
||||
}}
|
||||
>
|
||||
{/* 封面地址沿用服务端目录,不在小型服务器做图片转换。 */}
|
||||
@@ -126,7 +135,7 @@ export function ConversationRow({
|
||||
day: "numeric",
|
||||
})}
|
||||
</small>
|
||||
</button>
|
||||
</Link>
|
||||
<button
|
||||
className="dh-row-delete-desktop"
|
||||
aria-label={`删除对话:${summary.title}`}
|
||||
@@ -177,7 +186,8 @@ export function ConversationDeleteDialog({
|
||||
</span>
|
||||
<h2 id={id}>删除这段对话?</h2>
|
||||
<p id={`${id}-description`}>
|
||||
「{title}」将从你的对话列表中移除,你将无法继续查看。后台仍保留聊天记录。
|
||||
「{title}
|
||||
」将从你的对话列表中移除,你将无法继续查看。后台仍保留聊天记录。
|
||||
</p>
|
||||
{error && (
|
||||
<p className="dh-delete-error" role="alert">
|
||||
|
||||
+202
-150
@@ -2,6 +2,15 @@
|
||||
/* eslint-disable @next/next/no-img-element -- 本地封面直接读取,避免在小型服务器上运行图片转换。 */
|
||||
/** 星港数字人空间:探索、会话、世界书、身份资料和账号内容偏好。 */
|
||||
import Link from "next/link";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import {
|
||||
chatPath,
|
||||
contentPath,
|
||||
digitalTabs,
|
||||
expiredDigitalSessionPath,
|
||||
type DigitalTab,
|
||||
} from "@/lib/digital-routes";
|
||||
import { useDigitalBack } from "./digital-route-history";
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
@@ -51,25 +60,53 @@ import {
|
||||
import { ConversationRow, ConversationDeleteDialog } from "./conversation-row";
|
||||
import { DigitalMessage } from "./digital-message";
|
||||
import "./digital.css";
|
||||
type Tab = "discover" | "chats" | "library" | "me";
|
||||
type Tab = DigitalTab;
|
||||
/** 前台管理个人会话偏好;后台方案仅展示覆盖提示,不开放实验操作。 */
|
||||
export function DigitalHome({
|
||||
initial,
|
||||
initialSummaries,
|
||||
initialTab,
|
||||
initialConversation,
|
||||
initialDetail,
|
||||
}: {
|
||||
initial: Bootstrap;
|
||||
initialSummaries: Summary[];
|
||||
initialTab: Tab;
|
||||
initialConversation?: Conversation;
|
||||
initialDetail?: Card;
|
||||
}) {
|
||||
const [tab, setTab] = useState<Tab>("discover"),
|
||||
[boot, setBoot] = useState<Bootstrap | null>(initial),
|
||||
const router = useRouter();
|
||||
const tab = initialTab;
|
||||
const detail = initialDetail ?? null;
|
||||
const goBack = useDigitalBack(
|
||||
initialConversation ? "/chats" : digitalTabs[tab],
|
||||
);
|
||||
const [boot, setBoot] = useState<Bootstrap | null>(initial),
|
||||
[summaries, setSummaries] = useState<Summary[]>(initialSummaries);
|
||||
const [query, setQuery] = useState(""),
|
||||
[tag, setTag] = useState("全部"),
|
||||
[onlyFavorites, setOnlyFavorites] = useState(false);
|
||||
const [detail, setDetail] = useState<Card | null>(null),
|
||||
[conversation, setConversation] = useState<Conversation | null>(null);
|
||||
const searchParams = useSearchParams();
|
||||
const query = (searchParams.get("q") ?? "").slice(0, 200);
|
||||
const tag = (searchParams.get("tag") ?? "全部").slice(0, 80);
|
||||
const onlyFavorites = searchParams.get("favorites") === "1";
|
||||
function setFilters(next: {
|
||||
query?: string;
|
||||
tag?: string;
|
||||
favorites?: boolean;
|
||||
}) {
|
||||
const params = new URLSearchParams();
|
||||
const values = { query, tag, favorites: onlyFavorites, ...next };
|
||||
if (values.query) params.set("q", values.query.slice(0, 200));
|
||||
if (values.tag !== "全部") params.set("tag", values.tag.slice(0, 80));
|
||||
if (values.favorites) params.set("favorites", "1");
|
||||
const path = `/discover${params.size ? `?${params}` : ""}`;
|
||||
// 原生 replaceState 与 Next 同步查询参数,地址是筛选的唯一来源,返回时不会被旧组件状态覆盖。
|
||||
window.history.replaceState(window.history.state, "", path);
|
||||
}
|
||||
const [conversation, setConversation] = useState<Conversation | null>(
|
||||
initialConversation ?? null,
|
||||
);
|
||||
const [draftReady, setDraftReady] = useState(false);
|
||||
const [draft, setDraft] = useState(""),
|
||||
[busy, setBusy] = useState(false),
|
||||
[busy, setBusy] = useState(initialConversation?.busy ?? false),
|
||||
[opening, setOpening] = useState(false),
|
||||
[error, setError] = useState(""),
|
||||
[notice, setNotice] = useState("");
|
||||
@@ -77,7 +114,9 @@ export function DigitalHome({
|
||||
"loading" | "personal" | "managed" | "unknown"
|
||||
>("loading");
|
||||
const [showSettings, setShowSettings] = useState(false),
|
||||
[settings, setSettings] = useState<Settings | null>(null),
|
||||
[settings, setSettings] = useState<Settings | null>(
|
||||
initialConversation?.settings ?? null,
|
||||
),
|
||||
[profile, setProfile] = useState<Profile | null>(initial.profile);
|
||||
const [profileDraft, setProfileDraft] = useState<Profile | null>(
|
||||
initial.profile,
|
||||
@@ -117,7 +156,7 @@ export function DigitalHome({
|
||||
atBottom = useRef(true),
|
||||
touchY = useRef<number | null>(null),
|
||||
draggingScroll = useRef(false),
|
||||
currentId = useRef<string | null>(null),
|
||||
currentId = useRef<string | null>(initialConversation?.id ?? null),
|
||||
openSequence = useRef(0);
|
||||
const cards = boot?.catalog.filter((c) => c.kind === "character") ?? [],
|
||||
books = boot?.catalog.filter((c) => c.kind === "worldbook") ?? [],
|
||||
@@ -167,6 +206,16 @@ export function DigitalHome({
|
||||
async () => setSummaries(await digital<Summary[]>("conversations")),
|
||||
[],
|
||||
);
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
// 浏览器后退可能恢复 Next 缓存的页面快照,重新核对列表,避免遗漏刚创建或删除的会话。
|
||||
digital<Summary[]>("conversations", "GET", undefined, controller.signal)
|
||||
.then((items) => {
|
||||
if (!controller.signal.aborted) setSummaries(items);
|
||||
})
|
||||
.catch(() => {});
|
||||
return () => controller.abort();
|
||||
}, []);
|
||||
const reloadBootstrap = useCallback(async () => {
|
||||
const data = await digital<Bootstrap>("bootstrap");
|
||||
decisionReceived.current = performance.now();
|
||||
@@ -300,12 +349,22 @@ export function DigitalHome({
|
||||
}, [conversationId]);
|
||||
useEffect(() => {
|
||||
if (!conversationId) return;
|
||||
const frame = requestAnimationFrame(() => {
|
||||
try {
|
||||
setDraft(sessionStorage.getItem(`dh-draft:${conversationId}`) ?? "");
|
||||
} catch {}
|
||||
setDraftReady(true);
|
||||
});
|
||||
return () => cancelAnimationFrame(frame);
|
||||
}, [conversationId]);
|
||||
useEffect(() => {
|
||||
if (!conversationId || !draftReady) return;
|
||||
try {
|
||||
sessionStorage.setItem(`dh-draft:${conversationId}`, draft);
|
||||
} catch {}
|
||||
}, [draft, conversationId]);
|
||||
}, [draft, conversationId, draftReady]);
|
||||
useEffect(() => {
|
||||
if (!detail && !showSettings) return;
|
||||
if (!showSettings) return;
|
||||
const previous = document.activeElement as HTMLElement | null;
|
||||
const dialog = document.querySelector<HTMLElement>(
|
||||
'.dh-overlay [role="dialog"]',
|
||||
@@ -326,7 +385,6 @@ export function DigitalHome({
|
||||
setSelectionSheet(null);
|
||||
return;
|
||||
}
|
||||
setDetail(null);
|
||||
setShowSettings(false);
|
||||
}
|
||||
if (event.key === "Tab") {
|
||||
@@ -348,7 +406,7 @@ export function DigitalHome({
|
||||
document.removeEventListener("keydown", onKey);
|
||||
previous?.focus();
|
||||
};
|
||||
}, [detail, showSettings]);
|
||||
}, [showSettings]);
|
||||
/** 选择弹窗聚焦当前选项并限制滚动;取消、确认及切回桌面后恢复入口焦点。 */
|
||||
useEffect(() => {
|
||||
if (!selectionSheet) return;
|
||||
@@ -407,6 +465,10 @@ export function DigitalHome({
|
||||
};
|
||||
}, [busy, conversationId]);
|
||||
async function openConversation(id: string) {
|
||||
if (id !== conversation?.id) {
|
||||
router.push(chatPath(id));
|
||||
return;
|
||||
}
|
||||
closeIdeas();
|
||||
imeComposing.current = false;
|
||||
setSwipedConversation(null);
|
||||
@@ -433,8 +495,6 @@ export function DigitalHome({
|
||||
} catch {
|
||||
setDraft("");
|
||||
}
|
||||
setTab("chats");
|
||||
setDetail(null);
|
||||
} catch (e) {
|
||||
reportError(e);
|
||||
} finally {
|
||||
@@ -451,8 +511,7 @@ export function DigitalHome({
|
||||
greetingIndex: index,
|
||||
uiDecisionId: boot?.decision.decisionId ?? undefined,
|
||||
});
|
||||
await openConversation(c.id);
|
||||
await refreshSummaries();
|
||||
router.push(chatPath(c.id));
|
||||
} catch (e) {
|
||||
reportError(e);
|
||||
} finally {
|
||||
@@ -545,7 +604,11 @@ export function DigitalHome({
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (response.status === 401) {
|
||||
window.location.assign("/login");
|
||||
window.location.assign(
|
||||
expiredDigitalSessionPath(
|
||||
window.location.pathname + window.location.search,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
accepted = response.ok;
|
||||
@@ -668,9 +731,7 @@ export function DigitalHome({
|
||||
"POST",
|
||||
{ messageId: message.id },
|
||||
);
|
||||
await openConversation(result.id);
|
||||
await refreshSummaries();
|
||||
setNotice("已从这里开启新分支,原会话仍然保留");
|
||||
router.push(chatPath(result.id));
|
||||
} catch (e) {
|
||||
reportError(e);
|
||||
}
|
||||
@@ -705,9 +766,9 @@ export function DigitalHome({
|
||||
setSwipedConversation(null);
|
||||
if (currentId.current === id) {
|
||||
closeIdeas();
|
||||
setConversation(null);
|
||||
currentId.current = null;
|
||||
setDraft("");
|
||||
router.replace("/chats");
|
||||
}
|
||||
try {
|
||||
sessionStorage.removeItem(`dh-draft:${id}`);
|
||||
@@ -885,18 +946,9 @@ export function DigitalHome({
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
function switchTab(value: Tab) {
|
||||
closeIdeas();
|
||||
setSwipedConversation(null);
|
||||
if (conversation && busy) {
|
||||
setNotice("请先停止当前回复,再离开会话");
|
||||
return;
|
||||
}
|
||||
setConversation(null);
|
||||
currentId.current = null;
|
||||
if (value === "me" && tab !== "me" && profile) setProfileDraft(profile);
|
||||
setTab(value);
|
||||
setError("");
|
||||
router.push(digitalTabs[value]);
|
||||
}
|
||||
|
||||
const tags = [
|
||||
"全部",
|
||||
...new Set(cards.flatMap((c) => c.tags).slice(0, 30)),
|
||||
@@ -1005,12 +1057,14 @@ export function DigitalHome({
|
||||
))}
|
||||
</fieldset>
|
||||
);
|
||||
useEffect(
|
||||
() => () => {
|
||||
useEffect(() => {
|
||||
// 缓存页面重新显示时重新绑定会话,离开后的旧流请求不能更新下一页。
|
||||
currentId.current = conversationId ?? null;
|
||||
return () => {
|
||||
ideasRequest.current?.abort();
|
||||
},
|
||||
[],
|
||||
);
|
||||
currentId.current = null;
|
||||
};
|
||||
}, [conversationId]);
|
||||
// UI按消费者返回值决定,组名仅用于归因;C到Z同样可以返回新版UI。
|
||||
// 缺少value时兼容尚未升级的服务响应。
|
||||
const modern =
|
||||
@@ -1036,17 +1090,18 @@ export function DigitalHome({
|
||||
{ id: "me", label: "我的身份", icon: UserRound },
|
||||
] as const
|
||||
).map((item) => (
|
||||
<button
|
||||
<Link
|
||||
key={item.id}
|
||||
href={digitalTabs[item.id]}
|
||||
className={tab === item.id ? "is-active" : ""}
|
||||
onClick={() => switchTab(item.id)}
|
||||
aria-current={tab === item.id ? "page" : undefined}
|
||||
>
|
||||
<item.icon size={19} />
|
||||
<span>{item.label}</span>
|
||||
{item.id === "chats" && summaries.length > 0 && (
|
||||
<small>{summaries.length}</small>
|
||||
)}
|
||||
</button>
|
||||
</Link>
|
||||
))}
|
||||
</nav>
|
||||
<div className="dh-sidebar-bottom">
|
||||
@@ -1086,12 +1141,8 @@ export function DigitalHome({
|
||||
<header className="dh-chat-header">
|
||||
<button
|
||||
className="dh-icon"
|
||||
aria-label="返回对话列表"
|
||||
disabled={busy}
|
||||
onClick={() => {
|
||||
setConversation(null);
|
||||
currentId.current = null;
|
||||
}}
|
||||
aria-label="返回上一页"
|
||||
onClick={goBack}
|
||||
>
|
||||
<ArrowLeft size={21} />
|
||||
</button>
|
||||
@@ -1400,6 +1451,7 @@ export function DigitalHome({
|
||||
<textarea
|
||||
ref={textarea}
|
||||
aria-label="消息"
|
||||
disabled={!draftReady}
|
||||
placeholder={`对 ${currentCard?.name ?? "角色"} 说点什么…`}
|
||||
value={draft}
|
||||
maxLength={4000}
|
||||
@@ -1449,7 +1501,7 @@ export function DigitalHome({
|
||||
</small>
|
||||
</div>
|
||||
</section>
|
||||
) : (
|
||||
) : detail ? null : (
|
||||
<div className="dh-page">
|
||||
<header className="dh-page-header">
|
||||
<div>
|
||||
@@ -1490,7 +1542,7 @@ export function DigitalHome({
|
||||
cards.find(
|
||||
(c) => !profile?.favorites.includes(c.id),
|
||||
) ?? cards[0];
|
||||
if (card) setDetail(card);
|
||||
if (card) router.push(contentPath(card));
|
||||
}}
|
||||
>
|
||||
<WandSparkles size={16} />
|
||||
@@ -1511,12 +1563,12 @@ export function DigitalHome({
|
||||
aria-label="搜索角色"
|
||||
placeholder="搜索名字、性格或故事…"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
onChange={(e) => setFilters({ query: e.target.value })}
|
||||
/>
|
||||
{query && (
|
||||
<button
|
||||
aria-label="清空搜索"
|
||||
onClick={() => setQuery("")}
|
||||
onClick={() => setFilters({ query: "" })}
|
||||
>
|
||||
<X size={15} />
|
||||
</button>
|
||||
@@ -1524,7 +1576,7 @@ export function DigitalHome({
|
||||
</label>
|
||||
<button
|
||||
className={`dh-favorite-filter ${onlyFavorites ? "is-active" : ""}`}
|
||||
onClick={() => setOnlyFavorites(!onlyFavorites)}
|
||||
onClick={() => setFilters({ favorites: !onlyFavorites })}
|
||||
>
|
||||
<Heart
|
||||
size={17}
|
||||
@@ -1538,7 +1590,7 @@ export function DigitalHome({
|
||||
<button
|
||||
key={t}
|
||||
className={tag === t ? "is-active" : ""}
|
||||
onClick={() => setTag(t)}
|
||||
onClick={() => setFilters({ tag: t })}
|
||||
>
|
||||
{t}
|
||||
</button>
|
||||
@@ -1551,10 +1603,7 @@ export function DigitalHome({
|
||||
<div className="dh-card-grid">
|
||||
{filtered.map((c) => (
|
||||
<article className="dh-card" key={c.id}>
|
||||
<button
|
||||
className="dh-card-open"
|
||||
onClick={() => setDetail(c)}
|
||||
>
|
||||
<Link className="dh-card-open" href={contentPath(c)}>
|
||||
<img
|
||||
src={c.cover || "/icon.svg"}
|
||||
alt={c.name}
|
||||
@@ -1573,7 +1622,7 @@ export function DigitalHome({
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
</Link>
|
||||
<button
|
||||
className={`dh-card-favorite ${profile?.favorites.includes(c.id) ? "is-active" : ""}`}
|
||||
aria-label={`收藏${c.name}`}
|
||||
@@ -1598,9 +1647,11 @@ export function DigitalHome({
|
||||
<p>换一个关键词,或者看看其他故事。</p>
|
||||
<button
|
||||
onClick={() => {
|
||||
setTag("全部");
|
||||
setQuery("");
|
||||
setOnlyFavorites(false);
|
||||
setFilters({
|
||||
query: "",
|
||||
tag: "全部",
|
||||
favorites: false,
|
||||
});
|
||||
}}
|
||||
>
|
||||
看看全部角色
|
||||
@@ -1630,7 +1681,6 @@ export function DigitalHome({
|
||||
revealed={swipedConversation === s.id}
|
||||
disabled={opening || deleting}
|
||||
onReveal={setSwipedConversation}
|
||||
onOpen={() => openConversation(s.id)}
|
||||
onDelete={() => requestDelete(s)}
|
||||
/>
|
||||
))}
|
||||
@@ -1661,17 +1711,17 @@ export function DigitalHome({
|
||||
</div>
|
||||
<div className="dh-library-grid">
|
||||
{books.map((b) => (
|
||||
<button
|
||||
<Link
|
||||
key={b.id}
|
||||
className="dh-library-card"
|
||||
onClick={() => setDetail(b)}
|
||||
href={contentPath(b)}
|
||||
>
|
||||
<BookOpen size={24} />
|
||||
<small>{b.entriesCount} 条设定</small>
|
||||
<h3>{b.name}</h3>
|
||||
<p>{b.description}</p>
|
||||
<span>查看详情 →</span>
|
||||
</button>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
<div className="dh-section-heading">
|
||||
@@ -1680,17 +1730,17 @@ export function DigitalHome({
|
||||
</div>
|
||||
<div className="dh-library-grid">
|
||||
{presets.map((b) => (
|
||||
<button
|
||||
<Link
|
||||
key={b.id}
|
||||
className="dh-library-card"
|
||||
onClick={() => setDetail(b)}
|
||||
href={contentPath(b)}
|
||||
>
|
||||
<WandSparkles size={24} />
|
||||
<small>对话风格</small>
|
||||
<h3>{b.name}</h3>
|
||||
<p>{b.description}</p>
|
||||
<span>查看详情 →</span>
|
||||
</button>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
@@ -1741,7 +1791,9 @@ export function DigitalHome({
|
||||
<label className="dh-content-preference">
|
||||
<span>
|
||||
<strong>NSFW 成人内容</strong>
|
||||
<small>登录账号默认开启,可随时关闭;保存后用于后续回复。</small>
|
||||
<small>
|
||||
登录账号默认开启,可随时关闭;保存后用于后续回复。
|
||||
</small>
|
||||
</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
@@ -1784,90 +1836,90 @@ export function DigitalHome({
|
||||
</footer>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
{detail && (
|
||||
<div
|
||||
className="dh-overlay"
|
||||
onClick={(e) => {
|
||||
if (e.target === e.currentTarget) setDetail(null);
|
||||
}}
|
||||
>
|
||||
<section
|
||||
className="dh-detail"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label={detail.name}
|
||||
>
|
||||
<button
|
||||
className="dh-close"
|
||||
aria-label="关闭详情"
|
||||
onClick={() => setDetail(null)}
|
||||
>
|
||||
<X />
|
||||
</button>
|
||||
{detail.kind === "character" && (
|
||||
<div className="dh-detail-cover">
|
||||
<img src={detail.cover || "/icon.svg"} alt={detail.name} />
|
||||
</div>
|
||||
)}
|
||||
<div className="dh-detail-body">
|
||||
<span className="dh-eyebrow">
|
||||
{detail && (
|
||||
<div className="dh-detail-page">
|
||||
<header className="dh-detail-nav">
|
||||
<button className="dh-detail-back" onClick={goBack}>
|
||||
<ArrowLeft size={18} />
|
||||
返回上一页
|
||||
</button>
|
||||
<span>
|
||||
{detail.kind === "character"
|
||||
? `${detail.age} 岁 · ${detail.gender === "male" ? "男性" : "女性"}角色`
|
||||
? "角色详情"
|
||||
: detail.kind === "worldbook"
|
||||
? "WORLD BOOK"
|
||||
: "STORY PRESET"}
|
||||
? "世界书"
|
||||
: "对话风格"}
|
||||
</span>
|
||||
<h2>{detail.name}</h2>
|
||||
<div className="dh-card-tags">
|
||||
{detail.tags.map((t) => (
|
||||
<span key={t}>{t}</span>
|
||||
))}
|
||||
</div>
|
||||
<p className="dh-detail-description">{detail.description}</p>
|
||||
{detail.greeting && (
|
||||
<blockquote>
|
||||
{detail.greeting
|
||||
.replaceAll("{{user}}", profile?.userName ?? "旅人")
|
||||
.replaceAll("{{char}}", detail.name)}
|
||||
</blockquote>
|
||||
</header>
|
||||
<section
|
||||
className="dh-detail"
|
||||
role="region"
|
||||
aria-label={detail.name}
|
||||
>
|
||||
{detail.kind === "character" && (
|
||||
<div className="dh-detail-cover">
|
||||
<img src={detail.cover || "/icon.svg"} alt={detail.name} />
|
||||
</div>
|
||||
)}
|
||||
{detail.kind === "character" ? (
|
||||
<>
|
||||
<button
|
||||
className="dh-primary"
|
||||
disabled={opening}
|
||||
onClick={() => start(detail)}
|
||||
>
|
||||
{opening ? (
|
||||
<LoaderCircle className="dh-spin" size={17} />
|
||||
) : (
|
||||
<MessageCircle size={17} />
|
||||
)}
|
||||
开启这段故事
|
||||
</button>
|
||||
{detail.alternateGreetings.map((g, i) => (
|
||||
<button
|
||||
className="dh-alt-greeting"
|
||||
key={i}
|
||||
disabled={opening}
|
||||
onClick={() => start(detail, i + 1)}
|
||||
>
|
||||
<small>另一种相遇 {i + 1}</small>
|
||||
<p>{g.slice(0, 110)}</p>
|
||||
</button>
|
||||
<div className="dh-detail-body">
|
||||
<span className="dh-eyebrow">
|
||||
{detail.kind === "character"
|
||||
? `${detail.age} 岁 · ${detail.gender === "male" ? "男性" : "女性"}角色`
|
||||
: detail.kind === "worldbook"
|
||||
? "WORLD BOOK"
|
||||
: "STORY PRESET"}
|
||||
</span>
|
||||
<h1>{detail.name}</h1>
|
||||
<div className="dh-card-tags">
|
||||
{detail.tags.map((t) => (
|
||||
<span key={t}>{t}</span>
|
||||
))}
|
||||
</>
|
||||
) : (
|
||||
<p className="dh-form-note">
|
||||
进入任意对话,点击右上角「对话设定」即可使用。
|
||||
</p>
|
||||
)}
|
||||
<small className="dh-source">{detail.source}</small>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<p className="dh-detail-description">{detail.description}</p>
|
||||
{detail.greeting && (
|
||||
<blockquote>
|
||||
{detail.greeting
|
||||
.replaceAll("{{user}}", profile?.userName ?? "旅人")
|
||||
.replaceAll("{{char}}", detail.name)}
|
||||
</blockquote>
|
||||
)}
|
||||
{detail.kind === "character" ? (
|
||||
<>
|
||||
<button
|
||||
className="dh-primary"
|
||||
disabled={opening}
|
||||
onClick={() => start(detail)}
|
||||
>
|
||||
{opening ? (
|
||||
<LoaderCircle className="dh-spin" size={17} />
|
||||
) : (
|
||||
<MessageCircle size={17} />
|
||||
)}
|
||||
开启这段故事
|
||||
</button>
|
||||
{detail.alternateGreetings.map((g, i) => (
|
||||
<button
|
||||
className="dh-alt-greeting"
|
||||
key={i}
|
||||
disabled={opening}
|
||||
onClick={() => start(detail, i + 1)}
|
||||
>
|
||||
<small>另一种相遇 {i + 1}</small>
|
||||
<p>{g.slice(0, 110)}</p>
|
||||
</button>
|
||||
))}
|
||||
</>
|
||||
) : (
|
||||
<p className="dh-form-note">
|
||||
进入任意对话,点击右上角「对话设定」即可使用。
|
||||
</p>
|
||||
)}
|
||||
<small className="dh-source">{detail.source}</small>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
{showSettings && settings && (
|
||||
<div className={`dh-overlay ${selectionSheet ? "dh-sheet-open" : ""}`}>
|
||||
<form
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/** 独立数字人页面的数据入口:并行加载私有首屏,深链读取会话并统一处理失效资源。 */
|
||||
import { notFound } from "next/navigation";
|
||||
import { BackendError, requireBackendData } from "@/lib/backend";
|
||||
import { validDigitalId, type DigitalTab } from "@/lib/digital-routes";
|
||||
import type { Bootstrap, Conversation, Summary } from "@/lib/digital";
|
||||
import { DigitalHome } from "./digital-home";
|
||||
|
||||
/** 每个地址生成独立页面;刷新、分享或浏览器后退均能从服务端还原同一资源。 */
|
||||
export async function DigitalPage({
|
||||
tab,
|
||||
path,
|
||||
conversationId,
|
||||
contentId,
|
||||
}: {
|
||||
tab: DigitalTab;
|
||||
path: string;
|
||||
conversationId?: string;
|
||||
contentId?: string;
|
||||
}) {
|
||||
if (
|
||||
(conversationId && !validDigitalId(conversationId)) ||
|
||||
(contentId && !validDigitalId(contentId))
|
||||
)
|
||||
notFound();
|
||||
const [initial, initialSummaries, initialConversation] = await Promise.all([
|
||||
requireBackendData<Bootstrap>("/digital/bootstrap", path),
|
||||
requireBackendData<Summary[]>("/digital/conversations", path),
|
||||
conversationId
|
||||
? requireBackendData<Conversation>(
|
||||
`/digital/conversations/${conversationId}`,
|
||||
path,
|
||||
)
|
||||
: undefined,
|
||||
]).catch((error) => {
|
||||
if (error instanceof BackendError && error.status === 404) notFound();
|
||||
throw error;
|
||||
});
|
||||
const detail = contentId
|
||||
? initial.catalog.find(
|
||||
(card) =>
|
||||
card.id === contentId &&
|
||||
(tab === "discover"
|
||||
? card.kind === "character"
|
||||
: card.kind !== "character"),
|
||||
)
|
||||
: undefined;
|
||||
if (contentId && !detail) notFound();
|
||||
return (
|
||||
<DigitalHome
|
||||
key={path}
|
||||
initial={initial}
|
||||
initialSummaries={initialSummaries}
|
||||
initialTab={tab}
|
||||
initialConversation={initialConversation}
|
||||
initialDetail={detail}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
/** 记录当前浏览器条目的站内来源;返回优先沿真实历史,直接打开深链时回父页面。 */
|
||||
import type { Route } from "next";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { usePathname, useRouter, useSearchParams } from "next/navigation";
|
||||
import { safeDigitalReturnTo } from "@/lib/digital-routes";
|
||||
|
||||
/** 共享布局持续存在,让不同路由的进入顺序成为浏览器真实历史。 */
|
||||
export function DigitalRouteHistory() {
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
const previous = useRef<string | null>(null);
|
||||
useEffect(() => {
|
||||
const path =
|
||||
window.location.pathname + window.location.search + window.location.hash;
|
||||
const state = window.history.state ?? {};
|
||||
// 后退恢复的是已有条目,不能把它的来源覆盖成刚离开的页面。
|
||||
if (state.stellarNavigation?.path !== path) {
|
||||
const existing = state.stellarNavigation;
|
||||
// 筛选只替换当前 URL,不凭空增加一次返回步骤。
|
||||
const samePage =
|
||||
typeof existing?.path === "string" &&
|
||||
new URL(existing.path, window.location.origin).pathname === pathname;
|
||||
window.history.replaceState(
|
||||
{
|
||||
...state,
|
||||
stellarNavigation: {
|
||||
path,
|
||||
previous: samePage ? existing.previous : previous.current,
|
||||
},
|
||||
},
|
||||
"",
|
||||
path,
|
||||
);
|
||||
}
|
||||
previous.current = path;
|
||||
}, [pathname, searchParams]);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 页面内返回按钮与浏览器后退一致;外部打开或新标签页不返回站外。 */
|
||||
export function useDigitalBack(fallback: Route) {
|
||||
const router = useRouter();
|
||||
return () => {
|
||||
const previous = window.history.state?.stellarNavigation?.previous;
|
||||
if (
|
||||
typeof previous === "string" &&
|
||||
safeDigitalReturnTo(previous) === previous
|
||||
)
|
||||
router.back();
|
||||
else router.replace(fallback);
|
||||
};
|
||||
}
|
||||
@@ -121,7 +121,7 @@
|
||||
flex-direction: column;
|
||||
gap: 9px;
|
||||
}
|
||||
.dh-sidebar nav button {
|
||||
.dh-sidebar nav a {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14px;
|
||||
@@ -130,15 +130,15 @@
|
||||
color: var(--dh-muted);
|
||||
text-align: left;
|
||||
}
|
||||
.dh-sidebar nav button small {
|
||||
.dh-sidebar nav a small {
|
||||
margin-left: auto;
|
||||
font-size: 11px;
|
||||
}
|
||||
.dh-sidebar nav button.is-active {
|
||||
.dh-sidebar nav a.is-active {
|
||||
color: var(--dh-accent);
|
||||
background: color-mix(in srgb, var(--dh-accent) 9%, transparent);
|
||||
}
|
||||
.dh-sidebar nav button:hover {
|
||||
.dh-sidebar nav a:hover {
|
||||
background: var(--dh-panel);
|
||||
}
|
||||
.dh-sidebar-bottom {
|
||||
@@ -1963,7 +1963,7 @@
|
||||
gap: 2px;
|
||||
height: 100%;
|
||||
}
|
||||
.dh-sidebar nav button {
|
||||
.dh-sidebar nav a {
|
||||
padding: 7px 3px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -1973,7 +1973,7 @@
|
||||
justify-content: center;
|
||||
position: relative;
|
||||
}
|
||||
.dh-sidebar nav button small {
|
||||
.dh-sidebar nav a small {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 18px;
|
||||
@@ -1982,10 +1982,10 @@
|
||||
padding: 0 5px;
|
||||
border-radius: 9px;
|
||||
}
|
||||
.dh-sidebar nav button.is-active {
|
||||
.dh-sidebar nav a.is-active {
|
||||
background: none;
|
||||
}
|
||||
.dh-sidebar nav button.is-active svg {
|
||||
.dh-sidebar nav a.is-active svg {
|
||||
filter: drop-shadow(
|
||||
0 0 8px color-mix(in srgb, var(--dh-accent) 45%, transparent)
|
||||
);
|
||||
@@ -2422,3 +2422,67 @@ html.dh-chat-active body {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* 页面导航使用真实链接,视觉与触摸命中范围保持一致。 */
|
||||
.dh-sidebar nav a {
|
||||
cursor: pointer;
|
||||
}
|
||||
.dh-conversation-open[aria-disabled="true"] {
|
||||
opacity: 0.42;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.dh-detail-page {
|
||||
padding: 40px 32px;
|
||||
min-height: 100dvh;
|
||||
}
|
||||
.dh-detail-page .dh-detail {
|
||||
width: min(960px, 100%);
|
||||
max-height: none;
|
||||
overflow: visible;
|
||||
margin: 0 auto;
|
||||
}
|
||||
.dh-detail-page .dh-detail-body h1 {
|
||||
font-size: clamp(26px, 4vw, 38px);
|
||||
line-height: 1.3;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.dh-route-message {
|
||||
display: flex;
|
||||
min-height: 100dvh;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
padding: 32px;
|
||||
text-align: center;
|
||||
}
|
||||
.dh-route-message a,
|
||||
.dh-route-message button {
|
||||
padding: 12px 24px;
|
||||
border: 1px solid var(--dh-line);
|
||||
border-radius: 12px;
|
||||
}
|
||||
@media (max-width: 600px) {
|
||||
.dh-detail-page {
|
||||
padding: 16px 14px 100px;
|
||||
}
|
||||
.dh-detail-page .dh-detail {
|
||||
max-height: none;
|
||||
}
|
||||
}
|
||||
.dh-detail-nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
max-width: 960px;
|
||||
margin: 0 auto 20px;
|
||||
color: var(--dh-muted);
|
||||
}
|
||||
.dh-app .dh-detail-back {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-height: 44px;
|
||||
color: var(--dh-accent);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
/** 独立星港用户账号登录,成功后进入数字人空间。 */
|
||||
import { useRouter } from "next/navigation";
|
||||
import { safeDigitalReturnTo } from "@/lib/digital-routes";
|
||||
import { Eye, EyeOff, LockKeyhole, Smartphone } from "lucide-react";
|
||||
import { FormEvent, useState } from "react";
|
||||
|
||||
/** 独立用户的登录表单,不保存明文密码。 */
|
||||
export function LoginForm() {
|
||||
export function LoginForm({ returnTo = "/discover" }: { returnTo?: string }) {
|
||||
const router = useRouter();
|
||||
const [error, setError] = useState("");
|
||||
const [pending, setPending] = useState(false);
|
||||
@@ -31,7 +32,7 @@ export function LoginForm() {
|
||||
const payload = await response.json().catch(() => null);
|
||||
throw new Error(payload?.message || "登录失败,请检查账号和密码");
|
||||
}
|
||||
router.replace("/discover");
|
||||
router.replace(safeDigitalReturnTo(returnTo));
|
||||
router.refresh();
|
||||
} catch (error) {
|
||||
setError(
|
||||
|
||||
Reference in New Issue
Block a user