import React, { useContext, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import PulseLoader from '../../../components/ui/pulse-loader';
import { CUSTODY_ACCOUNT_ROUTE } from '../../../helpers/constants/routes';
import {
AlignItems,
DISPLAY,
TextColor,
TEXT_ALIGN,
FLEX_DIRECTION,
} from '../../../helpers/constants/design-system';
import { BUILT_IN_NETWORKS } from '../../../../shared/constants/network';
import { I18nContext } from '../../../contexts/i18n';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import { getMostRecentOverviewPage } from '../../../ducks/history/history';
import { setProviderType } from '../../../store/actions';
import { mmiActionsFactory } from '../../../store/institutional/institution-background';
import {
Label,
Text,
ButtonLink,
Button,
BUTTON_SIZES,
BUTTON_VARIANT,
} from '../../../components/component-library';
import Box from '../../../components/ui/box';
const ConfirmAddCustodianToken = () => {
const t = useContext(I18nContext);
const dispatch = useDispatch();
const history = useHistory();
const trackEvent = useContext(MetaMetricsContext);
const mmiActions = mmiActionsFactory();
const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage);
const connectRequests = useSelector(
(state) => state.metamask.institutionalFeatures?.connectRequests,
);
const complianceActivated = useSelector((state) =>
Boolean(state.metamask.institutionalFeatures?.complianceProjectId),
);
const [showMore, setShowMore] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [connectError, setConnectError] = useState('');
const handleConnectError = (e) => {
let errorMessage = e.message;
if (!errorMessage) {
errorMessage = 'Connection error';
}
setConnectError(errorMessage);
setIsLoading(false);
};
const renderSelectedToken = () => {
const connectRequest = connectRequests ? connectRequests[0] : undefined;
return (
{showMore && connectRequest?.token
? connectRequest?.token
: `...${connectRequest?.token.slice(-9)}`}
{!showMore && (
{
setShowMore(true);
}}
>
{t('showMore')}
)}
);
};
const connectRequest = connectRequests ? connectRequests[0] : undefined;
if (!connectRequest) {
history.push(mostRecentOverviewPage);
return null;
}
trackEvent({
category: 'MMI',
event: 'Custodian onboarding',
properties: {
actions: 'Custodian RPC request',
custodian: connectRequest.custodian,
apiUrl: connectRequest.apiUrl,
},
});
let custodianLabel = '';
if (
connectRequest.labels &&
connectRequest.labels.some((label) => label.key === 'service')
) {
custodianLabel = connectRequest.labels.find(
(label) => label.key === 'service',
).value;
}
return (
{t('custodianAccount')}
{t('mmiAddToken', [connectRequest.origin])}
{custodianLabel && (
<>
{t('custodian')}
>
)}
{t('token')}
{renderSelectedToken()}
{connectRequest.apiUrl && (
{t('apiUrl')}
{connectRequest.apiUrl}
)}
{!complianceActivated && (
{connectError}
)}
{isLoading ? (
) : (
)}
);
};
export default ConfirmAddCustodianToken;