Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Main scanner redirect #889

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/NavigationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const NavigationHeader = ({
return (
<View style={[container, style]}>
<View style={[styles.leftColumn, buttonOffset]}>
{displayBackButton && (
{displayBackButton && navigation.canGoBack() && (
<ActionButton onPress={handleBackPress} testID="NavigationBack">
<BackIcon width={20} height={20} />
</ActionButton>
Expand Down
28 changes: 20 additions & 8 deletions src/navigation/bottom-sheet/SendNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { ReactElement, memo } from 'react';
import { useSelector } from 'react-redux';
import { createNavigationContainerRef } from '@react-navigation/native';
import {
createNativeStackNavigator,
NativeStackNavigationOptions,
Expand All @@ -20,14 +21,13 @@ import Scanner from '../../screens/Wallets/Send/Scanner';
import Contacts from '../../screens/Wallets/Send/Contacts';
import CoinSelection from '../../screens/Wallets/Send/CoinSelection';
import { NavigationContainer } from '../../styles/components';
import { TProcessedData } from '../../utils/scanner';
import { useSnapPoints } from '../../hooks/bottomSheet';
import { viewControllerSelector } from '../../store/reselect/ui';
import {
resetOnChainTransaction,
setupOnChainTransaction,
} from '../../store/actions/wallet';
import { useSnapPoints } from '../../hooks/bottomSheet';
import { viewControllerIsOpenSelector } from '../../store/reselect/ui';
import { TProcessedData } from '../../utils/scanner';
import { createNavigationContainerRef } from '@react-navigation/native';

export type SendNavigationProp = NativeStackNavigationProp<SendStackParamList>;

Expand Down Expand Up @@ -71,6 +71,14 @@ export const sendNavigation = {
: never
): void {
if (navigationRef.isReady()) {
const currentRoute = navigationRef.getCurrentRoute()?.name;
const nextRoute = args[0];

if (currentRoute === nextRoute) {
console.log(`Already on screen ${currentRoute}. Skipping...`);
return;
}

navigationRef.navigate(...args);
} else {
// sendNavigation not ready, try again after a short wait
Expand All @@ -81,9 +89,11 @@ export const sendNavigation = {

const SendNavigation = (): ReactElement => {
const snapPoints = useSnapPoints('large');
const isOpen = useSelector((state) =>
viewControllerIsOpenSelector(state, 'sendNavigation'),
);
const { isOpen, screen } = useSelector((state) => {
return viewControllerSelector(state, 'sendNavigation');
});

const initialRouteName = screen ?? 'Recipient';

return (
<BottomSheetWrapper
Expand All @@ -92,7 +102,9 @@ const SendNavigation = (): ReactElement => {
onClose={resetOnChainTransaction}
onOpen={setupOnChainTransaction}>
<NavigationContainer key={isOpen.toString()} ref={navigationRef}>
<Stack.Navigator screenOptions={screenOptions}>
<Stack.Navigator
initialRouteName={initialRouteName}
screenOptions={screenOptions}>
<Stack.Screen name="Recipient" component={Recipient} />
<Stack.Screen name="Scanner" component={Scanner} />
<Stack.Screen name="Contacts" component={Contacts} />
Expand Down
25 changes: 14 additions & 11 deletions src/utils/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,11 @@ export const handleData = async ({
return ok({ type: EQRDataType.slashAuthURL });
}
case EQRDataType.bitcoinAddress: {
showBottomSheet('sendNavigation');
// If BottomSheet is not open yet (MainScanner)
showBottomSheet('sendNavigation', { screen: 'Amount' });

// If BottomSheet is already open (SendScanner)
sendNavigation.navigate('Amount');

// If no amount found in payment request, make sure that the user hasn't previously specified an amount from the send form.
if (!amount) {
Expand All @@ -727,8 +731,6 @@ export const handleData = async ({
},
});

sendNavigation.navigate('Amount');

return ok({ type: EQRDataType.bitcoinAddress, address, amount });
}

Expand All @@ -744,9 +746,16 @@ export const handleData = async ({
return err(decodedInvoice.error.message);
}

showBottomSheet('sendNavigation');

const invoiceAmount = decodedInvoice.value.amount_satoshis ?? 0;

if (invoiceAmount) {
showBottomSheet('sendNavigation', { screen: 'ReviewAndSend' });
sendNavigation.navigate('ReviewAndSend');
} else {
showBottomSheet('sendNavigation', { screen: 'Amount' });
sendNavigation.navigate('Amount');
}

await updateBitcoinTransaction({
selectedWallet,
selectedNetwork,
Expand All @@ -763,12 +772,6 @@ export const handleData = async ({
},
});

if (invoiceAmount) {
sendNavigation.navigate('ReviewAndSend');
} else {
sendNavigation.navigate('Amount');
}

return ok({
type: EQRDataType.lightningPaymentRequest,
amount: invoiceAmount,
Expand Down