2022-02-15 01:02:51 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2022-08-02 11:29:40 +02:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2022-02-15 01:02:51 +01:00
|
|
|
import { PageContainerFooter } from '../../../../components/ui/page-container';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2023-04-25 16:32:51 +02:00
|
|
|
import SnapInstallWarning from '../../../../components/app/snaps/snap-install-warning';
|
2022-02-15 01:02:51 +01:00
|
|
|
import Box from '../../../../components/ui/box/box';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2022-02-15 01:02:51 +01:00
|
|
|
BLOCK_SIZES,
|
2023-02-02 21:15:26 +01:00
|
|
|
BorderStyle,
|
2022-02-15 01:02:51 +01:00
|
|
|
FLEX_DIRECTION,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2023-03-17 12:00:05 +01:00
|
|
|
TextVariant,
|
|
|
|
TEXT_ALIGN,
|
2022-02-15 01:02:51 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
2022-09-23 12:39:54 +02:00
|
|
|
import { getSnapInstallWarnings } from '../util';
|
2023-03-17 12:00:05 +01:00
|
|
|
import PulseLoader from '../../../../components/ui/pulse-loader/pulse-loader';
|
2023-04-25 16:32:51 +02:00
|
|
|
import InstallError from '../../../../components/app/snaps/install-error/install-error';
|
|
|
|
import SnapAuthorship from '../../../../components/app/snaps/snap-authorship';
|
2023-03-17 12:00:05 +01:00
|
|
|
import { Text } from '../../../../components/component-library';
|
|
|
|
import { useOriginMetadata } from '../../../../hooks/useOriginMetadata';
|
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
2023-04-25 16:32:51 +02:00
|
|
|
import SnapPermissionsList from '../../../../components/app/snaps/snap-permissions-list';
|
2022-02-15 01:02:51 +01:00
|
|
|
|
|
|
|
export default function SnapInstall({
|
|
|
|
request,
|
2023-03-17 12:00:05 +01:00
|
|
|
requestState,
|
2022-02-15 01:02:51 +01:00
|
|
|
approveSnapInstall,
|
|
|
|
rejectSnapInstall,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) {
|
|
|
|
const t = useI18nContext();
|
|
|
|
|
|
|
|
const [isShowingWarning, setIsShowingWarning] = useState(false);
|
2023-03-17 12:00:05 +01:00
|
|
|
const originMetadata = useOriginMetadata(request.metadata?.dappOrigin) || {};
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const onCancel = useCallback(
|
|
|
|
() => rejectSnapInstall(request.metadata.id),
|
|
|
|
[request, rejectSnapInstall],
|
|
|
|
);
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const onSubmit = useCallback(
|
2022-09-20 14:46:25 +02:00
|
|
|
() => approveSnapInstall(request.metadata.id),
|
2022-07-31 20:26:40 +02:00
|
|
|
[request, approveSnapInstall],
|
|
|
|
);
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2023-03-17 12:00:05 +01:00
|
|
|
const hasError = !requestState.loading && requestState.error;
|
|
|
|
|
|
|
|
const isLoading = requestState.loading;
|
|
|
|
|
2022-09-20 14:46:25 +02:00
|
|
|
const hasPermissions =
|
2023-03-17 12:00:05 +01:00
|
|
|
!hasError &&
|
|
|
|
requestState?.permissions &&
|
|
|
|
Object.keys(requestState.permissions).length > 0;
|
|
|
|
|
|
|
|
const isEmpty = !isLoading && !hasError && !hasPermissions;
|
2022-09-20 14:46:25 +02:00
|
|
|
|
2022-09-23 12:39:54 +02:00
|
|
|
const warnings = getSnapInstallWarnings(
|
2023-03-17 12:00:05 +01:00
|
|
|
requestState?.permissions ?? {},
|
2022-09-23 12:39:54 +02:00
|
|
|
targetSubjectMetadata,
|
|
|
|
t,
|
|
|
|
);
|
2022-08-02 11:29:40 +02:00
|
|
|
|
2022-09-23 12:39:54 +02:00
|
|
|
const shouldShowWarning = warnings.length > 0;
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2023-03-17 12:00:05 +01:00
|
|
|
const snapName = getSnapName(targetSubjectMetadata.origin);
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
if (!hasError && shouldShowWarning) {
|
|
|
|
setIsShowingWarning(true);
|
|
|
|
} else if (hasError) {
|
|
|
|
onCancel();
|
|
|
|
} else {
|
|
|
|
onSubmit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
className="page-container snap-install"
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
2022-02-15 01:02:51 +01:00
|
|
|
height={BLOCK_SIZES.FULL}
|
2023-02-02 21:15:26 +01:00
|
|
|
borderStyle={BorderStyle.none}
|
2022-02-15 01:02:51 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
>
|
|
|
|
<Box
|
|
|
|
className="headers"
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-02-15 01:02:51 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
>
|
2023-04-14 12:04:23 +02:00
|
|
|
<Box paddingLeft={4} paddingRight={4}>
|
|
|
|
<SnapAuthorship snapId={targetSubjectMetadata.origin} />
|
|
|
|
</Box>
|
2023-03-17 12:00:05 +01:00
|
|
|
{!hasError && (
|
2023-04-14 12:04:23 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.headingLg}
|
|
|
|
paddingTop={4}
|
|
|
|
paddingBottom={2}
|
|
|
|
>
|
2023-03-17 12:00:05 +01:00
|
|
|
{t('snapInstall')}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
{isLoading && (
|
|
|
|
<Box
|
|
|
|
className="loader-container"
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
>
|
|
|
|
<PulseLoader />
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{hasError && (
|
|
|
|
<InstallError error={requestState.error} title={t('requestFailed')} />
|
|
|
|
)}
|
2022-09-20 14:46:25 +02:00
|
|
|
{hasPermissions && (
|
|
|
|
<>
|
2023-03-17 12:00:05 +01:00
|
|
|
<Text
|
|
|
|
className="headers__permission-description"
|
|
|
|
paddingBottom={4}
|
2023-04-14 12:04:23 +02:00
|
|
|
paddingLeft={4}
|
|
|
|
paddingRight={4}
|
2023-03-17 12:00:05 +01:00
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
2022-09-20 14:46:25 +02:00
|
|
|
>
|
2023-03-17 12:00:05 +01:00
|
|
|
{t('snapInstallRequestsPermission', [
|
|
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
|
|
<b key="2">{snapName}</b>,
|
|
|
|
])}
|
|
|
|
</Text>
|
2023-04-14 12:04:23 +02:00
|
|
|
<SnapPermissionsList
|
2023-03-17 12:00:05 +01:00
|
|
|
permissions={requestState.permissions || {}}
|
2023-04-05 15:34:33 +02:00
|
|
|
targetSubjectMetadata={targetSubjectMetadata}
|
2022-09-20 14:46:25 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2023-03-17 12:00:05 +01:00
|
|
|
{isEmpty && (
|
|
|
|
<Box
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
height={BLOCK_SIZES.FULL}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
>
|
2023-04-03 18:04:30 +02:00
|
|
|
<Text textAlign={TEXT_ALIGN.CENTER}>
|
2023-03-17 12:00:05 +01:00
|
|
|
{t('snapInstallRequest', [
|
|
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
|
|
<b key="2">{snapName}</b>,
|
|
|
|
])}
|
|
|
|
</Text>
|
|
|
|
</Box>
|
|
|
|
)}
|
2022-02-15 01:02:51 +01:00
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
className="footers"
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-02-15 01:02:51 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
>
|
|
|
|
<PageContainerFooter
|
|
|
|
cancelButtonType="default"
|
2023-03-17 12:00:05 +01:00
|
|
|
hideCancel={hasError}
|
|
|
|
disabled={isLoading}
|
2022-02-15 01:02:51 +01:00
|
|
|
onCancel={onCancel}
|
|
|
|
cancelText={t('cancel')}
|
2023-03-17 12:00:05 +01:00
|
|
|
onSubmit={handleSubmit}
|
|
|
|
submitText={t(
|
|
|
|
// eslint-disable-next-line no-nested-ternary
|
|
|
|
hasError ? 'ok' : hasPermissions ? 'approveAndInstall' : 'install',
|
|
|
|
)}
|
2022-02-15 01:02:51 +01:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
{isShowingWarning && (
|
|
|
|
<SnapInstallWarning
|
|
|
|
onCancel={() => setIsShowingWarning(false)}
|
|
|
|
onSubmit={onSubmit}
|
2022-09-23 12:39:54 +02:00
|
|
|
warnings={warnings}
|
2022-02-15 01:02:51 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SnapInstall.propTypes = {
|
|
|
|
request: PropTypes.object.isRequired,
|
2023-03-17 12:00:05 +01:00
|
|
|
requestState: PropTypes.object.isRequired,
|
2022-02-15 01:02:51 +01:00
|
|
|
approveSnapInstall: PropTypes.func.isRequired,
|
|
|
|
rejectSnapInstall: PropTypes.func.isRequired,
|
|
|
|
targetSubjectMetadata: PropTypes.shape({
|
|
|
|
iconUrl: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
origin: PropTypes.string.isRequired,
|
|
|
|
sourceCode: PropTypes.string,
|
2022-03-14 18:40:21 +01:00
|
|
|
version: PropTypes.string,
|
2022-02-15 01:02:51 +01:00
|
|
|
}).isRequired,
|
|
|
|
};
|