Files
role-user/src/components/growth-profile.tsx
T
湛兮 a580422100 feat(growth): 接入游客推荐与权益体验
- 登录:新增游客体验、登录承接和同源及请求体积校验
- 推荐与埋点:接入曝光回执、兴趣偏好和按账号隔离的重试队列
- 聊天体验:展示剩余额度、支持回答反馈及故事记忆管理
- 文档和测试:同步业务边界,覆盖队列幂等与代理登录校验
2026-10-28 21:17:45 +08:00

121 lines
4.3 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 { useEffect, useState } from "react";
import Link from "next/link";
import { digital, type Card } from "@/lib/digital";
import type { GrowthState } from "@/lib/growth";
/** 所有额度和偏好读取服务端真实状态,不展示虚构购买按钮。 */
export function GrowthProfile({
account,
cards,
}: {
account: string;
cards: Card[];
}) {
const [state, setState] = useState<GrowthState | null>(null),
[error, setError] = useState(""),
[notice, setNotice] = useState(""),
[busy, setBusy] = useState(false);
useEffect(() => {
const c = new AbortController();
digital<GrowthState>("growth", "GET", undefined, c.signal)
.then(setState)
.catch((e) => {
if (!c.signal.aborted) setError(e.message);
});
return () => c.abort();
}, [account]);
const tags = [...new Set(cards.flatMap((c) => c.tags))].slice(0, 30);
return (
<section className="dh-growth-profile">
<h2>我的体验与偏好</h2>
{error && <p role="alert">{error}</p>}
{state && (
<>
<p>
{state.entitlement.tier === "guest"
? "游客体验"
: state.entitlement.tier === "member"
? "会员权益"
: "免费权益"}{" "}
· 剩余 {state.entitlement.remaining} / {state.entitlement.limit} 次
· 生成中预占 {state.entitlement.reserved} 次
</p>
<small>
每次聊天或灵感生成消耗一次;空失败会释放,部分回复会保留并计次。
{state.entitlement.tier === "guest"
? "游客次数为本次身份的总额度,登录时承接使用记录。"
: "每日额度按服务器自然日重置。会员权益由后台运营授予。"}
</small>
{state.entitlement.tier === "guest" && (
<p>
<Link href="/login?next=%2Fprofile">登录并保留体验故事</Link>
</p>
)}
<fieldset>
<legend>我感兴趣的题材(最多12项)</legend>
{tags.map((tag) => (
<label key={tag}>
<input
type="checkbox"
checked={state.interests.includes(tag)}
disabled={
busy ||
(!state.interests.includes(tag) &&
state.interests.length >= 12)
}
onChange={(e) =>
setState({
...state,
interests: e.target.checked
? [...state.interests, tag]
: state.interests.filter((t) => t !== tag),
})
}
/>
{tag}
</label>
))}
</fieldset>
<label>
<input
type="checkbox"
checked={state.autoMemory}
disabled={busy}
onChange={(e) =>
setState({ ...state, autoMemory: e.target.checked })
}
/>
允许为我的故事提取记忆
</label>
<p className="dh-form-note">
仅提取你明确说“请记住”的事实,并定期生成历史原文摘录;你可以在故事内查看、更正或移除。关闭后不再使用自动记忆,手动添加的记忆继续生效。
</p>
<button
disabled={busy}
onClick={async () => {
setBusy(true);
setError("");
setNotice("");
try {
await digital("growth/preferences", "PUT", {
interests: state.interests,
autoMemory: state.autoMemory,
});
setNotice("偏好已保存,下次推荐与回复时生效");
} catch (e) {
setError(e instanceof Error ? e.message : "保存失败");
} finally {
setBusy(false);
}
}}
>
{busy ? "保存中…" : "保存推荐与记忆偏好"}
</button>
{notice && <p role="status">{notice}</p>}
</>
)}
</section>
);
}