feat(chat): 支持滑动删除灵感回复与H5回车发送
- 会话列表:水平滑动展开删除、桌面快捷入口、模态确认及失败重试 - 灵感回复:按上下文生成建议,仅加入草稿,保留原输入并取消过期请求 - 输入交互:移除回车发送的屏宽限制,保护中文输入法,支持Shift换行与手机换行按钮
This commit is contained in:
+268
-39
@@ -48,6 +48,7 @@ import {
|
||||
type Summary,
|
||||
type Bootstrap,
|
||||
} from "@/lib/digital";
|
||||
import { ConversationRow, ConversationDeleteDialog } from "./conversation-row";
|
||||
import "./digital.css";
|
||||
type Tab = "discover" | "chats" | "library" | "me";
|
||||
/** 接收文本与视觉打字分离;减少动画设置下直接呈现完整文本。 */
|
||||
@@ -139,6 +140,19 @@ export function DigitalHome({
|
||||
bookIds: string[];
|
||||
}>({ bookIds: [] });
|
||||
const selectionTrigger = useRef<HTMLElement | null>(null);
|
||||
const [swipedConversation, setSwipedConversation] = useState<string | null>(
|
||||
null,
|
||||
);
|
||||
const [deleteTarget, setDeleteTarget] = useState<Summary | null>(null),
|
||||
[deleting, setDeleting] = useState(false),
|
||||
[deleteError, setDeleteError] = useState("");
|
||||
const [ideasOpen, setIdeasOpen] = useState(false),
|
||||
[ideasLoading, setIdeasLoading] = useState(false),
|
||||
[ideas, setIdeas] = useState<string[]>([]),
|
||||
[ideasError, setIdeasError] = useState("");
|
||||
const ideasRequest = useRef<AbortController | null>(null),
|
||||
imeComposing = useRef(false),
|
||||
deleteLock = useRef(false);
|
||||
const favoritePending = useRef(false);
|
||||
const [activeMessage, setActiveMessage] = useState<string | null>(null),
|
||||
[showJump, setShowJump] = useState(false),
|
||||
@@ -362,6 +376,9 @@ export function DigitalHome({
|
||||
};
|
||||
}, [busy, conversationId]);
|
||||
async function openConversation(id: string) {
|
||||
closeIdeas();
|
||||
imeComposing.current = false;
|
||||
setSwipedConversation(null);
|
||||
if (streamingId.current && streamingId.current !== id) {
|
||||
setNotice("请先停止当前回复,再切换会话");
|
||||
return;
|
||||
@@ -436,6 +453,7 @@ export function DigitalHome({
|
||||
(mode === "send" && !draft.trim())
|
||||
)
|
||||
return;
|
||||
closeIdeas();
|
||||
const id = conversation.id,
|
||||
content = draft.trim(),
|
||||
requestId = crypto.randomUUID();
|
||||
@@ -445,7 +463,10 @@ export function DigitalHome({
|
||||
setNotice("");
|
||||
atBottom.current = true;
|
||||
streamingId.current = id;
|
||||
if (mode === "send") setDraft("");
|
||||
if (mode === "send") {
|
||||
setDraft("");
|
||||
if (textarea.current) textarea.current.style.height = "auto";
|
||||
}
|
||||
const local: Message[] = [
|
||||
...(mode === "send"
|
||||
? [
|
||||
@@ -570,11 +591,12 @@ export function DigitalHome({
|
||||
if (
|
||||
event.key === "Enter" &&
|
||||
!event.shiftKey &&
|
||||
!imeComposing.current &&
|
||||
!event.nativeEvent.isComposing &&
|
||||
window.matchMedia("(min-width: 768px)").matches
|
||||
event.nativeEvent.keyCode !== 229
|
||||
) {
|
||||
event.preventDefault();
|
||||
void send();
|
||||
if (!event.repeat) void send();
|
||||
}
|
||||
}
|
||||
async function pin(message: Message) {
|
||||
@@ -615,21 +637,133 @@ export function DigitalHome({
|
||||
reportError(e);
|
||||
}
|
||||
}
|
||||
async function removeConversation() {
|
||||
if (
|
||||
!conversation ||
|
||||
busy ||
|
||||
!window.confirm("删除此会话和全部消息?此操作无法撤销。")
|
||||
)
|
||||
function requestDelete(target: Summary) {
|
||||
if (target.id === conversation?.id && busy) {
|
||||
setNotice("请先停止当前回复,再删除对话");
|
||||
return;
|
||||
try {
|
||||
await digital(`conversations/${conversation.id}`, "DELETE");
|
||||
setConversation(null);
|
||||
currentId.current = null;
|
||||
await refreshSummaries();
|
||||
} catch (e) {
|
||||
reportError(e);
|
||||
}
|
||||
setDeleteError("");
|
||||
setDeleteTarget(target);
|
||||
setShowMenu(false);
|
||||
}
|
||||
function removeConversation() {
|
||||
if (conversation)
|
||||
requestDelete({
|
||||
id: conversation.id,
|
||||
title: conversation.title,
|
||||
characterId: conversation.characterId,
|
||||
updatedAt: "",
|
||||
});
|
||||
}
|
||||
async function confirmDelete() {
|
||||
if (!deleteTarget || deleteLock.current) return;
|
||||
const id = deleteTarget.id;
|
||||
deleteLock.current = true;
|
||||
setDeleting(true);
|
||||
setDeleteError("");
|
||||
try {
|
||||
await digital(`conversations/${id}`, "DELETE");
|
||||
setSummaries((current) => current.filter((s) => s.id !== id));
|
||||
setSwipedConversation(null);
|
||||
if (currentId.current === id) {
|
||||
closeIdeas();
|
||||
setConversation(null);
|
||||
currentId.current = null;
|
||||
setDraft("");
|
||||
}
|
||||
try {
|
||||
sessionStorage.removeItem(`dh-draft:${id}`);
|
||||
} catch {}
|
||||
setDeleteTarget(null);
|
||||
setNotice("对话已删除");
|
||||
} catch (e) {
|
||||
setDeleteError(e instanceof Error ? e.message : "删除失败,请重试");
|
||||
} finally {
|
||||
deleteLock.current = false;
|
||||
setDeleting(false);
|
||||
}
|
||||
}
|
||||
/** 灵感建议只填入草稿,旧请求和已离开的会话不能覆盖当前输入。 */
|
||||
function closeIdeas() {
|
||||
ideasRequest.current?.abort();
|
||||
ideasRequest.current = null;
|
||||
setIdeasOpen(false);
|
||||
setIdeasLoading(false);
|
||||
setIdeasError("");
|
||||
setIdeas([]);
|
||||
}
|
||||
async function generateIdeas() {
|
||||
if (!conversation || busy || streamingId.current || ideasRequest.current)
|
||||
return;
|
||||
const id = conversation.id,
|
||||
anchor = conversation.messages.at(-1)?.id ?? null,
|
||||
controller = new AbortController();
|
||||
ideasRequest.current = controller;
|
||||
setIdeasOpen(true);
|
||||
setIdeasLoading(true);
|
||||
setIdeasError("");
|
||||
setIdeas([]);
|
||||
try {
|
||||
const result = await digital<{
|
||||
suggestions: string[];
|
||||
anchorId: string | null;
|
||||
}>(
|
||||
`conversations/${id}/suggestions`,
|
||||
"POST",
|
||||
{ draft: draft.slice(0, 1000) },
|
||||
controller.signal,
|
||||
);
|
||||
if (ideasRequest.current !== controller || currentId.current !== id)
|
||||
return;
|
||||
if (result.anchorId !== anchor) {
|
||||
setIdeasError("对话已更新,请重新打开会话后再试");
|
||||
return;
|
||||
}
|
||||
setIdeas(result.suggestions);
|
||||
} catch (e) {
|
||||
if (!controller.signal.aborted && currentId.current === id)
|
||||
setIdeasError(e instanceof Error ? e.message : "灵感生成失败,请重试");
|
||||
} finally {
|
||||
if (ideasRequest.current === controller) {
|
||||
ideasRequest.current = null;
|
||||
setIdeasLoading(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
function chooseIdea(value: string) {
|
||||
const next = draft
|
||||
? `${draft}${draft.endsWith("\n") ? "" : "\n"}${value}`
|
||||
: value;
|
||||
if (next.length > 4000) {
|
||||
setIdeasError("草稿已接近字数上限,请先缩短内容");
|
||||
return;
|
||||
}
|
||||
setDraft(next);
|
||||
closeIdeas();
|
||||
setNotice("已加入草稿,可修改后发送");
|
||||
requestAnimationFrame(() => {
|
||||
const input = textarea.current;
|
||||
if (input) {
|
||||
input.focus();
|
||||
input.style.height = "auto";
|
||||
input.style.height = `${Math.min(input.scrollHeight, 160)}px`;
|
||||
input.setSelectionRange(next.length, next.length);
|
||||
}
|
||||
});
|
||||
}
|
||||
function insertLineBreak() {
|
||||
const input = textarea.current;
|
||||
if (!input || draft.length >= 4000) return;
|
||||
const start = input.selectionStart,
|
||||
end = input.selectionEnd,
|
||||
next = draft.slice(0, start) + "\n" + draft.slice(end);
|
||||
setDraft(next);
|
||||
requestAnimationFrame(() => {
|
||||
input.focus();
|
||||
input.setSelectionRange(start + 1, start + 1);
|
||||
input.style.height = "auto";
|
||||
input.style.height = `${Math.min(input.scrollHeight, 160)}px`;
|
||||
});
|
||||
}
|
||||
/** 抽屉仅编辑临时值;确认后回填设定,取消不改动已有选择。 */
|
||||
function openSelection(kind: "style" | "books") {
|
||||
@@ -705,6 +839,8 @@ export function DigitalHome({
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
function switchTab(value: Tab) {
|
||||
closeIdeas();
|
||||
setSwipedConversation(null);
|
||||
if (conversation && busy) {
|
||||
setNotice("请先停止当前回复,再离开会话");
|
||||
return;
|
||||
@@ -818,6 +954,12 @@ export function DigitalHome({
|
||||
))}
|
||||
</fieldset>
|
||||
);
|
||||
useEffect(
|
||||
() => () => {
|
||||
ideasRequest.current?.abort();
|
||||
},
|
||||
[],
|
||||
);
|
||||
const modern = boot?.decision.variant === "B";
|
||||
return (
|
||||
<div
|
||||
@@ -1083,6 +1225,83 @@ export function DigitalHome({
|
||||
</button>
|
||||
)}
|
||||
<div className="dh-composer-wrap">
|
||||
{ideasOpen && (
|
||||
<section
|
||||
className="dh-reply-ideas"
|
||||
aria-label="灵感回复"
|
||||
aria-busy={ideasLoading}
|
||||
>
|
||||
<header>
|
||||
<strong>
|
||||
<Sparkles size={14} />
|
||||
灵感回复
|
||||
</strong>
|
||||
<button
|
||||
type="button"
|
||||
className="dh-icon"
|
||||
aria-label="关闭灵感回复"
|
||||
onClick={closeIdeas}
|
||||
>
|
||||
<X size={16} />
|
||||
</button>
|
||||
</header>
|
||||
<p className="dh-ideas-note">
|
||||
{draft
|
||||
? "选择后追加到现有草稿,不会自动发送。"
|
||||
: "根据当前对话生成,选择后可编辑再发送。"}
|
||||
</p>
|
||||
{ideasLoading ? (
|
||||
<p className="dh-ideas-loading" role="status">
|
||||
<LoaderCircle size={16} className="dh-spin" />
|
||||
正在寻找合适的接话方式…
|
||||
</p>
|
||||
) : ideasError ? (
|
||||
<p className="dh-ideas-error" role="alert">
|
||||
{ideasError}
|
||||
</p>
|
||||
) : (
|
||||
<div className="dh-ideas-list">
|
||||
{ideas.map((idea, i) => (
|
||||
<button
|
||||
type="button"
|
||||
key={i}
|
||||
onClick={() => chooseIdea(idea)}
|
||||
>
|
||||
<small>{i + 1}</small>
|
||||
<span>{idea}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{!ideasLoading && (
|
||||
<button
|
||||
type="button"
|
||||
className="dh-ideas-refresh"
|
||||
onClick={generateIdeas}
|
||||
>
|
||||
<RefreshCw size={13} />
|
||||
{ideasError ? "重新生成" : "换一组"}
|
||||
</button>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
<div className="dh-compose-tools">
|
||||
<button
|
||||
type="button"
|
||||
disabled={busy || ideasLoading}
|
||||
onClick={generateIdeas}
|
||||
>
|
||||
<Sparkles size={14} />
|
||||
{ideasLoading ? "灵感生成中…" : "灵感回复"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="dh-mobile-linebreak"
|
||||
onClick={insertLineBreak}
|
||||
>
|
||||
换行
|
||||
</button>
|
||||
</div>
|
||||
<div className="dh-composer">
|
||||
<textarea
|
||||
ref={textarea}
|
||||
@@ -1096,6 +1315,13 @@ export function DigitalHome({
|
||||
e.target.style.height = "auto";
|
||||
e.target.style.height = `${Math.min(e.target.scrollHeight, 160)}px`;
|
||||
}}
|
||||
enterKeyHint="send"
|
||||
onCompositionStart={() => {
|
||||
imeComposing.current = true;
|
||||
}}
|
||||
onCompositionEnd={() => {
|
||||
imeComposing.current = false;
|
||||
}}
|
||||
onKeyDown={onKey}
|
||||
/>
|
||||
{busy ? (
|
||||
@@ -1298,30 +1524,22 @@ export function DigitalHome({
|
||||
开启新故事
|
||||
</button>
|
||||
</div>
|
||||
{summaries.length > 0 && (
|
||||
<p className="dh-swipe-hint">向左滑动对话可删除</p>
|
||||
)}
|
||||
<div className="dh-conversation-list">
|
||||
{summaries.map((s) => {
|
||||
const c = cards.find((c) => c.id === s.characterId);
|
||||
return (
|
||||
<button
|
||||
key={s.id}
|
||||
onClick={() => openConversation(s.id)}
|
||||
disabled={opening}
|
||||
>
|
||||
<img src={c?.cover || "/icon.svg"} alt="" />
|
||||
<div>
|
||||
<h3>{s.title}</h3>
|
||||
<p>{c?.description || "继续这段私人对话"}</p>
|
||||
</div>
|
||||
<small>
|
||||
{new Date(s.updatedAt).toLocaleDateString("zh-CN", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
})}
|
||||
</small>
|
||||
<MessageCircle size={18} />
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
{summaries.map((s) => (
|
||||
<ConversationRow
|
||||
key={s.id}
|
||||
summary={s}
|
||||
card={cards.find((c) => c.id === s.characterId)}
|
||||
revealed={swipedConversation === s.id}
|
||||
disabled={opening || deleting}
|
||||
onReveal={setSwipedConversation}
|
||||
onOpen={() => openConversation(s.id)}
|
||||
onDelete={() => requestDelete(s)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
{!summaries.length && (
|
||||
<div className="dh-empty">
|
||||
@@ -1748,6 +1966,17 @@ export function DigitalHome({
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{deleteTarget && (
|
||||
<ConversationDeleteDialog
|
||||
title={deleteTarget.title}
|
||||
busy={deleting}
|
||||
error={deleteError}
|
||||
onCancel={() => {
|
||||
if (!deleting) setDeleteTarget(null);
|
||||
}}
|
||||
onConfirm={confirmDelete}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user