Skip to content

Commit

Permalink
Generate sessionId inside useCancelPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Nov 26, 2024
1 parent d196200 commit d665393
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ export default function DepositBTCModal() {
queryKey: userKeys.balance(),
})

const sessionId = useRef(Math.random())
const { cancel, resolve, sessionIdToPromise } = useCancelPromise(
sessionId.current,
"Deposit cancelled",
)
const { cancel, resolve, shouldOpenErrorModal } =
useCancelPromise("Deposit cancelled")

const onStakeBTCSuccess = useCallback(() => {
handleBitcoinBalanceInvalidation()
Expand Down Expand Up @@ -65,15 +62,15 @@ export default function DepositBTCModal() {

const onDepositBTCError = useCallback(
(error: unknown) => {
if (!sessionIdToPromise[sessionId.current].shouldOpenErrorModal) return
if (!shouldOpenErrorModal) return

if (eip1193.didUserRejectRequest(error)) {
handlePause()
} else {
onError(error)
}
},
[sessionIdToPromise, handlePause, onError],
[shouldOpenErrorModal, handlePause, onError],
)

const { mutate: sendBitcoinTransaction, status } = useDepositBTCTransaction({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export default function SignMessageModal() {
const handleBitcoinPositionInvalidation = useInvalidateQueries({
queryKey: userKeys.position(),
})
const sessionId = useRef(Math.random())
const { cancel, resolve, sessionIdToPromise } = useCancelPromise(
sessionId.current,
const { cancel, resolve, shouldOpenErrorModal } = useCancelPromise(
"Withdrawal cancelled",
)
const { transactionFee } = useTransactionDetails(
Expand Down Expand Up @@ -70,15 +68,15 @@ export default function SignMessageModal() {

const onError = useCallback(
(error: unknown) => {
if (!sessionIdToPromise[sessionId.current].shouldOpenErrorModal) return
if (!shouldOpenErrorModal) return

if (eip1193.didUserRejectRequest(error)) {
handlePause()
} else {
onSignMessageError(error)
}
},
[sessionIdToPromise, handlePause, onSignMessageError],
[shouldOpenErrorModal, handlePause, onSignMessageError],
)

const { mutate: handleSignMessage } = useMutation({
Expand Down
26 changes: 15 additions & 11 deletions dapp/src/hooks/useCancelPromise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect } from "react"
import { useCallback, useEffect, useRef } from "react"

const sessionIdToPromise: Record<
number,
Expand All @@ -9,42 +9,46 @@ const sessionIdToPromise: Record<
}
> = {}

export default function useCancelPromise(
sessionId: number,
errorMsgText: string,
) {
export default function useCancelPromise(errorMsgText: string) {
const sessionId = useRef(Math.random())

useEffect(() => {
let cancel = (_: Error) => {}
const promise: Promise<void> = new Promise((_, reject) => {
cancel = reject
})

sessionIdToPromise[sessionId] = {
sessionIdToPromise[sessionId.current] = {
cancel,
promise,
shouldOpenErrorModal: true,
}
}, [sessionId])

const cancel = useCallback(() => {
const sessionData = sessionIdToPromise[sessionId]
sessionIdToPromise[sessionId] = {
const currentSessionId = sessionId.current
const sessionData = sessionIdToPromise[currentSessionId]
sessionIdToPromise[currentSessionId] = {
...sessionData,
shouldOpenErrorModal: false,
}

sessionIdToPromise[sessionId].cancel(new Error(errorMsgText))
sessionIdToPromise[currentSessionId].cancel(new Error(errorMsgText))
}, [errorMsgText, sessionId])

const resolve = useCallback(
() =>
Promise.race([sessionIdToPromise[sessionId].promise, Promise.resolve()]),
Promise.race([
sessionIdToPromise[sessionId.current].promise,
Promise.resolve(),
]),
[sessionId],
)

return {
cancel,
resolve,
sessionIdToPromise,
shouldOpenErrorModal:
sessionIdToPromise[sessionId.current]?.shouldOpenErrorModal,
}
}

0 comments on commit d665393

Please sign in to comment.