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,
|
2023-06-07 15:18:49 +02:00
|
|
|
BackgroundColor,
|
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,
|
2023-06-06 12:15:20 +02:00
|
|
|
FontWeight,
|
2023-06-09 14:22:31 +02:00
|
|
|
IconColor,
|
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-06-07 15:18:49 +02:00
|
|
|
import SnapAuthorshipHeader from '../../../../components/app/snaps/snap-authorship-header';
|
|
|
|
import {
|
|
|
|
AvatarIcon,
|
|
|
|
IconName,
|
|
|
|
ValidTag,
|
2023-07-20 21:49:35 +02:00
|
|
|
Text,
|
2023-06-07 15:18:49 +02:00
|
|
|
} from '../../../../components/component-library';
|
2023-03-17 12:00:05 +01:00
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
2023-04-25 16:32:51 +02:00
|
|
|
import SnapPermissionsList from '../../../../components/app/snaps/snap-permissions-list';
|
2023-06-07 15:18:49 +02:00
|
|
|
import { useScrollRequired } from '../../../../hooks/useScrollRequired';
|
2023-06-09 16:36:38 +02:00
|
|
|
import SiteOrigin from '../../../../components/ui/site-origin/site-origin';
|
|
|
|
import InstallError from '../../../../components/app/snaps/install-error/install-error';
|
|
|
|
import { useOriginMetadata } from '../../../../hooks/useOriginMetadata';
|
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();
|
2023-06-09 16:36:38 +02:00
|
|
|
const siteMetadata = useOriginMetadata(request?.metadata?.dappOrigin) || {};
|
|
|
|
const { origin, iconUrl, name } = siteMetadata;
|
2022-02-15 01:02:51 +01:00
|
|
|
const [isShowingWarning, setIsShowingWarning] = useState(false);
|
2023-06-07 15:18:49 +02:00
|
|
|
|
|
|
|
const { isScrollable, isScrolledToBottom, scrollToBottom, ref, onScroll } =
|
|
|
|
useScrollRequired([requestState]);
|
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-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-06-09 16:37:38 +02:00
|
|
|
const snapName = getSnapName(
|
|
|
|
targetSubjectMetadata.origin,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
);
|
2023-03-17 12:00:05 +01:00
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
if (!hasError && shouldShowWarning) {
|
|
|
|
setIsShowingWarning(true);
|
|
|
|
} else if (hasError) {
|
|
|
|
onCancel();
|
|
|
|
} else {
|
|
|
|
onSubmit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-09 16:36:38 +02:00
|
|
|
const getFooterMessage = () => {
|
|
|
|
if (hasError) {
|
|
|
|
return 'ok';
|
|
|
|
} else if (isLoading) {
|
|
|
|
return 'connect';
|
|
|
|
}
|
|
|
|
return 'install';
|
|
|
|
};
|
|
|
|
|
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}
|
|
|
|
>
|
2023-06-09 16:36:38 +02:00
|
|
|
{isLoading || hasError ? (
|
|
|
|
<Box
|
|
|
|
width="full"
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
paddingTop={4}
|
|
|
|
>
|
|
|
|
<SiteOrigin
|
|
|
|
chip
|
|
|
|
siteOrigin={origin}
|
|
|
|
title={origin}
|
|
|
|
iconSrc={iconUrl}
|
|
|
|
iconName={name}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
) : (
|
|
|
|
<SnapAuthorshipHeader snapId={targetSubjectMetadata.origin} />
|
|
|
|
)}
|
2022-02-15 01:02:51 +01:00
|
|
|
<Box
|
2023-06-07 15:18:49 +02:00
|
|
|
ref={ref}
|
|
|
|
onScroll={onScroll}
|
|
|
|
className="snap-install__content"
|
|
|
|
style={{
|
|
|
|
overflowY: 'auto',
|
2023-06-09 16:36:38 +02:00
|
|
|
flex: !isLoading && !hasError && '1',
|
2023-06-07 15:18:49 +02:00
|
|
|
}}
|
2022-02-15 01:02:51 +01:00
|
|
|
>
|
2023-03-17 12:00:05 +01:00
|
|
|
{isLoading && (
|
|
|
|
<Box
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-install__content__loader-container"
|
2023-03-17 12:00:05 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
|
|
|
>
|
|
|
|
<PulseLoader />
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{hasError && (
|
2023-06-09 16:36:38 +02:00
|
|
|
<InstallError
|
|
|
|
iconName={IconName.Warning}
|
|
|
|
title={t('connectionFailed')}
|
|
|
|
description={t('connectionFailedDescription', [
|
|
|
|
<Text as={ValidTag.Span} key="1" fontWeight={FontWeight.Medium}>
|
|
|
|
{snapName}
|
|
|
|
</Text>,
|
|
|
|
])}
|
|
|
|
error={requestState.error}
|
|
|
|
/>
|
2023-03-17 12:00:05 +01:00
|
|
|
)}
|
2023-06-07 15:18:49 +02:00
|
|
|
{!hasError && !isLoading && (
|
2022-09-20 14:46:25 +02:00
|
|
|
<>
|
2023-06-07 15:18:49 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.headingLg}
|
|
|
|
paddingTop={4}
|
|
|
|
paddingBottom={2}
|
|
|
|
textAlign="center"
|
|
|
|
>
|
|
|
|
{t('snapInstall')}
|
|
|
|
</Text>
|
2023-03-17 12:00:05 +01:00
|
|
|
<Text
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-install__content__permission-description"
|
2023-03-17 12:00:05 +01:00
|
|
|
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-06-07 15:18:49 +02:00
|
|
|
{t('snapInstallRequest', [
|
2023-06-06 12:15:20 +02:00
|
|
|
<Text
|
|
|
|
as={ValidTag.Span}
|
|
|
|
key="2"
|
|
|
|
variant={TextVariant.bodyMd}
|
|
|
|
fontWeight={FontWeight.Medium}
|
|
|
|
>
|
|
|
|
{snapName}
|
|
|
|
</Text>,
|
2023-03-17 12:00:05 +01:00
|
|
|
])}
|
|
|
|
</Text>
|
2023-04-14 12:04:23 +02:00
|
|
|
<SnapPermissionsList
|
2023-07-06 22:54:27 +02:00
|
|
|
snapId={targetSubjectMetadata.origin}
|
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-06-07 15:18:49 +02:00
|
|
|
{isScrollable && !isScrolledToBottom ? (
|
|
|
|
<AvatarIcon
|
|
|
|
className="snap-install__scroll-button"
|
|
|
|
data-testid="snap-install-scroll"
|
|
|
|
iconName={IconName.Arrow2Down}
|
|
|
|
backgroundColor={BackgroundColor.infoDefault}
|
2023-06-09 14:22:31 +02:00
|
|
|
color={IconColor.primaryInverse}
|
2023-06-07 15:18:49 +02:00
|
|
|
onClick={scrollToBottom}
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-09-20 14:46:25 +02:00
|
|
|
</>
|
|
|
|
)}
|
2022-02-15 01:02:51 +01:00
|
|
|
</Box>
|
|
|
|
<Box
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-install__footer"
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-02-15 01:02:51 +01:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
2023-06-07 15:18:49 +02:00
|
|
|
style={{
|
|
|
|
boxShadow: 'var(--shadow-size-lg) var(--color-shadow-default)',
|
|
|
|
}}
|
2022-02-15 01:02:51 +01:00
|
|
|
>
|
|
|
|
<PageContainerFooter
|
|
|
|
cancelButtonType="default"
|
2023-03-17 12:00:05 +01:00
|
|
|
hideCancel={hasError}
|
2023-06-07 15:18:49 +02:00
|
|
|
disabled={
|
|
|
|
isLoading || (!hasError && isScrollable && !isScrolledToBottom)
|
|
|
|
}
|
2022-02-15 01:02:51 +01:00
|
|
|
onCancel={onCancel}
|
|
|
|
cancelText={t('cancel')}
|
2023-03-17 12:00:05 +01:00
|
|
|
onSubmit={handleSubmit}
|
2023-06-09 16:36:38 +02:00
|
|
|
submitText={t(getFooterMessage())}
|
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}
|
2023-06-08 12:32:47 +02:00
|
|
|
snapName={snapName}
|
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,
|
|
|
|
};
|