mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-02 22:24:27 +01:00
a9429c5c0d
* renamed classnames according to BEM, removed unnecessary scss file * remove plurality in classnames
196 lines
5.9 KiB
JavaScript
196 lines
5.9 KiB
JavaScript
import PropTypes from 'prop-types';
|
|
import React, { useCallback, useState } from 'react';
|
|
import { PageContainerFooter } from '../../../../components/ui/page-container';
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
|
import SnapInstallWarning from '../../../../components/app/snaps/snap-install-warning';
|
|
import Box from '../../../../components/ui/box/box';
|
|
import {
|
|
AlignItems,
|
|
BLOCK_SIZES,
|
|
BorderStyle,
|
|
FLEX_DIRECTION,
|
|
JustifyContent,
|
|
TextVariant,
|
|
TEXT_ALIGN,
|
|
} from '../../../../helpers/constants/design-system';
|
|
import { getSnapInstallWarnings } from '../util';
|
|
import PulseLoader from '../../../../components/ui/pulse-loader/pulse-loader';
|
|
import InstallError from '../../../../components/app/snaps/install-error/install-error';
|
|
import SnapAuthorship from '../../../../components/app/snaps/snap-authorship';
|
|
import { Text } from '../../../../components/component-library';
|
|
import { useOriginMetadata } from '../../../../hooks/useOriginMetadata';
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
|
import SnapPermissionsList from '../../../../components/app/snaps/snap-permissions-list';
|
|
|
|
export default function SnapInstall({
|
|
request,
|
|
requestState,
|
|
approveSnapInstall,
|
|
rejectSnapInstall,
|
|
targetSubjectMetadata,
|
|
}) {
|
|
const t = useI18nContext();
|
|
|
|
const [isShowingWarning, setIsShowingWarning] = useState(false);
|
|
const originMetadata = useOriginMetadata(request.metadata?.dappOrigin) || {};
|
|
|
|
const onCancel = useCallback(
|
|
() => rejectSnapInstall(request.metadata.id),
|
|
[request, rejectSnapInstall],
|
|
);
|
|
|
|
const onSubmit = useCallback(
|
|
() => approveSnapInstall(request.metadata.id),
|
|
[request, approveSnapInstall],
|
|
);
|
|
|
|
const hasError = !requestState.loading && requestState.error;
|
|
|
|
const isLoading = requestState.loading;
|
|
|
|
const hasPermissions =
|
|
!hasError &&
|
|
requestState?.permissions &&
|
|
Object.keys(requestState.permissions).length > 0;
|
|
|
|
const isEmpty = !isLoading && !hasError && !hasPermissions;
|
|
|
|
const warnings = getSnapInstallWarnings(
|
|
requestState?.permissions ?? {},
|
|
targetSubjectMetadata,
|
|
t,
|
|
);
|
|
|
|
const shouldShowWarning = warnings.length > 0;
|
|
|
|
const snapName = getSnapName(targetSubjectMetadata.origin);
|
|
|
|
const handleSubmit = () => {
|
|
if (!hasError && shouldShowWarning) {
|
|
setIsShowingWarning(true);
|
|
} else if (hasError) {
|
|
onCancel();
|
|
} else {
|
|
onSubmit();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Box
|
|
className="page-container snap-install"
|
|
justifyContent={JustifyContent.spaceBetween}
|
|
height={BLOCK_SIZES.FULL}
|
|
borderStyle={BorderStyle.none}
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
>
|
|
<Box
|
|
className="snap-install__header"
|
|
alignItems={AlignItems.center}
|
|
paddingLeft={4}
|
|
paddingRight={4}
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
>
|
|
<SnapAuthorship snapId={targetSubjectMetadata.origin} />
|
|
{!isLoading && !hasError && (
|
|
<Text
|
|
variant={TextVariant.headingLg}
|
|
paddingTop={4}
|
|
paddingBottom={2}
|
|
>
|
|
{t('snapInstall')}
|
|
</Text>
|
|
)}
|
|
</Box>
|
|
<Box className="snap-install__content">
|
|
{isLoading && (
|
|
<Box
|
|
className="snap-install__content__loader-container"
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
alignItems={AlignItems.center}
|
|
justifyContent={JustifyContent.center}
|
|
>
|
|
<PulseLoader />
|
|
</Box>
|
|
)}
|
|
{hasError && (
|
|
<InstallError error={requestState.error} title={t('requestFailed')} />
|
|
)}
|
|
{hasPermissions && (
|
|
<>
|
|
<Text
|
|
className="snap-install__content__permission-description"
|
|
paddingBottom={4}
|
|
paddingLeft={4}
|
|
paddingRight={4}
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
>
|
|
{t('snapInstallRequestsPermission', [
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
<b key="2">{snapName}</b>,
|
|
])}
|
|
</Text>
|
|
<SnapPermissionsList
|
|
permissions={requestState.permissions || {}}
|
|
targetSubjectMetadata={targetSubjectMetadata}
|
|
/>
|
|
</>
|
|
)}
|
|
{isEmpty && (
|
|
<Box
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
height={BLOCK_SIZES.FULL}
|
|
alignItems={AlignItems.center}
|
|
justifyContent={JustifyContent.center}
|
|
>
|
|
<Text textAlign={TEXT_ALIGN.CENTER}>
|
|
{t('snapInstallRequest', [
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
<b key="2">{snapName}</b>,
|
|
])}
|
|
</Text>
|
|
</Box>
|
|
)}
|
|
</Box>
|
|
<Box
|
|
className="snap-install__footer"
|
|
alignItems={AlignItems.center}
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
>
|
|
<PageContainerFooter
|
|
cancelButtonType="default"
|
|
hideCancel={hasError}
|
|
disabled={isLoading}
|
|
onCancel={onCancel}
|
|
cancelText={t('cancel')}
|
|
onSubmit={handleSubmit}
|
|
submitText={t(
|
|
// eslint-disable-next-line no-nested-ternary
|
|
hasError ? 'ok' : hasPermissions ? 'approveAndInstall' : 'install',
|
|
)}
|
|
/>
|
|
</Box>
|
|
{isShowingWarning && (
|
|
<SnapInstallWarning
|
|
onCancel={() => setIsShowingWarning(false)}
|
|
onSubmit={onSubmit}
|
|
warnings={warnings}
|
|
/>
|
|
)}
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
SnapInstall.propTypes = {
|
|
request: PropTypes.object.isRequired,
|
|
requestState: 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,
|
|
};
|