Skip to content

Commit

Permalink
Add option to hide alerts in useSendMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Jul 4, 2023
1 parent 6a47099 commit 6c5676c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion utils/gear-hooks/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion utils/gear-hooks/src/hooks/api/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,4 +19,5 @@ export {
useHandleCalculateGas,
useReplyCalculateGas,
SendMessageOptions,
UseSendMessageOptions,
};
35 changes: 26 additions & 9 deletions utils/gear-hooks/src/hooks/api/useSendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
}
});
Expand All @@ -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;
Expand All @@ -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();
});
}
Expand All @@ -82,4 +99,4 @@ function useSendMessage(destination: HexString, metadata: ProgramMetadata | unde
return sendMessage;
}

export { useSendMessage, SendMessageOptions };
export { useSendMessage, SendMessageOptions, UseSendMessageOptions };
2 changes: 2 additions & 0 deletions utils/gear-hooks/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useHandleCalculateGas,
useReplyCalculateGas,
SendMessageOptions,
UseSendMessageOptions,
} from './api';
import { useAccount, useAlert, useApi } from './context';
import { useCreateHandler } from './handlers';
Expand All @@ -28,4 +29,5 @@ export {
useApi,
useCreateHandler,
SendMessageOptions,
UseSendMessageOptions,
};
2 changes: 2 additions & 0 deletions utils/gear-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
useApi,
useCreateHandler,
SendMessageOptions,
UseSendMessageOptions,
} from './hooks';

import { withoutCommas } from './utils';
Expand Down Expand Up @@ -74,4 +75,5 @@ export {
ProviderProps,
Account,
SendMessageOptions,
UseSendMessageOptions,
};

0 comments on commit 6c5676c

Please sign in to comment.