2022-08-03 18:02:44 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2022-09-23 12:39:54 +02:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2022-08-03 18:02:44 +02: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-08-03 18:02:44 +02: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-08-03 18:02:44 +02:00
|
|
|
BLOCK_SIZES,
|
2023-02-02 21:15:26 +01:00
|
|
|
BorderStyle,
|
2022-08-03 18:02:44 +02:00
|
|
|
FLEX_DIRECTION,
|
2023-06-07 15:18:49 +02:00
|
|
|
FontWeight,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2023-03-17 12:00:05 +01:00
|
|
|
TextVariant,
|
|
|
|
TEXT_ALIGN,
|
2023-06-09 14:22:31 +02:00
|
|
|
IconColor,
|
2022-08-03 18:02:44 +02:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
2023-03-17 12:00:05 +01:00
|
|
|
|
2023-04-25 16:32:51 +02:00
|
|
|
import UpdateSnapPermissionList from '../../../../components/app/snaps/update-snap-permission-list';
|
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';
|
2023-06-07 15:18:49 +02:00
|
|
|
import SnapAuthorshipHeader from '../../../../components/app/snaps/snap-authorship-header';
|
|
|
|
import {
|
|
|
|
AvatarIcon,
|
|
|
|
IconName,
|
|
|
|
Text,
|
|
|
|
ValidTag,
|
|
|
|
} from '../../../../components/component-library';
|
2023-03-17 12:00:05 +01:00
|
|
|
import { useOriginMetadata } from '../../../../hooks/useOriginMetadata';
|
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
2023-06-07 15:18:49 +02:00
|
|
|
import { useScrollRequired } from '../../../../hooks/useScrollRequired';
|
2022-08-03 18:02:44 +02:00
|
|
|
|
|
|
|
export default function SnapUpdate({
|
|
|
|
request,
|
2023-03-17 12:00:05 +01:00
|
|
|
requestState,
|
2022-08-03 18:02:44 +02:00
|
|
|
approveSnapUpdate,
|
|
|
|
rejectSnapUpdate,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
}) {
|
|
|
|
const t = useI18nContext();
|
|
|
|
|
|
|
|
const [isShowingWarning, setIsShowingWarning] = useState(false);
|
2023-03-17 12:00:05 +01:00
|
|
|
const originMetadata = useOriginMetadata(request.metadata?.dappOrigin) || {};
|
2022-08-03 18:02:44 +02:00
|
|
|
|
2023-06-07 15:18:49 +02:00
|
|
|
const { isScrollable, isScrolledToBottom, scrollToBottom, ref, onScroll } =
|
|
|
|
useScrollRequired([requestState]);
|
|
|
|
|
2022-08-03 18:02:44 +02:00
|
|
|
const onCancel = useCallback(
|
|
|
|
() => rejectSnapUpdate(request.metadata.id),
|
|
|
|
[request, rejectSnapUpdate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onSubmit = useCallback(
|
2022-09-20 14:46:25 +02:00
|
|
|
() => approveSnapUpdate(request.metadata.id),
|
2022-08-03 18:02:44 +02:00
|
|
|
[request, approveSnapUpdate],
|
|
|
|
);
|
|
|
|
|
2023-03-17 12:00:05 +01:00
|
|
|
const approvedPermissions = requestState.approvedPermissions ?? {};
|
|
|
|
const revokedPermissions = requestState.unusedPermissions ?? {};
|
|
|
|
const newPermissions = requestState.newPermissions ?? {};
|
2023-06-05 14:31:11 +02:00
|
|
|
const { newVersion } = requestState;
|
2023-03-17 12:00:05 +01:00
|
|
|
|
|
|
|
const isLoading = requestState.loading;
|
|
|
|
const hasError = !isLoading && requestState.error;
|
|
|
|
|
2022-09-23 12:39:54 +02:00
|
|
|
const warnings = getSnapInstallWarnings(
|
|
|
|
newPermissions,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
t,
|
|
|
|
);
|
|
|
|
|
|
|
|
const shouldShowWarning = warnings.length > 0;
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-08-03 18:02:44 +02:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
className="page-container snap-update"
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
2022-08-03 18:02:44 +02:00
|
|
|
height={BLOCK_SIZES.FULL}
|
2023-02-02 21:15:26 +01:00
|
|
|
borderStyle={BorderStyle.none}
|
2022-08-03 18:02:44 +02:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
>
|
2023-06-07 15:18:49 +02:00
|
|
|
<SnapAuthorshipHeader snapId={targetSubjectMetadata.origin} />
|
2022-08-03 18:02:44 +02:00
|
|
|
<Box
|
2023-06-07 15:18:49 +02:00
|
|
|
ref={ref}
|
|
|
|
onScroll={onScroll}
|
|
|
|
className="snap-update__content"
|
|
|
|
style={{
|
|
|
|
overflowY: 'auto',
|
|
|
|
flex: !isLoading && '1',
|
|
|
|
}}
|
2022-08-03 18:02:44 +02:00
|
|
|
>
|
2023-05-05 10:00:14 +02:00
|
|
|
{!isLoading && !hasError && (
|
2023-04-03 18:04:30 +02:00
|
|
|
<Text
|
|
|
|
paddingBottom={4}
|
|
|
|
paddingTop={4}
|
|
|
|
variant={TextVariant.headingLg}
|
2023-06-07 15:18:49 +02:00
|
|
|
textAlign="center"
|
2023-04-03 18:04:30 +02:00
|
|
|
>
|
2023-03-17 12:00:05 +01:00
|
|
|
{t('snapUpdate')}
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
{isLoading && (
|
|
|
|
<Box
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-update__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:50:51 +02:00
|
|
|
<InstallError
|
|
|
|
iconName={IconName.Warning}
|
|
|
|
error={requestState.error}
|
|
|
|
title={t('snapUpdateErrorTitle')}
|
|
|
|
description={t('snapUpdateErrorDescription', [
|
|
|
|
<Text as={ValidTag.Span} key="1" fontWeight={FontWeight.Medium}>
|
|
|
|
{snapName}
|
|
|
|
</Text>,
|
|
|
|
])}
|
|
|
|
/>
|
2023-03-17 12:00:05 +01:00
|
|
|
)}
|
2023-06-05 14:31:11 +02:00
|
|
|
{!hasError && !isLoading && (
|
2022-09-20 14:46:25 +02:00
|
|
|
<>
|
2023-03-17 12:00:05 +01:00
|
|
|
<Text
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-update__content__permission-description"
|
2023-03-17 12:00:05 +01:00
|
|
|
paddingBottom={4}
|
2023-04-25 19:20:37 +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-05 14:31:11 +02:00
|
|
|
{t('snapUpdateRequest', [
|
2023-06-07 15:18:49 +02:00
|
|
|
<Text
|
|
|
|
as={ValidTag.Span}
|
|
|
|
key="1"
|
|
|
|
variant={TextVariant.bodyMd}
|
|
|
|
fontWeight={FontWeight.Medium}
|
|
|
|
>
|
|
|
|
{originMetadata?.hostname}
|
|
|
|
</Text>,
|
|
|
|
<Text
|
|
|
|
as={ValidTag.Span}
|
|
|
|
key="2"
|
|
|
|
variant={TextVariant.bodyMd}
|
|
|
|
fontWeight={FontWeight.Medium}
|
|
|
|
>
|
|
|
|
{snapName}
|
|
|
|
</Text>,
|
|
|
|
<Text
|
|
|
|
as={ValidTag.Span}
|
|
|
|
key="3"
|
|
|
|
variant={TextVariant.bodyMd}
|
|
|
|
fontWeight={FontWeight.Medium}
|
|
|
|
>
|
|
|
|
{newVersion}
|
|
|
|
</Text>,
|
2023-03-17 12:00:05 +01:00
|
|
|
])}
|
|
|
|
</Text>
|
2022-09-20 14:46:25 +02:00
|
|
|
<UpdateSnapPermissionList
|
|
|
|
approvedPermissions={approvedPermissions}
|
|
|
|
revokedPermissions={revokedPermissions}
|
|
|
|
newPermissions={newPermissions}
|
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-update-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-08-03 18:02:44 +02:00
|
|
|
</Box>
|
|
|
|
<Box
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-update__footer"
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-08-03 18:02:44 +02:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
2023-06-07 15:18:49 +02:00
|
|
|
style={{
|
|
|
|
boxShadow: 'var(--shadow-size-lg) var(--color-shadow-default)',
|
|
|
|
}}
|
2022-08-03 18:02:44 +02: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-08-03 18:02:44 +02:00
|
|
|
onCancel={onCancel}
|
|
|
|
cancelText={t('cancel')}
|
2023-03-17 12:00:05 +01:00
|
|
|
onSubmit={handleSubmit}
|
2023-06-05 14:31:11 +02:00
|
|
|
submitText={t(hasError ? 'ok' : 'update')}
|
2022-08-03 18:02:44 +02:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
{isShowingWarning && (
|
|
|
|
<SnapInstallWarning
|
|
|
|
onCancel={() => setIsShowingWarning(false)}
|
|
|
|
onSubmit={onSubmit}
|
2023-06-08 12:32:47 +02:00
|
|
|
snapName={snapName}
|
2022-09-23 12:39:54 +02:00
|
|
|
warnings={warnings}
|
2022-08-03 18:02:44 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
SnapUpdate.propTypes = {
|
|
|
|
request: PropTypes.object.isRequired,
|
2023-03-17 12:00:05 +01:00
|
|
|
requestState: PropTypes.object.isRequired,
|
2022-08-03 18:02:44 +02:00
|
|
|
approveSnapUpdate: PropTypes.func.isRequired,
|
|
|
|
rejectSnapUpdate: PropTypes.func.isRequired,
|
|
|
|
targetSubjectMetadata: PropTypes.shape({
|
|
|
|
iconUrl: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
origin: PropTypes.string.isRequired,
|
|
|
|
sourceCode: PropTypes.string,
|
|
|
|
version: PropTypes.string,
|
|
|
|
}).isRequired,
|
|
|
|
};
|