diff --git a/utils/gear-hooks/package.json b/utils/gear-hooks/package.json index 4d7090b7cf..9cfe6a7336 100644 --- a/utils/gear-hooks/package.json +++ b/utils/gear-hooks/package.json @@ -1,6 +1,6 @@ { "name": "@gear-js/react-hooks", - "version": "0.5.21", + "version": "0.5.22", "description": "React hooks used across Gear applications", "author": "Gear Technologies", "license": "GPL-3.0", diff --git a/utils/gear-hooks/src/hooks/api/index.ts b/utils/gear-hooks/src/hooks/api/index.ts index 42906e8671..8720093059 100644 --- a/utils/gear-hooks/src/hooks/api/index.ts +++ b/utils/gear-hooks/src/hooks/api/index.ts @@ -1,5 +1,5 @@ import { useReadFullState, useReadWasmState } from './useReadState'; -import { useSendMessage, SendMessageOptions } from './useSendMessage'; +import { useSendMessage, SendMessageOptions, UseSendMessageOptions } from './useSendMessage'; import { useUploadProgram, useCreateProgram } from './useProgram'; import { useUploadCalculateGas, @@ -19,4 +19,5 @@ export { useHandleCalculateGas, useReplyCalculateGas, SendMessageOptions, + UseSendMessageOptions, }; diff --git a/utils/gear-hooks/src/hooks/api/useSendMessage.ts b/utils/gear-hooks/src/hooks/api/useSendMessage.ts index ffe5a8271a..2aa5d45e3b 100644 --- a/utils/gear-hooks/src/hooks/api/useSendMessage.ts +++ b/utils/gear-hooks/src/hooks/api/useSendMessage.ts @@ -8,6 +8,11 @@ import { AccountContext, AlertContext, ApiContext } from 'context'; import { DEFAULT_ERROR_OPTIONS, DEFAULT_SUCCESS_OPTIONS } from 'consts'; import { getAutoGasLimit } from 'utils'; +type UseSendMessageOptions = { + isMaxGasLimit?: boolean; + disableAlerts?: boolean; +}; + type SendMessageOptions = { value?: string | number; isOtherPanicsAllowed?: boolean; @@ -17,7 +22,11 @@ type SendMessageOptions = { const MAX_GAS_LIMIT = 250000000000; -function useSendMessage(destination: HexString, metadata: ProgramMetadata | undefined, isMaxGasLimit = false) { +function useSendMessage( + destination: HexString, + metadata: ProgramMetadata | undefined, + { isMaxGasLimit = true, disableAlerts }: UseSendMessageOptions, +) { const { api } = useContext(ApiContext); // сircular dependency fix const { account } = useContext(AccountContext); const alert = useContext(AlertContext); @@ -27,10 +36,12 @@ function useSendMessage(destination: HexString, metadata: ProgramMetadata | unde const handleEventsStatus = (events: EventRecord[], onSuccess?: () => void, onError?: () => void) => { events.forEach(({ event: { method, section } }) => { if (method === 'MessageQueued') { - alert.success(`${section}.MessageQueued`); + if (!disableAlerts) alert.success(`${section}.MessageQueued`); + onSuccess && onSuccess(); } else if (method === 'ExtrinsicFailed') { alert.error('Extrinsic Failed', { title }); + onError && onError(); } }); @@ -41,20 +52,25 @@ function useSendMessage(destination: HexString, metadata: ProgramMetadata | unde const { isReady, isInBlock, isInvalid, isFinalized } = status; if (isInvalid) { - alert.update(alertId, 'Transaction error. Status: isInvalid', DEFAULT_ERROR_OPTIONS); - } else if (isReady) { + if (alertId) { + alert.update(alertId, 'Transaction error. Status: isInvalid', DEFAULT_ERROR_OPTIONS); + } else { + alert.error('Transaction error. Status: isInvalid'); + } + } else if (isReady && alertId) { alert.update(alertId, 'Ready'); - } else if (isInBlock) { + } else if (isInBlock && alertId) { alert.update(alertId, 'In Block'); } else if (isFinalized) { - alert.update(alertId, 'Finalized', DEFAULT_SUCCESS_OPTIONS); + if (alertId) alert.update(alertId, 'Finalized', DEFAULT_SUCCESS_OPTIONS); + handleEventsStatus(events, onSuccess, onError); } }; const sendMessage = (payload: AnyJson, options?: SendMessageOptions) => { if (account && metadata) { - const alertId = alert.loading('Sign In', { title }); + const alertId = disableAlerts ? '' : alert.loading('Sign In', { title }); const { value = 0, isOtherPanicsAllowed = false, onSuccess, onError } = options || {}; const { address, decodedAddress, meta } = account; @@ -73,7 +89,8 @@ function useSendMessage(destination: HexString, metadata: ProgramMetadata | unde api.message.signAndSend(address, { signer }, (result) => handleStatus(result, alertId, onSuccess, onError)), ) .catch(({ message }: Error) => { - alert.update(alertId, message, DEFAULT_ERROR_OPTIONS); + if (alertId) alert.update(alertId, message, DEFAULT_ERROR_OPTIONS); + onError && onError(); }); } @@ -82,4 +99,4 @@ function useSendMessage(destination: HexString, metadata: ProgramMetadata | unde return sendMessage; } -export { useSendMessage, SendMessageOptions }; +export { useSendMessage, SendMessageOptions, UseSendMessageOptions }; diff --git a/utils/gear-hooks/src/hooks/index.ts b/utils/gear-hooks/src/hooks/index.ts index 9961e54d43..b3b96e7ef5 100644 --- a/utils/gear-hooks/src/hooks/index.ts +++ b/utils/gear-hooks/src/hooks/index.ts @@ -9,6 +9,7 @@ import { useHandleCalculateGas, useReplyCalculateGas, SendMessageOptions, + UseSendMessageOptions, } from './api'; import { useAccount, useAlert, useApi } from './context'; import { useCreateHandler } from './handlers'; @@ -28,4 +29,5 @@ export { useApi, useCreateHandler, SendMessageOptions, + UseSendMessageOptions, }; diff --git a/utils/gear-hooks/src/index.ts b/utils/gear-hooks/src/index.ts index 90cb9031ff..31894e5ff0 100644 --- a/utils/gear-hooks/src/index.ts +++ b/utils/gear-hooks/src/index.ts @@ -13,6 +13,7 @@ import { useApi, useCreateHandler, SendMessageOptions, + UseSendMessageOptions, } from './hooks'; import { withoutCommas } from './utils'; @@ -74,4 +75,5 @@ export { ProviderProps, Account, SendMessageOptions, + UseSendMessageOptions, };