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
@@ -11,7 +11,7 @@ import { Logger } from "@/utils/logger";
const log = new Logger("UseChatPromotionBootstrap");
export function useChatPromotionBootstrap(): boolean {
export function useChatPromotionBootstrap(characterId: string): boolean {
const chatDispatch = useChatDispatch();
const startedRef = useRef(false);
const [isReady, setIsReady] = useState(false);
@@ -23,8 +23,8 @@ export function useChatPromotionBootstrap(): boolean {
void (async () => {
try {
const [entryPromotion, pendingUnlock] = await Promise.all([
consumePendingChatPromotion(),
peekPendingChatUnlock(),
consumePendingChatPromotion(characterId),
peekPendingChatUnlock(characterId),
]);
const promotion = entryPromotion ?? pendingUnlock?.promotion ?? null;
@@ -46,7 +46,7 @@ export function useChatPromotionBootstrap(): boolean {
setIsReady(true);
}
})();
}, [chatDispatch]);
}, [characterId, chatDispatch]);
return isReady;
}
@@ -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,