|
| 1 | +///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps) |
| 2 | +import React, { |
| 3 | + useEffect, |
| 4 | + useRef, |
| 5 | + useState, |
| 6 | + useCallback, |
| 7 | + useMemo, |
| 8 | +} from 'react'; |
| 9 | +import { View, TextInput, ScrollView } from 'react-native'; |
| 10 | +import { NativeViewGestureHandler } from 'react-native-gesture-handler'; |
| 11 | +import { Snap } from '@metamask/snaps-utils'; |
| 12 | +import BottomSheet, { |
| 13 | + BottomSheetRef, |
| 14 | +} from '../../../../component-library/components/BottomSheets/BottomSheet'; |
| 15 | +import Text, { |
| 16 | + TextVariant, |
| 17 | +} from '../../../../component-library/components/Texts/Text'; |
| 18 | +import { InternalAccount } from '@metamask/keyring-api'; |
| 19 | +import BannerAlert from '../../../../component-library/components/Banners/Banner/variants/BannerAlert'; |
| 20 | +import { BannerAlertSeverity } from '../../../../component-library/components/Banners/Banner'; |
| 21 | +import BottomSheetHeader from '../../../../component-library/components/BottomSheets/BottomSheetHeader'; |
| 22 | +import { useStyles } from '../../../hooks/useStyles'; |
| 23 | +import stylesheet from './KeyringSnapRemovalWarning.styles'; |
| 24 | +import { strings } from '../../../../../locales/i18n'; |
| 25 | +import { KeyringAccountListItem } from '../components/KeyringAccountListItem'; |
| 26 | +import { getAccountLink } from '@metamask/etherscan-link'; |
| 27 | +import { useSelector } from 'react-redux'; |
| 28 | +import { selectProviderConfig } from '../../../../selectors/networkController'; |
| 29 | +import BottomSheetFooter, { |
| 30 | + ButtonsAlignment, |
| 31 | +} from '../../../../component-library/components/BottomSheets/BottomSheetFooter'; |
| 32 | +import { |
| 33 | + ButtonProps, |
| 34 | + ButtonSize, |
| 35 | + ButtonVariants, |
| 36 | +} from '../../../../component-library/components/Buttons/Button/Button.types'; |
| 37 | +import { |
| 38 | + KEYRING_SNAP_REMOVAL_WARNING, |
| 39 | + KEYRING_SNAP_REMOVAL_WARNING_CANCEL, |
| 40 | + KEYRING_SNAP_REMOVAL_WARNING_CONTINUE, |
| 41 | + KEYRING_SNAP_REMOVAL_WARNING_TEXT_INPUT, |
| 42 | +} from './KeyringSnapRemovalWarning.constants'; |
| 43 | +import Logger from '../../../../util/Logger'; |
| 44 | + |
| 45 | +interface KeyringSnapRemovalWarningProps { |
| 46 | + snap: Snap; |
| 47 | + keyringAccounts: InternalAccount[]; |
| 48 | + onCancel: () => void; |
| 49 | + onClose: () => void; |
| 50 | + onSubmit: () => void; |
| 51 | +} |
| 52 | + |
| 53 | +export default function KeyringSnapRemovalWarning({ |
| 54 | + snap, |
| 55 | + keyringAccounts, |
| 56 | + onCancel, |
| 57 | + onClose, |
| 58 | + onSubmit, |
| 59 | +}: KeyringSnapRemovalWarningProps) { |
| 60 | + const [showConfirmation, setShowConfirmation] = useState(false); |
| 61 | + const [confirmedRemoval, setConfirmedRemoval] = useState(false); |
| 62 | + const [confirmationInput, setConfirmationInput] = useState(''); |
| 63 | + const [error, setError] = useState(false); |
| 64 | + const { chainId } = useSelector(selectProviderConfig); |
| 65 | + const { styles } = useStyles(stylesheet, {}); |
| 66 | + const bottomSheetRef = useRef<BottomSheetRef>(null); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + setShowConfirmation(keyringAccounts.length === 0); |
| 70 | + }, [keyringAccounts]); |
| 71 | + |
| 72 | + const validateConfirmationInput = useCallback( |
| 73 | + (input: string): boolean => input === snap.manifest.proposedName, |
| 74 | + [snap.manifest.proposedName], |
| 75 | + ); |
| 76 | + |
| 77 | + const handleConfirmationInputChange = useCallback( |
| 78 | + (text: string) => { |
| 79 | + setConfirmationInput(text); |
| 80 | + setConfirmedRemoval(validateConfirmationInput(text)); |
| 81 | + }, |
| 82 | + [validateConfirmationInput], |
| 83 | + ); |
| 84 | + |
| 85 | + const handleContinuePress = useCallback(() => { |
| 86 | + if (!showConfirmation) { |
| 87 | + setShowConfirmation(true); |
| 88 | + } else if (confirmedRemoval) { |
| 89 | + try { |
| 90 | + onSubmit(); |
| 91 | + } catch (e) { |
| 92 | + Logger.error( |
| 93 | + e as Error, |
| 94 | + 'KeyringSnapRemovalWarning: error while removing snap', |
| 95 | + ); |
| 96 | + setError(true); |
| 97 | + } |
| 98 | + } |
| 99 | + }, [showConfirmation, confirmedRemoval, onSubmit]); |
| 100 | + |
| 101 | + const cancelButtonProps: ButtonProps = useMemo( |
| 102 | + () => ({ |
| 103 | + variant: ButtonVariants.Secondary, |
| 104 | + label: strings( |
| 105 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.cancel_button', |
| 106 | + ), |
| 107 | + size: ButtonSize.Lg, |
| 108 | + onPress: onCancel, |
| 109 | + testID: KEYRING_SNAP_REMOVAL_WARNING_CANCEL, |
| 110 | + }), |
| 111 | + [onCancel], |
| 112 | + ); |
| 113 | + |
| 114 | + const continueButtonProps: ButtonProps = useMemo( |
| 115 | + () => ({ |
| 116 | + variant: ButtonVariants.Primary, |
| 117 | + label: showConfirmation |
| 118 | + ? strings( |
| 119 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.remove_snap_button', |
| 120 | + ) |
| 121 | + : strings( |
| 122 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.continue_button', |
| 123 | + ), |
| 124 | + size: ButtonSize.Lg, |
| 125 | + onPress: handleContinuePress, |
| 126 | + isDisabled: showConfirmation && !confirmedRemoval, |
| 127 | + isDanger: showConfirmation, |
| 128 | + testID: KEYRING_SNAP_REMOVAL_WARNING_CONTINUE, |
| 129 | + }), |
| 130 | + [showConfirmation, confirmedRemoval, handleContinuePress], |
| 131 | + ); |
| 132 | + |
| 133 | + const buttonPropsArray = useMemo( |
| 134 | + () => [cancelButtonProps, continueButtonProps], |
| 135 | + [cancelButtonProps, continueButtonProps], |
| 136 | + ); |
| 137 | + |
| 138 | + const accountListItems = useMemo( |
| 139 | + () => |
| 140 | + keyringAccounts.map((account, index) => ( |
| 141 | + <KeyringAccountListItem |
| 142 | + key={index} |
| 143 | + account={account} |
| 144 | + blockExplorerUrl={getAccountLink(account.address, chainId)} |
| 145 | + /> |
| 146 | + )), |
| 147 | + [keyringAccounts, chainId], |
| 148 | + ); |
| 149 | + |
| 150 | + return ( |
| 151 | + <BottomSheet |
| 152 | + ref={bottomSheetRef} |
| 153 | + isFullscreen={false} |
| 154 | + onClose={onClose} |
| 155 | + shouldNavigateBack={false} |
| 156 | + testID={KEYRING_SNAP_REMOVAL_WARNING} |
| 157 | + style={styles.bottomSheet} |
| 158 | + > |
| 159 | + <View style={styles.container}> |
| 160 | + <BottomSheetHeader> |
| 161 | + <Text variant={TextVariant.HeadingMD}> |
| 162 | + {strings( |
| 163 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.title', |
| 164 | + )} |
| 165 | + </Text> |
| 166 | + </BottomSheetHeader> |
| 167 | + <BannerAlert |
| 168 | + severity={BannerAlertSeverity.Warning} |
| 169 | + title={strings( |
| 170 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.banner_title', |
| 171 | + )} |
| 172 | + /> |
| 173 | + {showConfirmation ? ( |
| 174 | + <> |
| 175 | + <Text variant={TextVariant.BodyMD} style={styles.description}> |
| 176 | + {`${strings( |
| 177 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.remove_account_snap_alert_description_1', |
| 178 | + )} `} |
| 179 | + <Text variant={TextVariant.BodyMDBold}> |
| 180 | + {snap.manifest.proposedName} |
| 181 | + </Text> |
| 182 | + {` ${strings( |
| 183 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.remove_account_snap_alert_description_2', |
| 184 | + )}`} |
| 185 | + </Text> |
| 186 | + <TextInput |
| 187 | + style={styles.input} |
| 188 | + value={confirmationInput} |
| 189 | + onChangeText={handleConfirmationInputChange} |
| 190 | + testID={KEYRING_SNAP_REMOVAL_WARNING_TEXT_INPUT} |
| 191 | + /> |
| 192 | + {error && ( |
| 193 | + <Text variant={TextVariant.BodySM} style={styles.errorText}> |
| 194 | + {strings( |
| 195 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.remove_snap_error', |
| 196 | + { |
| 197 | + snapName: snap.manifest.proposedName, |
| 198 | + }, |
| 199 | + )} |
| 200 | + </Text> |
| 201 | + )} |
| 202 | + </> |
| 203 | + ) : ( |
| 204 | + <> |
| 205 | + <Text variant={TextVariant.BodyMD} style={styles.description}> |
| 206 | + {strings( |
| 207 | + 'app_settings.snaps.snap_settings.remove_account_snap_warning.description', |
| 208 | + )} |
| 209 | + </Text> |
| 210 | + <NativeViewGestureHandler disallowInterruption> |
| 211 | + <ScrollView style={styles.scrollView}> |
| 212 | + {accountListItems} |
| 213 | + </ScrollView> |
| 214 | + </NativeViewGestureHandler> |
| 215 | + </> |
| 216 | + )} |
| 217 | + </View> |
| 218 | + <BottomSheetFooter |
| 219 | + style={styles.buttonContainer} |
| 220 | + buttonsAlignment={ButtonsAlignment.Horizontal} |
| 221 | + buttonPropsArray={buttonPropsArray} |
| 222 | + /> |
| 223 | + </BottomSheet> |
| 224 | + ); |
| 225 | +} |
| 226 | +///: END:ONLY_INCLUDE_IF |
0 commit comments