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
@@ -5,7 +5,10 @@ import { useEffect, useRef } from "react";
import { shallowEqual } from "@xstate/react";
import { hasPendingChatUnlock } from "@/lib/navigation/chat_unlock_session";
import { useChatDispatch } from "@/stores/chat/chat-context";
import {
useChatDispatch,
useChatSelector,
} from "@/stores/chat/chat-context";
import { usePaymentSelector } from "@/stores/payment/payment-context";
export function ChatPaymentSuccessSync() {
@@ -17,6 +20,7 @@ export function ChatPaymentSuccessSync() {
}),
shallowEqual,
);
const characterId = useChatSelector((state) => state.context.characterId);
const chatDispatch = useChatDispatch();
const lastPaidKeyRef = useRef<string | null>(null);
@@ -29,7 +33,7 @@ export function ChatPaymentSuccessSync() {
let cancelled = false;
void (async () => {
if (await hasPendingChatUnlock()) return;
if (await hasPendingChatUnlock(characterId)) return;
if (!cancelled) chatDispatch({ type: "ChatPaymentSucceeded" });
})();
@@ -38,6 +42,7 @@ export function ChatPaymentSuccessSync() {
};
}, [
chatDispatch,
characterId,
paymentState.currentOrderId,
paymentState.isPaid,
paymentState.orderStatus,