import PropTypes from 'prop-types'; import React, { useCallback, useState } from 'react'; import { PageContainerFooter } from '../../../../components/ui/page-container'; import PermissionsConnectFooter from '../../../../components/app/permissions-connect-footer'; import PermissionConnectHeader from '../../../../components/app/permissions-connect-header'; import { useI18nContext } from '../../../../hooks/useI18nContext'; import SnapInstallWarning from '../../../../components/app/flask/snap-install-warning'; import Box from '../../../../components/ui/box/box'; import { AlignItems, BLOCK_SIZES, BorderStyle, FLEX_DIRECTION, JustifyContent, TypographyVariant, } from '../../../../helpers/constants/design-system'; import Typography from '../../../../components/ui/typography'; import UpdateSnapPermissionList from '../../../../components/app/flask/update-snap-permission-list'; import { getSnapInstallWarnings } from '../util'; export default function SnapUpdate({ request, approveSnapUpdate, rejectSnapUpdate, targetSubjectMetadata, }) { const t = useI18nContext(); const [isShowingWarning, setIsShowingWarning] = useState(false); const onCancel = useCallback( () => rejectSnapUpdate(request.metadata.id), [request, rejectSnapUpdate], ); const onSubmit = useCallback( () => approveSnapUpdate(request.metadata.id), [request, approveSnapUpdate], ); const approvedPermissions = request.approvedPermissions ?? {}; const revokedPermissions = request.unusedPermissions ?? {}; const newPermissions = request.newPermissions ?? {}; const hasPermissions = Object.keys(approvedPermissions).length + Object.keys(revokedPermissions).length + Object.keys(newPermissions).length > 0; const warnings = getSnapInstallWarnings( newPermissions, targetSubjectMetadata, t, ); const shouldShowWarning = warnings.length > 0; return ( {t('snapUpdateExplanation', [`${request.metadata.dappOrigin}`])} {hasPermissions && ( <> {t('snapRequestsPermission')} )} setIsShowingWarning(true) : onSubmit } submitText={t('approveAndUpdate')} /> {isShowingWarning && ( setIsShowingWarning(false)} onSubmit={onSubmit} snapName={targetSubjectMetadata.name} warnings={warnings} /> )} ); } SnapUpdate.propTypes = { request: 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, };