1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 15:50:28 +01:00
metamask-extension/ui/pages/permissions-connect/flask/snap-install/snap-install.js
2023-02-02 20:15:26 +00:00

133 lines
4.1 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 {
AlignItems,
BLOCK_SIZES,
BorderStyle,
FLEX_DIRECTION,
JustifyContent,
TypographyVariant,
} from '../../../../helpers/constants/design-system';
import Typography from '../../../../components/ui/typography';
import { getSnapInstallWarnings } from '../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.metadata.id),
[request, approveSnapInstall],
);
const hasPermissions =
request?.permissions && Object.keys(request.permissions).length > 0;
const warnings = getSnapInstallWarnings(
request?.permissions ?? {},
targetSubjectMetadata,
t,
);
const shouldShowWarning = warnings.length > 0;
return (
<Box
className="page-container snap-install"
justifyContent={JustifyContent.spaceBetween}
height={BLOCK_SIZES.FULL}
borderStyle={BorderStyle.none}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<Box
className="headers"
alignItems={AlignItems.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: AlignItems.center }}
/>
{hasPermissions && (
<>
<Typography
boxProps={{
padding: [4, 4, 0, 4],
}}
variant={TypographyVariant.H7}
as="span"
>
{t('snapRequestsPermission')}
</Typography>
<PermissionsConnectPermissionList
permissions={request.permissions || {}}
/>
</>
)}
</Box>
<Box
className="footers"
alignItems={AlignItems.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(hasPermissions ? 'approveAndInstall' : 'install')}
/>
</Box>
{isShowingWarning && (
<SnapInstallWarning
onCancel={() => setIsShowingWarning(false)}
onSubmit={onSubmit}
warnings={warnings}
/>
)}
</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,
};