fix(chat): isolate pending flows and cancel stale requests
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user