1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00
metamask-extension/ui/pages/permissions-connect/snaps/snap-update/snap-update.js
Olaf Tomalka 95c37e1ba3
feat: add yaml feature management (#18125)
* feat: add yaml feature management

Add yaml feature file per build type.
Also add method to parse yaml and set
enabled features env to true. The build
process will then replace any process.env[feature]
that exists on the config by its value

* chore: add example for desktop

* Added initial draft of build features

* [TMP] Sync between computers

* Is able to succesfully build stable extension with snaps feature

* Removing var context from builds.yml

* Add asssets to builds.yml

* Minor bug fixes and removing debug logs

* [WIP] Test changes

* Removed TODOs

* Fix regession bug

Also
* remove debug logs
* merge Variables.set and Variables.setMany with an overload

* Fix build, lint and a bunch of issues

* Update LavaMoat policies

* Re-add desktop build type

* Fix some tests

* Fix desktop build

* Define some env variables used by MV3

* Fix lint

* Fix remove-fenced-code tests

* Fix README typo

* Move new code

* Fix missing asset copy

* Move Jest env setup

* Fix path for test after rebase

* Fix code fences

* Fix fencing and LavaMoat policies

* Fix MMI code-fencing after rebase

* Fix MMI code fencing after merge

* Fix more MMI code fencing

---------

Co-authored-by: cryptotavares <joao.tavares@consensys.net>
Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Brad Decker <bhdecker84@gmail.com>
2023-04-25 16:32:51 +02:00

201 lines
6.1 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 UpdateSnapPermissionList from '../../../../components/app/snaps/update-snap-permission-list';
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';
export default function SnapUpdate({
request,
requestState,
approveSnapUpdate,
rejectSnapUpdate,
targetSubjectMetadata,
}) {
const t = useI18nContext();
const [isShowingWarning, setIsShowingWarning] = useState(false);
const originMetadata = useOriginMetadata(request.metadata?.dappOrigin) || {};
const onCancel = useCallback(
() => rejectSnapUpdate(request.metadata.id),
[request, rejectSnapUpdate],
);
const onSubmit = useCallback(
() => approveSnapUpdate(request.metadata.id),
[request, approveSnapUpdate],
);
const approvedPermissions = requestState.approvedPermissions ?? {};
const revokedPermissions = requestState.unusedPermissions ?? {};
const newPermissions = requestState.newPermissions ?? {};
const isLoading = requestState.loading;
const hasError = !isLoading && requestState.error;
const hasPermissions =
!hasError &&
Object.keys(approvedPermissions).length +
Object.keys(revokedPermissions).length +
Object.keys(newPermissions).length >
0;
const isEmpty = !isLoading && !hasError && !hasPermissions;
const warnings = getSnapInstallWarnings(
newPermissions,
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-update"
justifyContent={JustifyContent.spaceBetween}
height={BLOCK_SIZES.FULL}
borderStyle={BorderStyle.none}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<Box
className="headers"
paddingLeft={4}
paddingRight={4}
alignItems={AlignItems.center}
flexDirection={FLEX_DIRECTION.COLUMN}
>
<SnapAuthorship snapId={targetSubjectMetadata.origin} />
{!hasError && (
<Text
paddingBottom={4}
paddingTop={4}
variant={TextVariant.headingLg}
>
{t('snapUpdate')}
</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')} />
)}
{hasPermissions && (
<>
<Text
className="headers__permission-description"
paddingBottom={4}
textAlign={TEXT_ALIGN.CENTER}
>
{t('snapUpdateRequestsPermission', [
<b key="1">{originMetadata?.hostname}</b>,
<b key="2">{snapName}</b>,
])}
</Text>
<UpdateSnapPermissionList
approvedPermissions={approvedPermissions}
revokedPermissions={revokedPermissions}
newPermissions={newPermissions}
targetSubjectMetadata={targetSubjectMetadata}
/>
</>
)}
{isEmpty && (
<Box
flexDirection={FLEX_DIRECTION.COLUMN}
height={BLOCK_SIZES.FULL}
alignItems={AlignItems.center}
justifyContent={JustifyContent.center}
>
<Text textAlign={TEXT_ALIGN.CENTER}>
{t('snapUpdateRequest', [
<b key="1">{originMetadata?.hostname}</b>,
<b key="2">{snapName}</b>,
])}
</Text>
</Box>
)}
</Box>
<Box
className="footers"
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 ? 'approveAndUpdate' : 'update',
)}
/>
</Box>
{isShowingWarning && (
<SnapInstallWarning
onCancel={() => setIsShowingWarning(false)}
onSubmit={onSubmit}
snapName={targetSubjectMetadata.name}
warnings={warnings}
/>
)}
</Box>
);
}
SnapUpdate.propTypes = {
request: PropTypes.object.isRequired,
requestState: PropTypes.object.isRequired,
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,
};