修复页面切换后图片生成结果不更新问题
This commit is contained in:
parent
b3da726b2d
commit
b36edf51f1
@ -65,6 +65,33 @@ export function Chat({
|
||||
|
||||
// 🆕 持久化生成任务状态(解决用户离开再返回时加载状态丢失)
|
||||
const { pendingGeneration, startGeneration, completeGeneration } = usePendingGeneration(id);
|
||||
|
||||
// 🆕 标记是否已经尝试刷新过(避免无限刷新)
|
||||
const hasTriedRefreshRef = useRef(false);
|
||||
|
||||
// 🆕 当检测到恢复的 pendingGeneration 时,刷新页面获取最新消息
|
||||
useEffect(() => {
|
||||
if (pendingGeneration && !hasTriedRefreshRef.current) {
|
||||
// 检查最后一条消息是否已经是 AI 回复
|
||||
const lastMessage = initialMessages.length > 0 ? initialMessages[initialMessages.length - 1] : null;
|
||||
|
||||
if (lastMessage?.role === "assistant") {
|
||||
// 最后一条已经是 AI 回复,说明生成已完成,清除 pending 状态
|
||||
console.log("[Chat] 🎉 检测到生成已完成(最后消息是 AI 回复),清除 pending 状态");
|
||||
completeGeneration();
|
||||
} else if (lastMessage?.role === "user") {
|
||||
// 最后一条是用户消息,可能还在生成中,或者已经完成但页面数据过期
|
||||
// 标记已尝试刷新,然后刷新页面获取最新数据
|
||||
hasTriedRefreshRef.current = true;
|
||||
console.log("[Chat] 🔄 检测到恢复的 pending 状态,刷新页面获取最新消息...");
|
||||
|
||||
// 延迟一小段时间再刷新,避免立即刷新
|
||||
setTimeout(() => {
|
||||
router.refresh();
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
}, [pendingGeneration, initialMessages, completeGeneration, router]);
|
||||
|
||||
const { mutate } = useSWRConfig();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user