fix(chat): isolate pending flows and cancel stale requests

This commit is contained in:
2026-07-17 19:22:01 +08:00
parent 2fc312b5c7
commit 8bb1e21886
27 changed files with 501 additions and 92 deletions
@@ -79,6 +79,7 @@ export function useChatUnlockCoordinator({
const trackedPaywallRef = useRef<string | null>(null);
const chatState = useChatSelector(
(state) => ({
characterId: state.context.characterId,
historyLoaded: state.context.historyLoaded,
isUnlockingHistory: state.matches({ userSession: "unlockingHistory" }),
lockedHistoryCount: state.context.lockedHistoryCount,
@@ -125,7 +126,7 @@ export function useChatUnlockCoordinator({
let cancelled = false;
const resumePendingUnlock = async () => {
const pending = await peekPendingChatUnlock();
const pending = await peekPendingChatUnlock(chatState.characterId);
if (
cancelled ||
!pending ||
@@ -138,7 +139,7 @@ export function useChatUnlockCoordinator({
return;
}
const consumed = await consumePendingChatUnlock();
const consumed = await consumePendingChatUnlock(chatState.characterId);
if (cancelled || !consumed) return;
chatDispatch({
@@ -157,6 +158,7 @@ export function useChatUnlockCoordinator({
};
}, [
chatDispatch,
chatState.characterId,
chatState.historyLoaded,
defaultReturnUrl,
enabled,