@dynamic-labs/sdk-react-core
Version:
A React SDK for implementing wallet web3 authentication and authorization to your website.
73 lines (70 loc) • 3.43 kB
JavaScript
'use client'
import { jsx } from 'react/jsx-runtime';
import { createContext, useState, useCallback, useMemo, useContext } from 'react';
import { ErrorCode } from '@dynamic-labs/utils';
/** @deprecated Prefer setError with a translation key instead */
const errorMessageMap = {
'-32000': 'Message signature denied',
'-32002': 'Please unlock your wallet extension and try again.',
'-32003': 'Message signature denied.',
'-32602': 'Invalid parameters. Please try again.',
'-32603': 'There was an internal error. Please try again.',
0: 'Message signature denied.',
4001: 'Message signature denied.',
4100: 'Please unlock your wallet extension and try again.',
4900: 'There was an internal error. Please try again.',
5001: 'Message signature denied.',
5002: 'Message signature denied.',
account_already_linked_to_different_profile: 'This social account is already linked to a different profile.',
connection_rejected: 'Connection rejected. Please try again.',
metamask_timeout: 'Whoops. Looks like something went wrong. Please try again.',
'missing-nonce': 'Something went wrong. Please try again.',
'missing-public-address': 'Connection cancelled. Please try again',
oauth_window_blocked: 'To connect to your social account, enable popups in your browser and then try again.',
oauth_window_timeout: 'Session time out. Please try again.',
too_many_email_verification_attempts: 'Too many email verification attempts, please try again later',
'user-cancelled': '',
wallet_not_deployed: 'Your smart wallet has not been deployed.',
wrong_email_verification_token: 'The code you entered is incorrect. Please try again.',
};
const ErrorContext = createContext(undefined);
const ErrorContextProvider = ({ children }) => {
/** The error message as a string, used as a fallback if translation is not available */
const [error, _setError] = useState(undefined);
/** The [ErrorCode] of the error, used for translation. */
const [errorCode, _setErrorCode] = useState(undefined);
const setError = useCallback((error, errorCode) => {
_setError(error);
_setErrorCode(errorCode);
}, []);
/** Uses the default error message for fallbacks */
const setDefaultError = useCallback(() => {
setError('Something went wrong. Please try again.', ErrorCode.DEFAULT);
}, [setError]);
/** @deprecated Prefer setError with a translation key instead */
const setErrorMessage = useCallback((status) => {
var _a;
setError((_a = errorMessageMap[status]) !== null && _a !== void 0 ? _a : 'Something went wrong. Please try again.');
}, [setError]);
const clearError = useCallback(() => {
_setError(undefined);
_setErrorCode(undefined);
}, []);
const value = useMemo(() => ({
clearError,
error,
errorCode,
setDefaultError,
setError,
setErrorMessage,
}), [clearError, error, errorCode, setDefaultError, setError, setErrorMessage]);
return (jsx(ErrorContext.Provider, { value: value, children: children }));
};
const useErrorContext = () => {
const context = useContext(ErrorContext);
if (context === undefined) {
throw new Error('usage of useErrorContext not wrapped in `ErrorContextProvider`.');
}
return context;
};
export { ErrorContext, ErrorContextProvider, useErrorContext };