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, BackgroundColor, BLOCK_SIZES, BorderStyle, FLEX_DIRECTION, JustifyContent, TextVariant, TEXT_ALIGN, FontWeight, IconColor, } from '../../../../helpers/constants/design-system'; import { getSnapInstallWarnings } from '../util'; import PulseLoader from '../../../../components/ui/pulse-loader/pulse-loader'; import SnapAuthorshipHeader from '../../../../components/app/snaps/snap-authorship-header'; import { AvatarIcon, IconName, Text, ValidTag, } from '../../../../components/component-library'; import { getSnapName } from '../../../../helpers/utils/util'; import SnapPermissionsList from '../../../../components/app/snaps/snap-permissions-list'; import { useScrollRequired } from '../../../../hooks/useScrollRequired'; import SiteOrigin from '../../../../components/ui/site-origin/site-origin'; import InstallError from '../../../../components/app/snaps/install-error/install-error'; import { useOriginMetadata } from '../../../../hooks/useOriginMetadata'; export default function SnapInstall({ request, requestState, approveSnapInstall, rejectSnapInstall, targetSubjectMetadata, }) { const t = useI18nContext(); const siteMetadata = useOriginMetadata(request?.metadata?.dappOrigin) || {}; const { origin, iconUrl, name } = siteMetadata; const [isShowingWarning, setIsShowingWarning] = useState(false); const { isScrollable, isScrolledToBottom, scrollToBottom, ref, onScroll } = useScrollRequired([requestState]); const onCancel = useCallback( () => rejectSnapInstall(request.metadata.id), [request, rejectSnapInstall], ); const onSubmit = useCallback( () => approveSnapInstall(request.metadata.id), [request, approveSnapInstall], ); const hasError = !requestState.loading && requestState.error; const isLoading = requestState.loading; const warnings = getSnapInstallWarnings( requestState?.permissions ?? {}, targetSubjectMetadata, t, ); const shouldShowWarning = warnings.length > 0; const snapName = getSnapName( targetSubjectMetadata.origin, targetSubjectMetadata, ); const handleSubmit = () => { if (!hasError && shouldShowWarning) { setIsShowingWarning(true); } else if (hasError) { onCancel(); } else { onSubmit(); } }; const getFooterMessage = () => { if (hasError) { return 'ok'; } else if (isLoading) { return 'connect'; } return 'install'; }; return ( {isLoading || hasError ? ( ) : ( )} {isLoading && ( )} {hasError && ( {snapName} , ])} error={requestState.error} /> )} {!hasError && !isLoading && ( <> {t('snapInstall')} {t('snapInstallRequest', [ {snapName} , ])} {isScrollable && !isScrolledToBottom ? ( ) : null} )} {isShowingWarning && ( setIsShowingWarning(false)} onSubmit={onSubmit} warnings={warnings} snapName={snapName} /> )} ); } SnapInstall.propTypes = { request: PropTypes.object.isRequired, requestState: PropTypes.object.isRequired, approveSnapInstall: PropTypes.func.isRequired, rejectSnapInstall: PropTypes.func.isRequired, targetSubjectMetadata: PropTypes.shape({ iconUrl: PropTypes.string, name: PropTypes.string, origin: PropTypes.string.isRequired, sourceCode: PropTypes.string, version: PropTypes.string, }).isRequired, };