2023-04-21 10:58:03 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { getCurrentKeyring, getSelectedAddress } from '../../../selectors';
|
|
|
|
import { getInteractiveReplacementToken } from '../../../selectors/institutional/selectors';
|
|
|
|
import { getIsUnlocked } from '../../../ducks/metamask/metamask';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import { mmiActionsFactory } from '../../../store/institutional/institution-background';
|
2023-07-13 18:27:49 +02:00
|
|
|
import { showInteractiveReplacementTokenModal } from '../../../store/institutional/institution-actions';
|
2023-04-21 10:58:03 +02:00
|
|
|
import { sha256 } from '../../../../shared/modules/hash.utils';
|
|
|
|
import {
|
|
|
|
Size,
|
|
|
|
IconColor,
|
|
|
|
AlignItems,
|
2023-07-13 18:27:49 +02:00
|
|
|
Display,
|
|
|
|
BlockSize,
|
2023-04-21 10:58:03 +02:00
|
|
|
JustifyContent,
|
|
|
|
TextColor,
|
|
|
|
TextVariant,
|
|
|
|
BackgroundColor,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import {
|
|
|
|
Icon,
|
|
|
|
IconName,
|
|
|
|
IconSize,
|
|
|
|
ButtonLink,
|
2023-07-13 18:27:49 +02:00
|
|
|
Box,
|
2023-07-18 17:15:25 +02:00
|
|
|
Text,
|
2023-04-21 10:58:03 +02:00
|
|
|
} from '../../component-library';
|
|
|
|
|
|
|
|
const InteractiveReplacementTokenNotification = ({ isVisible }) => {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const mmiActions = mmiActionsFactory();
|
|
|
|
|
|
|
|
const keyring = useSelector(getCurrentKeyring);
|
|
|
|
const address = useSelector(getSelectedAddress);
|
|
|
|
const isUnlocked = useSelector(getIsUnlocked);
|
|
|
|
const interactiveReplacementToken = useSelector(
|
|
|
|
getInteractiveReplacementToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
const [showNotification, setShowNotification] = useState(isVisible);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleShowNotification = async () => {
|
|
|
|
const hasInteractiveReplacementToken =
|
|
|
|
interactiveReplacementToken &&
|
|
|
|
Boolean(Object.keys(interactiveReplacementToken).length);
|
|
|
|
|
2023-07-13 18:27:49 +02:00
|
|
|
if (!/^Custody/u.test(keyring.type) || !hasInteractiveReplacementToken) {
|
2023-04-21 10:58:03 +02:00
|
|
|
setShowNotification(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-17 12:11:38 +02:00
|
|
|
const token = await dispatch(mmiActions.getCustodianToken(address));
|
2023-04-21 10:58:03 +02:00
|
|
|
const custodyAccountDetails = await dispatch(
|
|
|
|
mmiActions.getAllCustodianAccountsWithToken(
|
|
|
|
keyring.type.split(' - ')[1],
|
|
|
|
token,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const showNotificationValue =
|
|
|
|
isUnlocked &&
|
|
|
|
interactiveReplacementToken.oldRefreshToken &&
|
|
|
|
custodyAccountDetails &&
|
|
|
|
Boolean(Object.keys(custodyAccountDetails).length);
|
|
|
|
|
|
|
|
let tokenAccount;
|
|
|
|
|
|
|
|
if (Array.isArray(custodyAccountDetails)) {
|
|
|
|
tokenAccount = custodyAccountDetails
|
|
|
|
.filter(
|
|
|
|
(item) => item.address.toLowerCase() === address.toLowerCase(),
|
|
|
|
)
|
|
|
|
.map((item) => ({
|
|
|
|
token: item.authDetails?.refreshToken,
|
|
|
|
}))[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshTokenAccount = await sha256(
|
|
|
|
tokenAccount?.token + interactiveReplacementToken.url,
|
|
|
|
);
|
|
|
|
|
|
|
|
setShowNotification(
|
|
|
|
showNotificationValue &&
|
|
|
|
refreshTokenAccount === interactiveReplacementToken.oldRefreshToken,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
handleShowNotification();
|
2023-07-13 14:02:06 +02:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [address, interactiveReplacementToken.oldRefreshToken, isUnlocked]);
|
2023-04-21 10:58:03 +02:00
|
|
|
|
|
|
|
return showNotification ? (
|
|
|
|
<Box
|
2023-07-13 18:27:49 +02:00
|
|
|
width={BlockSize.Full}
|
|
|
|
display={Display.Flex}
|
2023-04-21 10:58:03 +02:00
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
padding={[1, 2]}
|
|
|
|
backgroundColor={BackgroundColor.backgroundAlternative}
|
|
|
|
className="interactive-replacement-token-notification"
|
|
|
|
data-testid="interactive-replacement-token-notification"
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
name={IconName.Danger}
|
|
|
|
color={IconColor.errorDefault}
|
2023-07-13 18:27:49 +02:00
|
|
|
size={IconSize.Md}
|
2023-04-21 10:58:03 +02:00
|
|
|
/>
|
2023-07-13 18:27:49 +02:00
|
|
|
<Text variant={TextVariant.bodySm} gap={2} color={TextColor.errorDefault}>
|
2023-04-21 10:58:03 +02:00
|
|
|
{t('custodySessionExpired')}
|
|
|
|
</Text>
|
2023-07-13 18:27:49 +02:00
|
|
|
<Text variant={TextVariant.bodySm}>
|
|
|
|
<ButtonLink
|
|
|
|
data-testid="show-modal"
|
|
|
|
size={Size.inherit}
|
|
|
|
marginLeft={1}
|
|
|
|
onClick={() => {
|
|
|
|
dispatch(showInteractiveReplacementTokenModal());
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('learnMoreUpperCase')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Text>
|
2023-04-21 10:58:03 +02:00
|
|
|
</Box>
|
|
|
|
) : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default InteractiveReplacementTokenNotification;
|
|
|
|
|
|
|
|
InteractiveReplacementTokenNotification.propTypes = {
|
|
|
|
isVisible: PropTypes.bool,
|
|
|
|
};
|