1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/pages/permissions-connect/flask/snap-install/snap-install.js
Hassan Malik a7179a6b88
[FLASK] Add update snap UI (#15143)
* added snap-update folder

* addded update route, snap update component, updated permissions connect components

* added actions and selectors

* updated permissions selectors and updated permissions connect container to have update snap logic

* updated translations, added selector, updated request object

* updated translations, added update snap permission list component

* more fixes

* added CSS, redid some HTML

* lint fixes

* Add missing grantPermissions action

* updated button padding

* fixes

* removed prop type

* fix Update & Install wrapping

* made changes for forthcoming snap controller PR

* removed ununsed imports

* updated css

* re-added padding rule and removed unused translation messages

* addressed comments

* add subtext for new permissions

* lint fix

* removed unused translations

* some more changes

* fix e2e tests

* lint fix

* added in delay for e2e tests

* Revert "added in delay for e2e tests"

This reverts commit 095962a2c0c9de0b0b343d3134bb0787044dd8ce.

* fixed routing logic

Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Guillaume Roux <guillaumeroux123@gmail.com>
2022-08-03 12:02:44 -04:00

139 lines
4.4 KiB
JavaScript

import PropTypes from 'prop-types';
import React, { useCallback, useState } from 'react';
import { PageContainerFooter } from '../../../../components/ui/page-container';
import PermissionsConnectPermissionList from '../../../../components/app/permissions-connect-permission-list';
import PermissionsConnectFooter from '../../../../components/app/permissions-connect-footer';
import PermissionConnectHeader from '../../../../components/app/permissions-connect-header';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import SnapInstallWarning from '../../../../components/app/flask/snap-install-warning';
import Box from '../../../../components/ui/box/box';
import {
ALIGN_ITEMS,
BLOCK_SIZES,
BORDER_STYLE,
FLEX_DIRECTION,
JUSTIFY_CONTENT,
TYPOGRAPHY,
} from '../../../../helpers/constants/design-system';
import Typography from '../../../../components/ui/typography';
import { coinTypeToProtocolName } from '../../../../helpers/utils/util';
export default function SnapInstall({
request,
approveSnapInstall,
rejectSnapInstall,
targetSubjectMetadata,
}) {
const t = useI18nContext();
const [isShowingWarning, setIsShowingWarning] = useState(false);
const onCancel = useCallback(
() => rejectSnapInstall(request.metadata.id),
[request, rejectSnapInstall],
);
const onSubmit = useCallback(
() => approveSnapInstall(request),
[request, approveSnapInstall],
);
const bip44EntropyPermissions =
request.permissions &&
Object.keys(request.permissions).filter((v) =>
v.startsWith('snap_getBip44Entropy_'),
);
const shouldShowWarning = bip44EntropyPermissions?.length > 0;
const getCoinType = (bip44EntropyPermission) =>
bip44EntropyPermission?.split('_').slice(-1);
return (
<Box
className="page-container snap-install"
justifyContent={JUSTIFY_CONTENT.SPACE_BETWEEN}
height={BLOCK_SIZES.FULL}
borderStyle={BORDER_STYLE.NONE}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<Box
className="headers"
alignItems={ALIGN_ITEMS.CENTER}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<PermissionConnectHeader
icon={targetSubjectMetadata.iconUrl}
iconName={targetSubjectMetadata.name}
headerTitle={t('snapInstall')}
headerText={null} // TODO(ritave): Add header text when snaps support description
siteOrigin={targetSubjectMetadata.origin}
isSnapInstallOrUpdate
snapVersion={targetSubjectMetadata.version}
boxProps={{ alignItems: ALIGN_ITEMS.CENTER }}
/>
<Typography
boxProps={{
padding: [4, 4, 0, 4],
}}
variant={TYPOGRAPHY.H7}
tag="span"
>
{t('snapRequestsPermission')}
</Typography>
<PermissionsConnectPermissionList
permissions={request.permissions || {}}
/>
</Box>
<Box
className="footers"
alignItems={ALIGN_ITEMS.CENTER}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<Box className="snap-install__footer--no-source-code" paddingTop={4}>
<PermissionsConnectFooter />
</Box>
<PageContainerFooter
cancelButtonType="default"
onCancel={onCancel}
cancelText={t('cancel')}
onSubmit={
shouldShowWarning ? () => setIsShowingWarning(true) : onSubmit
}
submitText={t('approveAndInstall')}
/>
</Box>
{isShowingWarning && (
<SnapInstallWarning
onCancel={() => setIsShowingWarning(false)}
onSubmit={onSubmit}
warnings={bip44EntropyPermissions.map((permission, i) => {
const coinType = getCoinType(permission);
return {
id: `key-access-${i}`,
message: t('snapInstallWarningKeyAccess', [
targetSubjectMetadata.name,
coinTypeToProtocolName(coinType) ||
t('unrecognizedProtocol', [coinType]),
]),
};
})}
/>
)}
</Box>
);
}
SnapInstall.propTypes = {
request: PropTypes.object.isRequired,
approveSnapInstall: PropTypes.func.isRequired,
rejectSnapInstall: PropTypes.func.isRequired,
targetSubjectMetadata: PropTypes.shape({
iconUrl: PropTypes.string,
name: PropTypes.string,
origin: PropTypes.string.isRequired,
sourceCode: PropTypes.string,
version: PropTypes.string,
}).isRequired,
};