fix(web): 審核完成後正確導航到下一張卡片
問題: handleApprove 是異步的,但 setSelectedIndex 不等它完成就執行 修復: 1. fetchPendingApprovals 返回新的審核數量 2. handleApprove 返回 newCount 3. onApprove 使用 await 等待完成後,根據 newCount 決定導航 4. newCount > 0 且 index 有效: 保持 Modal 開啟顯示下一筆 5. newCount = 0: 關閉 Modal Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -126,9 +126,9 @@ export function OpenClawStateMachine({
|
||||
// ==========================================================================
|
||||
// API: Fetch Pending Approvals
|
||||
// ==========================================================================
|
||||
const fetchPendingApprovals = useCallback(async () => {
|
||||
const fetchPendingApprovals = useCallback(async (): Promise<number> => {
|
||||
const apiBaseUrl = getApiBaseUrl()
|
||||
if (!apiBaseUrl) return
|
||||
if (!apiBaseUrl) return 0
|
||||
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
@@ -160,10 +160,12 @@ export function OpenClawStateMachine({
|
||||
}
|
||||
|
||||
console.log('[OpenClaw] Fetched approvals:', data.count)
|
||||
return data.count
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown error'
|
||||
setError(message)
|
||||
console.error('[OpenClaw] Fetch error:', message)
|
||||
return 0
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
@@ -205,11 +207,13 @@ export function OpenClawStateMachine({
|
||||
}
|
||||
|
||||
console.log('[OpenClaw] Approval signed by', signer.name, ':', approvalId)
|
||||
// Refresh approvals list
|
||||
await fetchPendingApprovals()
|
||||
// Refresh approvals list and return new count
|
||||
const newCount = await fetchPendingApprovals()
|
||||
return newCount
|
||||
} catch (err) {
|
||||
console.error('[OpenClaw] Sign error:', err)
|
||||
setError(err instanceof Error ? err.message : 'Sign failed')
|
||||
return -1
|
||||
}
|
||||
}, [fetchPendingApprovals])
|
||||
|
||||
@@ -423,17 +427,23 @@ export function OpenClawStateMachine({
|
||||
{selectedApproval && (
|
||||
<ApprovalCard
|
||||
request={selectedApproval}
|
||||
onApprove={() => {
|
||||
handleApprove(selectedApproval.id)
|
||||
// 簽核後移到下一個或關閉
|
||||
if (selectedIndex !== null && selectedIndex < pendingApprovals.length - 1) {
|
||||
setSelectedIndex(selectedIndex + 1)
|
||||
onApprove={async () => {
|
||||
const newCount = await handleApprove(selectedApproval.id) ?? 0
|
||||
// 簽核後: 根據 API 返回的新數量決定導航
|
||||
if (newCount > 0 && selectedIndex !== null) {
|
||||
// 還有待審核項目,保持 Modal 開啟
|
||||
// 如果當前 index 超出範圍,調整到最後一筆
|
||||
if (selectedIndex >= newCount) {
|
||||
setSelectedIndex(newCount - 1)
|
||||
}
|
||||
// 否則保持 index 不變,列表刷新後自動顯示下一筆
|
||||
} else {
|
||||
// 沒有待審核項目了,關閉 Modal
|
||||
setSelectedIndex(null)
|
||||
}
|
||||
}}
|
||||
onReject={() => {
|
||||
handleReject(selectedApproval.id)
|
||||
onReject={async () => {
|
||||
await handleReject(selectedApproval.id)
|
||||
setSelectedIndex(null)
|
||||
}}
|
||||
holdDuration={1000}
|
||||
|
||||
Reference in New Issue
Block a user