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,
|
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-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2023-03-17 12:00:05 +01:00
|
|
|
TextVariant,
|
|
|
|
TEXT_ALIGN,
|
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';
|
|
|
|
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';
|
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
|
|
|
|
|
|
|
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 ?? {};
|
|
|
|
|
|
|
|
const isLoading = requestState.loading;
|
|
|
|
const hasError = !isLoading && requestState.error;
|
|
|
|
|
2022-09-20 14:46:25 +02:00
|
|
|
const hasPermissions =
|
2023-03-17 12:00:05 +01:00
|
|
|
!hasError &&
|
2022-09-20 14:46:25 +02:00
|
|
|
Object.keys(approvedPermissions).length +
|
|
|
|
Object.keys(revokedPermissions).length +
|
|
|
|
Object.keys(newPermissions).length >
|
2023-03-17 12:00:05 +01:00
|
|
|
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(
|
|
|
|
newPermissions,
|
|
|
|
targetSubjectMetadata,
|
|
|
|
t,
|
|
|
|
);
|
|
|
|
|
|
|
|
const shouldShowWarning = warnings.length > 0;
|
|
|
|
|
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-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}
|
|
|
|
>
|
|
|
|
<Box
|
2023-05-23 15:07:44 +02:00
|
|
|
className="snap-update__header"
|
2023-04-03 18:04:30 +02:00
|
|
|
paddingLeft={4}
|
|
|
|
paddingRight={4}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-08-03 18:02:44 +02:00
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
>
|
2023-03-24 17:16:46 +01:00
|
|
|
<SnapAuthorship snapId={targetSubjectMetadata.origin} />
|
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-03-17 12:00:05 +01:00
|
|
|
{t('snapUpdate')}
|
|
|
|
</Text>
|
|
|
|
)}
|
2023-04-25 19:20:37 +02:00
|
|
|
</Box>
|
2023-05-23 15:07:44 +02:00
|
|
|
<Box className="snap-update__content">
|
2023-03-17 12:00:05 +01:00
|
|
|
{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 && (
|
|
|
|
<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
|
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-03-17 12:00:05 +01:00
|
|
|
{t('snapUpdateRequestsPermission', [
|
|
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
|
|
<b key="2">{snapName}</b>,
|
|
|
|
])}
|
|
|
|
</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-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('snapUpdateRequest', [
|
|
|
|
<b key="1">{originMetadata?.hostname}</b>,
|
|
|
|
<b key="2">{snapName}</b>,
|
|
|
|
])}
|
|
|
|
</Text>
|
|
|
|
</Box>
|
|
|
|
)}
|
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}
|
|
|
|
>
|
|
|
|
<PageContainerFooter
|
|
|
|
cancelButtonType="default"
|
2023-03-17 12:00:05 +01:00
|
|
|
hideCancel={hasError}
|
|
|
|
disabled={isLoading}
|
2022-08-03 18:02:44 +02: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 ? 'approveAndUpdate' : 'update',
|
|
|
|
)}
|
2022-08-03 18:02:44 +02:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
{isShowingWarning && (
|
|
|
|
<SnapInstallWarning
|
|
|
|
onCancel={() => setIsShowingWarning(false)}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
snapName={targetSubjectMetadata.name}
|
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,
|
|
|
|
};
|