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

refactor: routes.component.js and creation of ToastMaster #27735

Open
wants to merge 34 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0928ee9
kinda stable but some layouts are off
HowardBraham Oct 3, 2024
d0895c3
stable, fixing some toasts
HowardBraham Oct 3, 2024
f9695dd
lint
HowardBraham Oct 5, 2024
66c10fd
getShowSurveyToast
HowardBraham Oct 8, 2024
28a379a
moving things to toast-master.js
HowardBraham Oct 8, 2024
4a8232a
moved the test
HowardBraham Oct 8, 2024
b56dd9c
showPrivacyPolicyToast
HowardBraham Oct 9, 2024
f4b3232
showNftEnablementToast
HowardBraham Oct 9, 2024
c1f69ca
useNftDetection
HowardBraham Oct 9, 2024
7a86c88
PermittedNetworkToast
HowardBraham Oct 9, 2024
beedaa1
remove console
HowardBraham Oct 9, 2024
a1d0fe1
fix hooks
HowardBraham Oct 9, 2024
322832e
removed eslint-disable
HowardBraham Oct 12, 2024
ba03d8f
fixed import
HowardBraham Oct 9, 2024
cb00324
setOnboardingDate
HowardBraham Oct 9, 2024
04c5ef9
convert toast-master-selectors to TS
HowardBraham Oct 9, 2024
e92846e
cleanup isolated.js
HowardBraham Oct 9, 2024
39ededb
rename isolated.js to routes-helpers.js
HowardBraham Oct 9, 2024
0f96be8
move toast-master to its own folder
HowardBraham Oct 9, 2024
4122c85
fixed unit tests
HowardBraham Oct 9, 2024
f1f39d3
import { submitRequestToBackground }
HowardBraham Oct 9, 2024
454f330
state suggestion from @MajorLift
HowardBraham Oct 9, 2024
112ade5
fixed merge conflict from #27561
HowardBraham Oct 10, 2024
dc95e2d
moved getShowConnectAccountToast()
HowardBraham Oct 14, 2024
36c5f1c
renamed get to select
HowardBraham Oct 14, 2024
77bbd7f
renamed routes-helpers.js to utils.js
HowardBraham Oct 14, 2024
dd0d26a
split to selectors.ts and utils.ts
HowardBraham Oct 14, 2024
dafb010
usePrevious
HowardBraham Oct 14, 2024
8fae36e
Split ToastMaster into several separate local components
HowardBraham Oct 14, 2024
ae6a552
fix lint errors
HowardBraham Oct 14, 2024
66bcb75
fixing rules of hooks
HowardBraham Oct 15, 2024
a43a077
submitRequestToBackgroundAndCatch
HowardBraham Oct 16, 2024
5eb0703
@MajorLift fix
HowardBraham Oct 16, 2024
bfd7b86
rename to isShown
HowardBraham Oct 17, 2024
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { BannerAlert } from '../../../../component-library';
import { useI18nContext } from '../../../../../hooks/useI18nContext';
import { getOpenSeaEnabled } from '../../../../../selectors';
import {
detectNfts,
setOpenSeaEnabled,
setShowNftDetectionEnablementToast,
setUseNftDetection,
} from '../../../../../store/actions';
import { getOpenSeaEnabled } from '../../../../../selectors';
import { BannerAlert } from '../../../../component-library';
import { setShowNftDetectionEnablementToast } from '../../../toast-master/utils';

export default function NFTsDetectionNoticeNFTsTab() {
const t = useI18nContext();
Expand Down
108 changes: 108 additions & 0 deletions ui/components/app/toast-master/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { InternalAccount, isEvmAccountType } from '@metamask/keyring-api';
import { getAlertEnabledness } from '../../../ducks/metamask/metamask';
import { PRIVACY_POLICY_DATE } from '../../../helpers/constants/privacy-policy';
import {
SURVEY_DATE,
SURVEY_END_TIME,
SURVEY_START_TIME,
} from '../../../helpers/constants/survey';
import { getPermittedAccountsForCurrentTab } from '../../../selectors';
import { MetaMaskReduxState } from '../../../store/store';
import { getIsPrivacyToastRecent } from './utils';

// TODO: get this into one of the larger definitions of state type
type State = Omit<MetaMaskReduxState, 'appState'> & {
appState: {
showNftDetectionEnablementToast?: boolean;
};
metamask: {
newPrivacyPolicyToastClickedOrClosed?: boolean;
newPrivacyPolicyToastShownDate?: number;
onboardingDate?: number;
showNftDetectionEnablementToast?: boolean;
surveyLinkLastClickedOrClosed?: number;
switchedNetworkNeverShowMessage?: boolean;
};
};

/**
* Determines if the survey toast should be shown based on the current time, survey start and end times, and whether the survey link was last clicked or closed.
*
* @param state - The application state containing the necessary survey data.
* @returns True if the current time is between the survey start and end times and the survey link was not last clicked or closed. False otherwise.
*/
export function selectShowSurveyToast(state: State): boolean {
if (state.metamask?.surveyLinkLastClickedOrClosed) {
return false;
}

const startTime = new Date(`${SURVEY_DATE} ${SURVEY_START_TIME}`).getTime();
const endTime = new Date(`${SURVEY_DATE} ${SURVEY_END_TIME}`).getTime();
const now = Date.now();

return now > startTime && now < endTime;
}

/**
* Determines if the privacy policy toast should be shown based on the current date and whether the new privacy policy toast was clicked or closed.
*
* @param state - The application state containing the privacy policy data.
* @returns Boolean is True if the toast should be shown, and the number is the date the toast was last shown.
*/
export function selectShowPrivacyPolicyToast(state: State): {
showPrivacyPolicyToast: boolean;
newPrivacyPolicyToastShownDate?: number;
} {
const {
newPrivacyPolicyToastClickedOrClosed,
newPrivacyPolicyToastShownDate,
onboardingDate,
} = state.metamask || {};
const newPrivacyPolicyDate = new Date(PRIVACY_POLICY_DATE);
const currentDate = new Date(Date.now());

const showPrivacyPolicyToast =
!newPrivacyPolicyToastClickedOrClosed &&
currentDate >= newPrivacyPolicyDate &&
getIsPrivacyToastRecent(newPrivacyPolicyToastShownDate) &&
// users who onboarded before the privacy policy date should see the notice
// and
// old users who don't have onboardingDate set should see the notice
(!onboardingDate || onboardingDate < newPrivacyPolicyDate.valueOf());

return { showPrivacyPolicyToast, newPrivacyPolicyToastShownDate };
}

export function selectNftDetectionEnablementToast(state: State): boolean {
return Boolean(state.appState?.showNftDetectionEnablementToast);
}

// If there is more than one connected account to activeTabOrigin,
// *BUT* the current account is not one of them, show the banner
export function selectShowConnectAccountToast(
state: State,
account: InternalAccount,
): boolean {
const allowShowAccountSetting = getAlertEnabledness(state).unconnectedAccount;
const connectedAccounts = getPermittedAccountsForCurrentTab(state);
const isEvmAccount = isEvmAccountType(account?.type);

return (
allowShowAccountSetting &&
account &&
state.activeTab?.origin &&
isEvmAccount &&
connectedAccounts.length > 0 &&
!connectedAccounts.some((address) => address === account.address)
);
}

/**
* Retrieves user preference to never see the "Switched Network" toast
*
* @param state - Redux state object.
* @returns Boolean preference value
*/
export function selectSwitchedNetworkNeverShowMessage(state: State): boolean {
return Boolean(state.metamask.switchedNetworkNeverShowMessage);
}
Loading
Loading