import React, { useState, useContext, useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import PropTypes from 'prop-types'; import { useI18nContext } from '../../../hooks/useI18nContext'; import Button from '../../../components/ui/button'; import PulseLoader from '../../../components/ui/pulse-loader'; import { INSTITUTIONAL_FEATURES_DONE_ROUTE } from '../../../helpers/constants/routes'; import { MetaMetricsContext } from '../../../contexts/metametrics'; import { getMostRecentOverviewPage } from '../../../ducks/history/history'; import { Text, BUTTON_SIZES, BUTTON_VARIANT, } from '../../../components/component-library'; import { TextColor, TextVariant, OVERFLOW_WRAP, TEXT_ALIGN, DISPLAY, } from '../../../helpers/constants/design-system'; import Box from '../../../components/ui/box'; import { mmiActionsFactory } from '../../../store/institutional/institution-background'; export default function ConfirmAddInstitutionalFeature({ history }) { const t = useI18nContext(); const dispatch = useDispatch(); const mmiActions = mmiActionsFactory(); const [isLoading, setIsLoading] = useState(false); const [connectError, setConnectError] = useState(''); const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage); const connectRequests = useSelector( (state) => state.metamask.institutionalFeatures?.connectRequests, ); const trackEvent = useContext(MetaMetricsContext); const connectRequest = connectRequests[0]; useEffect(() => { if (!connectRequest) { history.push(mostRecentOverviewPage); } }, [connectRequest, history, mostRecentOverviewPage]); if (!connectRequest) { return null; } const serviceLabel = connectRequest.labels.find( (label) => label.key === 'service', ); const sendEvent = ({ actions, service }) => { trackEvent({ category: 'MMI', event: 'Institutional feature connection', properties: { actions, service, }, }); }; const handleConnectError = ({ message }) => { let error = message; if (message.startsWith('401')) { error = t('projectIdInvalid'); } if (!error) { error = t('connectionError'); } setIsLoading(false); setConnectError(error); sendEvent({ actions: 'Institutional feature RPC error' }); }; const removeConnectInstitutionalFeature = ({ actions, service, push }) => { dispatch( mmiActions.removeConnectInstitutionalFeature({ origin: connectRequest.origin, projectId: connectRequest.token.projectId, }), ); sendEvent({ actions, service }); history.push(push); }; const confirmAddInstitutionalFeature = async () => { setIsLoading(true); setConnectError(''); try { await dispatch( mmiActions.setComplianceAuthData({ clientId: connectRequest.token.clientId, projectId: connectRequest.token.projectId, }), ); removeConnectInstitutionalFeature({ actions: 'Institutional feature RPC confirm', service: serviceLabel.value, push: { pathname: INSTITUTIONAL_FEATURES_DONE_ROUTE, state: { imgSrc: 'images/compliance-logo.png', title: t('complianceActivatedTitle'), description: t('complianceActivatedDesc'), }, }, }); } catch (e) { handleConnectError(e); } }; sendEvent({ actions: 'Institutional feature RPC request', service: serviceLabel.value, }); return ( {t('institutionalFeatures')} {t('mmiAuthenticate', [connectRequest.origin, serviceLabel.value])} {t('projectName')} {connectRequest?.token?.projectName} {t('id')}: {connectRequest?.token?.projectId} {connectError && ( {connectError} )} {isLoading ? ( ) : ( )} ); } ConfirmAddInstitutionalFeature.propTypes = { history: PropTypes.object, };