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: network value component for re-designed confirmation pages #11812

Merged
merged 10 commits into from
Oct 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ exports[`Confirm should match snapshot for personal sign 1`] = `
}
}
>
Ethereum Main Network
Mainnet
</Text>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import Badge, {
BadgeVariant,
} from '../../../../../../../component-library/components/Badges/Badge';
import BadgeWrapper from '../../../../../../../component-library/components/Badges/BadgeWrapper';
import { RootState } from '../../../../../../UI/BasicFunctionality/BasicFunctionalityModal/BasicFunctionalityModal.test';
import { selectChainId } from '../../../../../../../selectors/networkController';
import { useStyles } from '../../../../../../../component-library/hooks';
import {
selectNetworkImageSource,
selectNetworkName,
} from '../../../../../../../selectors/networkInfos';
import { RootState } from '../../../../../../UI/BasicFunctionality/BasicFunctionalityModal/BasicFunctionalityModal.test';
import useAccountInfo from '../../../../hooks/useAccountInfo';
import useApprovalRequest from '../../../../hooks/useApprovalRequest';
import useNetworkInfo from '../../../../hooks/useNetworkInfo';
import styleSheet from './AccountNetworkInfoCollapsed.styles';

const AccountNetworkInfoCollapsed = () => {
const { approvalRequest } = useApprovalRequest();
const networkName = useSelector(selectNetworkName);
const networkImage = useSelector(selectNetworkImageSource);
const chainId = useSelector(selectChainId);
const { networkName, networkImage } = useNetworkInfo(chainId);
const useBlockieIcon = useSelector(
(state: RootState) => state.settings.useBlockieIcon,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ exports[`AccountNetworkInfoCollapsed should match snapshot for personal sign 1`]
}
}
>
Ethereum Main Network
Mainnet
</Text>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { View } from 'react-native';
import { useSelector } from 'react-redux';

import { strings } from '../../../../../../../../locales/i18n';
import { selectProviderConfig } from '../../../../../../../selectors/networkController';
import { selectNetworkName } from '../../../../../../../selectors/networkInfos';
import {
selectChainId,
selectProviderConfig,
} from '../../../../../../../selectors/networkController';
import useAccountInfo from '../../../../hooks/useAccountInfo';
import useApprovalRequest from '../../../../hooks/useApprovalRequest';
import InfoSection from '../../../UI/InfoRow/InfoSection';
import InfoRow from '../../../UI/InfoRow';
import Address from '../../../UI/InfoRow/InfoValue/Address';
import InfoURL from '../../../UI/InfoRow/InfoValue/InfoURL';
import DisplayURL from '../../../UI/InfoRow/InfoValue/DisplayURL';
import Network from '../../../UI/InfoRow/InfoValue/Network';

// todo: use value component for address, network, currency value
const AccountNetworkInfoExpanded = () => {
const { approvalRequest } = useApprovalRequest();
const networkName = useSelector(selectNetworkName);
const chainId = useSelector(selectChainId);
const { rpcUrl: networkRpcUrl, type: networkType } =
useSelector(selectProviderConfig);
const fromAddress = approvalRequest?.requestData?.from;
Expand All @@ -35,10 +37,10 @@ const AccountNetworkInfoExpanded = () => {
// todo: add tooltip content when available
tooltip={strings('confirm.network')}
>
{networkName}
<Network chainId={chainId} />
</InfoRow>
<InfoRow label={strings('confirm.rpc_url')}>
<InfoURL
<DisplayURL
url={networkRpcUrl ?? `https://${networkType}.infura.io/v3/`}
/>
</InfoRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,61 @@ exports[`AccountNetworkInfoExpanded should match snapshot for personal sign 1`]
/>
</View>
</View>
<Text
<View
style={
{
"color": "#141618",
"fontFamily": "EuclidCircularB-Regular",
"fontSize": 14,
"fontWeight": "400",
"marginTop": 8,
"alignItems": "center",
"display": "flex",
"flexDirection": "row",
}
}
>
Ethereum Main Network
</Text>
<View
style={
{
"alignItems": "center",
"backgroundColor": "#ffffff",
"borderRadius": 12,
"height": 24,
"justifyContent": "center",
"overflow": "hidden",
"width": 24,
}
}
>
<Image
onError={[Function]}
resizeMode="contain"
source={
{
"default": {
"uri": "MockImage",
},
}
}
style={
{
"height": 24,
"width": 24,
}
}
testID="network-avatar-image"
/>
</View>
<Text
style={
{
"color": "#141618",
"fontFamily": "EuclidCircularB-Regular",
"fontSize": 14,
"fontWeight": "400",
"marginLeft": 8,
}
}
>
Mainnet
</Text>
</View>
</View>
<View
style={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ exports[`AccountNetworkInfo should match snapshot for personal sign 1`] = `
}
}
>
Ethereum Main Network
Mainnet
</Text>
</View>
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { strings } from '../../../../../../../../locales/i18n';
import useApprovalRequest from '../../../../hooks/useApprovalRequest';
import InfoSection from '../../../UI/InfoRow/InfoSection';
import InfoRow from '../../../UI/InfoRow';
import InfoURL from '../../../UI/InfoRow/InfoValue/InfoURL';
import DisplayURL from '../../../UI/InfoRow/InfoValue/DisplayURL';
import Message from './Message';
import Simulation from './Simulation';

Expand All @@ -23,7 +23,7 @@ const PersonalSign = () => {
label={strings('confirm.request_from')}
tooltip={strings('confirm.personal_sign_tooltip')}
>
<InfoURL url={approvalRequest.origin} />
<DisplayURL url={approvalRequest.origin} />
</InfoRow>
</InfoSection>
<Message />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StyleProp, Text, TextStyle, View } from 'react-native';

import InfoRow from './InfoRow';
import InfoSection from './InfoSection';
import InfoURL from './InfoValue/InfoURL';
import DisplayURL from './InfoValue/DisplayURL';

const style = {
container: { padding: 8 },
Expand All @@ -30,10 +30,10 @@ storiesOf('Confirmations / InfoRow', module)
<Text style={style.title as StyleProp<TextStyle>}>URL</Text>
<InfoSection>
<InfoRow label="url-key">
<InfoURL url="https://google.com" />
<DisplayURL url="https://google.com" />
</InfoRow>
<InfoRow label="url-key">
<InfoURL url="http://google.com" />
<DisplayURL url="http://google.com" />
</InfoRow>
</InfoSection>
</View>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from 'react';
import { render } from '@testing-library/react-native';

import InfoURL from './InfoURL';
import DisplayURL from './DisplayURL';

describe('InfoURL', () => {
describe('DisplayURL', () => {
it('should display url as expected', async () => {
const container = render(<InfoURL url="https://google.com" />);
const container = render(<DisplayURL url="https://google.com" />);
expect(container).toMatchSnapshot();
});

it('should show warning if protocol is HTTP', async () => {
const { getByText } = render(<InfoURL url="http://google.com" />);
const { getByText } = render(<DisplayURL url="http://google.com" />);
expect(getByText('HTTP')).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import Icon, {
} from '../../../../../../../../component-library/components/Icons/Icon';
import Logger from '../../../../../../../../util/Logger';
import { useStyles } from '../../../../../../../../component-library/hooks';
import styleSheet from './InfoURL.styles';
import styleSheet from './DisplayURL.styles';

interface InfoURLProps {
interface DisplayURLProps {
url: string;
}

const InfoURL = ({ url }: InfoURLProps) => {
const DisplayURL = ({ url }: DisplayURLProps) => {
let urlObject;

try {
urlObject = new URL(url);
} catch (e) {
// eslint-disable-next-line no-console
Logger.error(e as Error, `InfoURL: new URL(url) cannot parse ${url}`);
Logger.error(e as Error, `DisplayURL: new URL(url) cannot parse ${url}`);
}

const isHTTP = urlObject?.protocol === 'http:';
Expand All @@ -47,4 +47,4 @@ const InfoURL = ({ url }: InfoURLProps) => {
);
};

export default InfoURL;
export default DisplayURL;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`InfoURL should display url as expected 1`] = `
exports[`DisplayURL should display url as expected 1`] = `
<View
style={
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './DisplayURL';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { StyleSheet } from 'react-native';

import { Theme } from '../../../../../../../../util/theme/models';
import { fontStyles } from '../../../../../../../../styles/common';

const styleSheet = (params: { theme: Theme }) => {
const { theme } = params;

return StyleSheet.create({
container: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
},
value: {
color: theme.colors.text.default,
...fontStyles.normal,
fontSize: 14,
marginLeft: 8,
},
});
};

export default styleSheet;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { CHAIN_IDS } from '@metamask/transaction-controller';
import React from 'react';

import renderWithProvider from '../../../../../../../../util/test/renderWithProvider';
import { backgroundState } from '../../../../../../../../util/test/initial-root-state';
import { mockNetworkState } from '../../../../../../../../util/test/network';
import Network from './Network';

const mockInitialState = {
engine: {
backgroundState: {
...backgroundState,
NetworkController: {
...mockNetworkState({
id: 'mainnet',
nickname: 'Ethereum Mainnet',
ticker: 'ETH',
chainId: CHAIN_IDS.MAINNET,
}),
},
},
},
};

describe('Network', () => {
it('should match snapshot', async () => {
const container = renderWithProvider(
<Network chainId={CHAIN_IDS.MAINNET} />,
{ state: mockInitialState },
);
expect(container).toMatchSnapshot();
jpuri marked this conversation as resolved.
Show resolved Hide resolved
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { Text, View } from 'react-native';

import Avatar, {
AvatarSize,
AvatarVariant,
} from '../../../../../../../../component-library/components/Avatars/Avatar';
import { useStyles } from '../../../../../../../../component-library/hooks';
import useNetworkInfo from '../../../../../hooks/useNetworkInfo';
import styleSheet from './Network.styles';

interface NetworkProps {
chainId?: string;
}

const Network = ({ chainId }: NetworkProps) => {
const { networkName, networkImage } = useNetworkInfo(chainId);
const { styles } = useStyles(styleSheet, {});

if (!chainId) {
return null;
}

return (
<View style={styles.container}>
<Avatar
variant={AvatarVariant.Network}
name={networkName}
imageSource={networkImage}
size={AvatarSize.Sm}
/>
<Text style={styles.value}>{networkName}</Text>
</View>
);
};

export default Network;
Loading
Loading