diff --git a/components/HandOffComponent.ios.tsx b/components/HandOffComponent.ios.tsx index 93df39d49..1d979c549 100644 --- a/components/HandOffComponent.ios.tsx +++ b/components/HandOffComponent.ios.tsx @@ -2,24 +2,24 @@ import React from 'react'; import DefaultPreference from 'react-native-default-preference'; // @ts-ignore: react-native-handoff is not in the type definition import Handoff from 'react-native-handoff'; +import { useSettings } from '../hooks/context/useSettings'; import { GROUP_IO_BLUEWALLET } from '../blue_modules/currency'; import { BlueApp } from '../class'; -import { useSettings } from '../hooks/context/useSettings'; +import { HandOffComponentProps } from './types'; -interface HandOffComponentProps { - url?: string; - title?: string; - type: (typeof HandOffComponent.activityTypes)[keyof typeof HandOffComponent.activityTypes]; - userInfo?: object; -} +const HandOffComponent: React.FC = props => { + const { isHandOffUseEnabled } = useSettings(); -interface HandOffComponentWithActivityTypes extends React.FC { - activityTypes: { - ReceiveOnchain: string; - Xpub: string; - ViewInBlockExplorer: string; - }; -} + if (process.env.NODE_ENV === 'development') { + console.debug('HandOffComponent: props', props); + } + if (isHandOffUseEnabled) { + return ; + } + return null; +}; + +const MemoizedHandOffComponent = React.memo(HandOffComponent); export const setIsHandOffUseEnabled = async (value: boolean) => { await DefaultPreference.setName(GROUP_IO_BLUEWALLET); @@ -39,24 +39,4 @@ export const getIsHandOffUseEnabled = async (): Promise => { return false; }; -const HandOffComponent: HandOffComponentWithActivityTypes = props => { - const { isHandOffUseEnabled } = useSettings(); - - if (process.env.NODE_ENV === 'development') { - console.debug('HandOffComponent: props', props); - } - if (isHandOffUseEnabled) { - return ; - } - return null; -}; - -const activityTypes = { - ReceiveOnchain: 'io.bluewallet.bluewallet.receiveonchain', - Xpub: 'io.bluewallet.bluewallet.xpub', - ViewInBlockExplorer: 'io.bluewallet.bluewallet.blockexplorer', -}; - -HandOffComponent.activityTypes = activityTypes; - -export default HandOffComponent; +export default MemoizedHandOffComponent; diff --git a/components/HandOffComponent.tsx b/components/HandOffComponent.tsx index 03f70e58b..a814a46b0 100644 --- a/components/HandOffComponent.tsx +++ b/components/HandOffComponent.tsx @@ -1,21 +1,8 @@ import React from 'react'; +import { HandOffComponentProps } from './types'; -interface HandOffComponentProps { - url?: string; - title?: string; - type: (typeof HandOffComponent.activityTypes)[keyof typeof HandOffComponent.activityTypes]; - userInfo?: object; -} - -interface HandOffComponentWithActivityTypes extends React.FC { - activityTypes: { - ReceiveOnchain: string; - Xpub: string; - ViewInBlockExplorer: string; - }; -} - -const HandOffComponent: HandOffComponentWithActivityTypes = props => { +const HandOffComponent: React.FC = props => { + console.debug('HandOffComponent: props', props); return null; }; @@ -25,12 +12,4 @@ export const getIsHandOffUseEnabled = async (): Promise => { return false; }; -const activityTypes = { - ReceiveOnchain: 'io.bluewallet.bluewallet.receiveonchain', - Xpub: 'io.bluewallet.bluewallet.xpub', - ViewInBlockExplorer: 'io.bluewallet.bluewallet.blockexplorer', -}; - -HandOffComponent.activityTypes = activityTypes; - export default HandOffComponent; diff --git a/components/HandOffComponentListener.ios.tsx b/components/HandOffComponentListener.ios.tsx index 5da4aa30b..66a08b230 100644 --- a/components/HandOffComponentListener.ios.tsx +++ b/components/HandOffComponentListener.ios.tsx @@ -1,11 +1,11 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useCallback } from 'react'; import { NativeEventEmitter, NativeModules } from 'react-native'; -import * as NavigationService from '../NavigationService'; -import HandOffComponent from './HandOffComponent.ios'; import { useStorage } from '../hooks/context/useStorage'; +import { useExtendedNavigation } from '../hooks/useExtendedNavigation'; +import { HandOffActivityType } from './types'; interface UserActivityData { - activityType: keyof typeof HandOffComponent.activityTypes; + activityType: HandOffActivityType; userInfo: { address?: string; xpub?: string; @@ -15,40 +15,40 @@ interface UserActivityData { const { EventEmitter } = NativeModules; const eventEmitter = new NativeEventEmitter(EventEmitter); -const HandOffComponentListener: React.FC = () => { +const HandOffComponentListener: React.FC = React.memo(() => { const { walletsInitialized } = useStorage(); + const { navigate } = useExtendedNavigation(); + + const onUserActivityOpen = useCallback((data: UserActivityData) => { + switch (data.activityType) { + case HandOffActivityType.ReceiveOnchain: + navigate('ReceiveDetailsRoot', { + screen: 'ReceiveDetails', + params: { + address: data.userInfo.address, + }, + }); + break; + case HandOffActivityType.Xpub: + navigate('WalletXpubRoot', { + screen: 'WalletXpub', + params: { + xpub: data.userInfo.xpub, + }, + }); + break; + default: + console.log(`Unhandled activity type: ${data.activityType}`); + break; + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); useEffect(() => { if (!walletsInitialized) { return; } - const onUserActivityOpen = (data: UserActivityData) => { - switch (data.activityType) { - case HandOffComponent.activityTypes.ReceiveOnchain: - NavigationService.navigate('ReceiveDetailsRoot', { - // @ts-ignore: fix later - screen: 'ReceiveDetails', - params: { - address: data.userInfo.address, - }, - }); - break; - case HandOffComponent.activityTypes.Xpub: - NavigationService.navigate('WalletXpubRoot', { - // @ts-ignore: fix later - screen: 'WalletXpub', - params: { - xpub: data.userInfo.xpub, - }, - }); - break; - default: - console.log(`Unhandled activity type: ${data.activityType}`); - break; - } - }; - const addListeners = () => { const activitySubscription = eventEmitter.addListener('onUserActivityOpen', onUserActivityOpen); @@ -65,9 +65,9 @@ const HandOffComponentListener: React.FC = () => { return () => { subscriptions.activitySubscription?.remove(); }; - }, [walletsInitialized]); + }, [walletsInitialized, onUserActivityOpen]); return null; -}; +}); export default HandOffComponentListener; diff --git a/components/types.ts b/components/types.ts index d9f4b9835..57a57f67a 100644 --- a/components/types.ts +++ b/components/types.ts @@ -34,3 +34,16 @@ export interface ToolTipMenuProps { onMenuWillShow?: () => void; onMenuWillHide?: () => void; } + +export enum HandOffActivityType { + ReceiveOnchain = 'io.bluewallet.bluewallet.receiveonchain', + Xpub = 'io.bluewallet.bluewallet.xpub', + ViewInBlockExplorer = 'io.bluewallet.bluewallet.blockexplorer', +} + +export interface HandOffComponentProps { + url?: string; + title?: string; + type: HandOffActivityType; + userInfo?: object; +} diff --git a/screen/receive/details.js b/screen/receive/details.js index 4371228f6..45bcfe771 100644 --- a/screen/receive/details.js +++ b/screen/receive/details.js @@ -32,6 +32,7 @@ import loc, { formatBalance } from '../../loc'; import { BitcoinUnit, Chain } from '../../models/bitcoinUnits'; import { SuccessView } from '../send/success'; import { useStorage } from '../../hooks/context/useStorage'; +import { HandOffActivityType } from '../../components/types'; const ReceiveDetails = () => { const { walletID, address } = useRoute().params; @@ -426,7 +427,7 @@ const ReceiveDetails = () => { return ( {address !== undefined && showAddress && ( - + )} {showConfirmedBalance ? renderConfirmedBalance() : null} {showPendingBalance ? renderPendingBalance() : null} diff --git a/screen/send/success.js b/screen/send/success.js index 4840d9944..fa415fa2d 100644 --- a/screen/send/success.js +++ b/screen/send/success.js @@ -1,11 +1,10 @@ +import React, { useEffect, useRef } from 'react'; import { useNavigation, useRoute } from '@react-navigation/native'; import BigNumber from 'bignumber.js'; import LottieView from 'lottie-react-native'; import PropTypes from 'prop-types'; -import React, { useEffect, useRef } from 'react'; import { StyleSheet, View } from 'react-native'; import { Text } from 'react-native-elements'; - import { BlueCard } from '../../BlueComponents'; import Button from '../../components/Button'; import SafeArea from '../../components/SafeArea'; @@ -13,6 +12,7 @@ import { useTheme } from '../../components/themes'; import loc from '../../loc'; import { BitcoinUnit } from '../../models/bitcoinUnits'; import HandOffComponent from '../../components/HandOffComponent'; +import { HandOffActivityType } from '../../components/types'; const Success = () => { const pop = () => { @@ -51,7 +51,7 @@ const Success = () => { {txid && ( )} diff --git a/screen/transactions/TransactionDetails.tsx b/screen/transactions/TransactionDetails.tsx index 599c8fccc..6a8527f9e 100644 --- a/screen/transactions/TransactionDetails.tsx +++ b/screen/transactions/TransactionDetails.tsx @@ -18,6 +18,7 @@ import loc from '../../loc'; import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { DetailViewStackParamList } from '../../navigation/DetailViewStackParamList'; import { useStorage } from '../../hooks/context/useStorage'; +import { HandOffActivityType } from '../../components/types'; interface TransactionDetailsProps { route: RouteProp<{ params: { hash: string; walletID: string } }, 'params'>; @@ -259,7 +260,7 @@ const TransactionDetails = () => { diff --git a/screen/transactions/TransactionStatus.tsx b/screen/transactions/TransactionStatus.tsx index 9d32b80cb..672363b12 100644 --- a/screen/transactions/TransactionStatus.tsx +++ b/screen/transactions/TransactionStatus.tsx @@ -18,6 +18,7 @@ import { useTheme } from '../../components/themes'; import loc, { formatBalanceWithoutSuffix } from '../../loc'; import { BitcoinUnit } from '../../models/bitcoinUnits'; import { useStorage } from '../../hooks/context/useStorage'; +import { HandOffActivityType } from '../../components/types'; import HeaderRightButton from '../../components/HeaderRightButton'; enum ButtonStatus { @@ -476,7 +477,7 @@ const TransactionStatus = () => { diff --git a/screen/wallets/export.js b/screen/wallets/export.js index e67ce9cb9..ea054ca6e 100644 --- a/screen/wallets/export.js +++ b/screen/wallets/export.js @@ -11,6 +11,7 @@ import { useTheme } from '../../components/themes'; import usePrivacy from '../../hooks/usePrivacy'; import loc from '../../loc'; import { useStorage } from '../../hooks/context/useStorage'; +import { HandOffActivityType } from '../../components/types'; const WalletExport = () => { const { wallets, saveToDisk } = useStorage(); @@ -115,11 +116,7 @@ const WalletExport = () => { )} {wallet.type === WatchOnlyWallet.type && ( - + )} ))} diff --git a/screen/wallets/xpub.tsx b/screen/wallets/xpub.tsx index 2fabe76f1..65d8951a1 100644 --- a/screen/wallets/xpub.tsx +++ b/screen/wallets/xpub.tsx @@ -12,6 +12,7 @@ import usePrivacy from '../../hooks/usePrivacy'; import loc from '../../loc'; import { styles, useDynamicStyles } from './xpub.styles'; import { useStorage } from '../../hooks/context/useStorage'; +import { HandOffActivityType } from '../../components/types'; type WalletXpubRouteProp = RouteProp<{ params: { walletID: string; xpub: string } }, 'params'>; export type RootStackParamList = { @@ -94,7 +95,7 @@ const WalletXpub: React.FC = () => { {xPubText && } - +