feat(ai-writing): 后台挂起改为即时返回,乐观标记历史「挂起中」
点击「转后台」后立刻 reset 回初始页,挂起请求 fire-and-forget; 历史下拉靠 markBackground/clearBackground 乐观标记马上显示「挂起中」, 不等后端确认,与「多智能体会商」交互一致。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b59cbd51d9
commit
0a456e9146
2 files changed
+56
-15
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
|
||||
import {
|
||||
listAIWritingSessions,
|
||||
@@ -10,6 +10,27 @@ import type { AIWritingSessionSummary } from '../api/ai-writing-sessions';
|
||||
export function useAIWritingSessions() {
|
||||
const [sessions, setSessions] = useState<AIWritingSessionSummary[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
// 「后台挂起」乐观标记:点击挂起后立刻让历史下拉显示「挂起中」,不必等后端确认。
|
||||
// 后端状态(status='background')刷新到位后由调用方 clearBackground 清除。
|
||||
const [pendingBackground, setPendingBackground] = useState<Set<string>>(() => new Set());
|
||||
|
||||
const markBackground = useCallback((id: string) => {
|
||||
setPendingBackground((prev) => {
|
||||
if (prev.has(id)) return prev;
|
||||
const next = new Set(prev);
|
||||
next.add(id);
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const clearBackground = useCallback((id: string) => {
|
||||
setPendingBackground((prev) => {
|
||||
if (!prev.has(id)) return prev;
|
||||
const next = new Set(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setLoading(true);
|
||||
@@ -41,5 +62,18 @@ export function useAIWritingSessions() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { sessions, loading, refresh, rename, remove };
|
||||
// 把乐观「挂起中」标记叠加到真实列表上:已在列表里的会话立刻显示 background。
|
||||
const displaySessions = useMemo(
|
||||
() =>
|
||||
pendingBackground.size === 0
|
||||
? sessions
|
||||
: sessions.map((s) =>
|
||||
pendingBackground.has(s.id) && s.status !== 'done'
|
||||
? { ...s, status: 'background' }
|
||||
: s,
|
||||
),
|
||||
[sessions, pendingBackground],
|
||||
);
|
||||
|
||||
return { sessions: displaySessions, loading, refresh, rename, remove, markBackground, clearBackground };
|
||||
}
|
||||
Reference in new issue
Block a user