fix: guard immediate mutation pending state (#3628)

This commit is contained in:
Neil
2026-05-30 04:09:25 -07:00
committed by GitHub
parent 6b47974729
commit fb32a9cd39
+10 -6
View File
@@ -1,4 +1,5 @@
import { useCallback, useRef, useState } from 'react'
import { useMountedRef } from './useMountedRef'
/**
* Wraps an immediate mutation (no undo delay) with loading/error state
@@ -7,6 +8,7 @@ import { useCallback, useRef, useState } from 'react'
export function useImmediateMutation() {
const [pendingKeys, setPendingKeys] = useState<Set<string>>(new Set())
const pendingRef = useRef(pendingKeys)
const mountedRef = useMountedRef()
pendingRef.current = pendingKeys
const isPending = useCallback((key: string) => pendingKeys.has(key), [pendingKeys])
@@ -40,14 +42,16 @@ export function useImmediateMutation() {
opts.onRevert?.()
opts.onError?.(err instanceof Error ? err.message : 'Update failed')
} finally {
setPendingKeys((prev) => {
const next = new Set(prev)
next.delete(key)
return next
})
if (mountedRef.current) {
setPendingKeys((prev) => {
const next = new Set(prev)
next.delete(key)
return next
})
}
}
},
[]
[mountedRef]
)
return { isPending, run }