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 ( {!isLoading && !hasError && ( {t('snapUpdate')} )} {isLoading && ( )} {hasError && ( )} {hasPermissions && ( <> {t('snapUpdateRequestsPermission', [ {originMetadata?.hostname}, {snapName}, ])} )} {isEmpty && ( {t('snapUpdateRequest', [ {originMetadata?.hostname}, {snapName}, ])} )} {isShowingWarning && ( setIsShowingWarning(false)} onSubmit={onSubmit} snapName={targetSubjectMetadata.name} warnings={warnings} /> )} ); } 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, };