Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Changes in network switched toast to include origin information also #30524

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion app/_locales/en_GB/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions ui/components/multichain/toast/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Toast = ({
className,
}: {
startAdornment: React.ReactNode | React.ReactNode[];
text: string;
text: string | string[];
actionText?: string;
onActionClick?: () => void;
onClose: () => void;
Expand Down Expand Up @@ -62,6 +62,8 @@ export const Toast = ({
return null;
}

const displayText = typeof text === 'string' ? [text] : text;

return (
<BannerBase
data-theme={theme === ThemeType.light ? ThemeType.dark : ThemeType.light}
Expand All @@ -73,9 +75,11 @@ export const Toast = ({
<Box display={Display.Flex} gap={4} data-testid={dataTestId}>
{startAdornment}
<Box>
<Text className="toast-text" variant={textVariant}>
{text}
</Text>
{displayText.map((txt) => (
<Text key={txt} className="toast-text" variant={textVariant}>
{txt}
</Text>
))}
{actionText && onActionClick ? (
<ButtonLink onClick={onActionClick}>{actionText}</ButtonLink>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const render = () => {
time: new Date().getTime(),
type: TransactionType.personalSign,
chainId: '0x1',
origin: 'https://dummy.io',
};

const mockExpectedState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,97 @@ const TOAST_TIMEOUT_MILLISECONDS = 5 * 1000; // 5 Seconds
const NetworkChangeToastLegacy = ({
confirmation,
}: {
confirmation: { id: string; chainId: string };
confirmation: { id: string; chainId: string; origin: string };
}) => {
const newChainId = confirmation?.chainId;
const [toastVisible, setToastVisible] = useState(false);
const { chainId: newChainId, origin: newOrigin } = confirmation ?? {};
const [toastMessage, setToastMessage] = useState<string[]>([]);
const t = useI18nContext();

const network = useSelector((state) =>
selectNetworkConfigurationByChainId(state, newChainId),
);

const hideToast = useCallback(() => {
setToastVisible(false);
}, [setToastVisible]);
setToastMessage([]);
}, [setToastMessage]);

useEffect(() => {
let isMounted = true;

if (!confirmation) {
if (!confirmation?.id) {
return undefined;
}

(async () => {
const lastInteractedConfirmationInfo =
await getLastInteractedConfirmationInfo();
const currentTimestamp = new Date().getTime();
if (
lastInteractedConfirmationInfo &&
lastInteractedConfirmationInfo.chainId !== newChainId &&
currentTimestamp - lastInteractedConfirmationInfo.timestamp <=
CHAIN_CHANGE_THRESHOLD_MILLISECONDS &&
isMounted
) {
setToastVisible(true);
setTimeout(() => {
if (isMounted) {
hideToast();

if (lastInteractedConfirmationInfo) {
const currentTimestamp = new Date().getTime();

const timeSinceLastConfirmation =
currentTimestamp - lastInteractedConfirmationInfo.timestamp;

const recentlyViewedOtherConfirmation =
timeSinceLastConfirmation <= CHAIN_CHANGE_THRESHOLD_MILLISECONDS;

if (recentlyViewedOtherConfirmation && isMounted) {
const { chainId, origin } = lastInteractedConfirmationInfo;

const messages: string[] = [];
if (chainId !== newChainId) {
messages.push(t('networkSwitchMessage', [network.name ?? '']));
}

if (origin !== newOrigin) {
messages.push(t('originSwitchMessage', [new URL(newOrigin).host]));
}

if (messages.length) {
setToastMessage(messages);
setTimeout(() => {
if (isMounted) {
// hideToast();
}
}, TOAST_TIMEOUT_MILLISECONDS);
}
}, TOAST_TIMEOUT_MILLISECONDS);
}
}
if (
(!lastInteractedConfirmationInfo ||
lastInteractedConfirmationInfo?.id !== confirmation.id) &&
isMounted
) {

const isNewId =
!lastInteractedConfirmationInfo ||
lastInteractedConfirmationInfo?.id !== confirmation.id;

if (isNewId && isMounted) {
setLastInteractedConfirmationInfo({
id: confirmation.id,
chainId: newChainId,
origin: newOrigin,
timestamp: new Date().getTime(),
});
}
})();

return () => {
isMounted = false;
};
}, [confirmation?.id]);
}, [
confirmation?.id,
hideToast,
network.name,
newChainId,
newOrigin,
setToastMessage,
t,
]);

if (!toastVisible) {
if (!toastMessage?.length) {
return null;
}

return (
<Box className="toast_wrapper">
<Toast
onClose={hideToast}
text={t('networkSwitchMessage', [network.name ?? ''])}
startAdornment={null}
/>
<Toast onClose={hideToast} text={toastMessage} startAdornment={null} />
</Box>
);
};
Expand Down
Loading