import React, { useRef, useState, useEffect } from 'react'; import { useRoute, useNavigation, RouteProp } from '@react-navigation/native'; import { Keyboard, StyleSheet, TextInput, View, ScrollView, TouchableOpacity, Text } from 'react-native'; import { BlueButtonLink, BlueCard, BlueSpacing10, BlueSpacing20, BlueSpacing40, BlueText } from '../../BlueComponents'; import Button from '../../components/Button'; import { useTheme } from '../../components/themes'; import { scanQrHelper } from '../../helpers/scan-qr'; import loc from '../../loc'; import { useStorage } from '../../hooks/context/useStorage'; import { TWallet } from '../../class/wallets/types'; import { WalletCarouselItem } from '../../components/WalletsCarousel'; import { DetailViewStackParamList } from '../../navigation/DetailViewStackParamList'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import Icon from 'react-native-vector-icons/MaterialIcons'; import { Divider } from '@rneui/themed'; import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback'; import presentAlert from '../../components/Alert'; type RouteProps = RouteProp; type NavigationProp = NativeStackNavigationProp; const IsItMyAddress: React.FC = () => { const { wallets } = useStorage(); const navigation = useNavigation(); const route = useRoute(); const { colors } = useTheme(); const scanButtonRef = useRef(); const scrollViewRef = useRef(null); const firstWalletRef = useRef(null); const [address, setAddress] = useState(''); const [matchingWallets, setMatchingWallets] = useState(); const [resultCleanAddress, setResultCleanAddress] = useState(); const stylesHooks = StyleSheet.create({ input: { borderColor: colors.formBorder, borderBottomColor: colors.formBorder, backgroundColor: colors.inputBackgroundColor, }, }); useEffect(() => { if (route.params?.address && route.params.address !== address) { setAddress(route.params.address); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [route.params?.address]); useEffect(() => { const currentAddress = route.params?.address; if (currentAddress !== address) { navigation.setParams({ address }); } }, [address, navigation, route.params?.address]); const handleUpdateAddress = (nextValue: string) => setAddress(nextValue); const clearAddressInput = () => { setAddress(''); setResultCleanAddress(undefined); setMatchingWallets(undefined); }; const checkAddress = () => { Keyboard.dismiss(); const cleanAddress = address.replace('bitcoin:', '').replace('BITCOIN:', '').replace('bitcoin=', '').split('?')[0]; const matching: TWallet[] = []; for (const w of wallets) { if (w.weOwnAddress(cleanAddress)) { matching.push(w); } } if (matching.length > 0) { setMatchingWallets(matching); setResultCleanAddress(cleanAddress); triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess); } else { triggerHapticFeedback(HapticFeedbackTypes.NotificationError); presentAlert({ message: loc.is_it_my_address.no_wallet_owns_address, buttons: [ { text: loc.receive.reset, onPress: () => { clearAddressInput(); }, style: 'destructive', }, { text: loc._.ok, onPress: () => {}, style: 'cancel', }, ], options: { cancelable: true }, }); setMatchingWallets([]); setResultCleanAddress(undefined); } }; const onBarScanned = (value: string) => { const cleanAddress = value.replace(/^bitcoin(:|=)/i, '').split('?')[0]; setAddress(value); setResultCleanAddress(cleanAddress); }; const importScan = async () => { const data = await scanQrHelper(route.name, true, undefined, true); if (data) { onBarScanned(data); } }; const viewQRCode = () => { if (!resultCleanAddress) return; navigation.navigate('ReceiveDetailsRoot', { screen: 'ReceiveDetails', params: { address: resultCleanAddress, }, }); }; const isCheckAddressDisabled = address.trim().length === 0; useEffect(() => { if (matchingWallets && matchingWallets.length > 0 && scrollViewRef.current && firstWalletRef.current) { firstWalletRef.current.measureLayout(scrollViewRef.current.getInnerViewNode(), (x, y) => { scrollViewRef.current?.scrollTo({ x: 0, y: y - 20, animated: true }); }); } }, [matchingWallets]); const renderFormattedText = (text: string, values: { [key: string]: string }) => { const regex = /\{(\w+)\}/g; const parts = []; let lastIndex = 0; let match; let index = 0; while ((match = regex.exec(text)) !== null) { if (match.index > lastIndex) { parts.push({text.substring(lastIndex, match.index)}); } const value = values[match[1]]; if (value) { parts.push( {value} , ); } lastIndex = regex.lastIndex; } if (lastIndex < text.length) { parts.push({text.substring(lastIndex)}); } return parts; }; return ( {address.length > 0 && ( )} {resultCleanAddress && ( <>