diff --git a/apps/web/src/stores/approval.store.ts b/apps/web/src/stores/approval.store.ts index 72929646f..dc7ac1256 100644 --- a/apps/web/src/stores/approval.store.ts +++ b/apps/web/src/stores/approval.store.ts @@ -176,6 +176,14 @@ export const useApprovalStore = create()( // Sign Approval // ========================================================================== signApproval: async (id, signerId, signerName, comment) => { + // 🔧 Race Condition 修復: 簽核期間暫停 Polling + const wasPolling = pollingTimer !== null + if (wasPolling) { + clearInterval(pollingTimer!) + pollingTimer = null + console.log('[Approval] Polling paused during sign') + } + set({ signingId: id, error: null }) try { @@ -219,6 +227,14 @@ export const useApprovalStore = create()( } }) + // 🔧 Race Condition 修復: 延遲 1 秒後恢復 Polling,讓後端有時間更新 + if (wasPolling) { + setTimeout(() => { + get().startPolling(get().pollingInterval || DEFAULT_POLLING_INTERVAL) + console.log('[Approval] Polling resumed after sign') + }, 1000) + } + return result } catch (err) { console.error('[Approval] Sign failed:', err) @@ -226,6 +242,13 @@ export const useApprovalStore = create()( signingId: null, error: `Sign failed: ${err}`, }) + + // 🔧 失敗也要恢復 Polling + if (wasPolling) { + get().startPolling(get().pollingInterval || DEFAULT_POLLING_INTERVAL) + console.log('[Approval] Polling resumed after sign error') + } + return null } }, @@ -234,6 +257,14 @@ export const useApprovalStore = create()( // Reject Approval // ========================================================================== rejectApproval: async (id, rejectorId, rejectorName, reason) => { + // 🔧 Race Condition 修復: 拒絕期間暫停 Polling + const wasPolling = pollingTimer !== null + if (wasPolling) { + clearInterval(pollingTimer!) + pollingTimer = null + console.log('[Approval] Polling paused during reject') + } + set({ rejectingId: id, error: null }) try { @@ -266,6 +297,14 @@ export const useApprovalStore = create()( } }) + // 🔧 Race Condition 修復: 延遲 1 秒後恢復 Polling + if (wasPolling) { + setTimeout(() => { + get().startPolling(get().pollingInterval || DEFAULT_POLLING_INTERVAL) + console.log('[Approval] Polling resumed after reject') + }, 1000) + } + return true } catch (err) { console.error('[Approval] Reject failed:', err) @@ -273,6 +312,13 @@ export const useApprovalStore = create()( rejectingId: null, error: `Reject failed: ${err}`, }) + + // 🔧 失敗也要恢復 Polling + if (wasPolling) { + get().startPolling(get().pollingInterval || DEFAULT_POLLING_INTERVAL) + console.log('[Approval] Polling resumed after reject error') + } + return false } },