Close colleague wait modal when order is no longer pending.

Settle on pending:false or any non-waiting status, and stop polling after a max wait so rejected orders do not leave the overlay stuck.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-07 22:49:32 +03:30
co-authored by Cursor
parent 47beece729
commit e9abc9840a
+29 -14
View File
@@ -10,6 +10,7 @@ import { getToken } from '@/utils'
export const colleagueOrderWaiting = ref(false)
const CLIENT_POLL_MS = 5000
const MAX_WAIT_MS = 15 * 60 * 1000
const STREAM_EVENT = 'colleague-order'
type WaitResult = 'success' | 'failed' | 'none'
@@ -68,17 +69,31 @@ function stsFromPayload(payload: Record<string, any> | null | undefined): number
return Number.isFinite(sts) ? sts : null
}
async function readStatus(orderId: number | null): Promise<number | null> {
/** نتیجه قطعی از پاسخ status/pending — null یعنی هنوز باید صبر کرد */
function settleFromPayload(payload: Record<string, any> | null | undefined): WaitResult | null {
if (!payload || typeof payload !== 'object') return null
const pending = payload.pending
const sts = stsFromPayload(payload)
if (sts === 2) return 'success'
if (sts === 3) return 'failed'
if (sts !== null && sts !== 4) return 'failed'
if (pending === false) {
if (sts === 2) return 'success'
return 'failed'
}
return null
}
async function readSettle(orderId: number | null): Promise<WaitResult | null> {
try {
if (orderId && orderId > 0) {
const row = (await getColleagueOrderStatus(orderId)) as Record<string, any>
return stsFromPayload(row)
return settleFromPayload(row)
}
const row = (await getPendingColleagueOrder()) as Record<string, any>
if (row?.pending === false && row?.sts == null) {
return null
}
return stsFromPayload(row)
return settleFromPayload(row)
} catch {
return null
}
@@ -90,6 +105,7 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise<Wa
inflight = (async () => {
colleagueOrderWaiting.value = true
let settled: WaitResult | null = null
const startedAt = Date.now()
const finish = (result: WaitResult) => {
if (settled) return
@@ -101,22 +117,21 @@ export async function waitForColleagueOrder(orderId?: number | null): Promise<Wa
if (orderId && Number(detail?.order_id) > 0 && Number(detail.order_id) !== orderId) {
return
}
const sts = stsFromPayload(detail)
if (sts === 2) finish('success')
if (sts === 3) finish('failed')
const result = settleFromPayload(detail ?? null)
if (result) finish(result)
}
window.addEventListener(STREAM_EVENT, onStream)
try {
while (!settled) {
const sts = await readStatus(orderId ?? null)
if (sts === 2) {
finish('success')
if (Date.now() - startedAt > MAX_WAIT_MS) {
finish('failed')
break
}
if (sts === 3) {
finish('failed')
const result = await readSettle(orderId ?? null)
if (result) {
finish(result)
break
}
await new Promise<void>((resolve) => {