1
1
import { useEffect } from 'react' ;
2
2
import { useDispatch , useSelector } from 'react-redux' ;
3
3
4
- import { useNavigation } from '@react-navigation/native' ;
4
+ import { useNavigation , useNavigationState } from '@react-navigation/native' ;
5
5
6
6
import {
7
7
authorizeDeviceThunk ,
@@ -27,6 +27,7 @@ import {
27
27
RootStackParamList ,
28
28
RootStackRoutes ,
29
29
StackToStackCompositeNavigationProps ,
30
+ useNavigationRouteMatch ,
30
31
} from '@suite-native/navigation' ;
31
32
import { selectIsOnboardingFinished } from '@suite-native/settings' ;
32
33
@@ -41,19 +42,6 @@ type NavigationProp = StackToStackCompositeNavigationProps<
41
42
RootStackParamList
42
43
> ;
43
44
44
- // We encourage user to disconnect device when he is redirected to suspicious device screen.
45
- // We should not redirect him away so he can read the screen content and decide what to do.
46
- // If the device is connected again, he still should stay on that screen.
47
- const isSuspiciousDeviceScreenFocused = ( navigation : NavigationProp ) => {
48
- const previousRoute = navigation . getState ( ) ?. routes . at ( - 1 ) ;
49
- const innerStackRoute = previousRoute ?. state ?. routes . at ( - 1 ) ;
50
-
51
- return (
52
- previousRoute ?. name === RootStackRoutes . OnboardingStack &&
53
- innerStackRoute ?. name === OnboardingStackRoutes . SuspiciousDevice
54
- ) ;
55
- } ;
56
-
57
45
export const useHandleDeviceConnection = ( ) => {
58
46
const isNoPhysicalDeviceConnected = useSelector ( selectIsNoPhysicalDeviceConnected ) ;
59
47
const isPortfolioTrackerDevice = useSelector ( selectIsPortfolioTrackerDevice ) ;
@@ -81,7 +69,14 @@ export const useHandleDeviceConnection = () => {
81
69
const navigation = useNavigation < NavigationProp > ( ) ;
82
70
const dispatch = useDispatch ( ) ;
83
71
84
- const lastRoute = navigation . getState ( ) ?. routes . at ( - 1 ) ?. name ;
72
+ // We encourage user to disconnect device when he is redirected to suspicious device screen.
73
+ // We should not redirect him away so he can read the screen content and decide what to do.
74
+ // If the device is connected again, he still should stay on that screen.
75
+ const isSuspiciousDeviceScreenFocused = useNavigationRouteMatch (
76
+ OnboardingStackRoutes . SuspiciousDevice ,
77
+ ) ;
78
+
79
+ const lastRoute = useNavigationState ( state => state ?. routes . at ( - 1 ) ?. name ) ;
85
80
const isDeviceSettingsStackFocused = lastRoute === RootStackRoutes . DeviceSettingsStack ;
86
81
const isSendStackFocused = lastRoute === RootStackRoutes . SendStack ;
87
82
const shouldBlockSendReviewRedirect = isDeviceRemembered && isSendStackFocused ;
@@ -90,7 +85,7 @@ export const useHandleDeviceConnection = () => {
90
85
91
86
// When is an uninitialized device model that supports device setup, navigate to device onboarding.
92
87
useEffect ( ( ) => {
93
- if ( isSuspiciousDeviceScreenFocused ( navigation ) ) return ;
88
+ if ( isSuspiciousDeviceScreenFocused ) return ;
94
89
if (
95
90
isDeviceSetupSupported &&
96
91
! isDeviceInitialized &&
@@ -116,6 +111,7 @@ export const useHandleDeviceConnection = () => {
116
111
isDeviceConnected ,
117
112
isOnboardingFinished ,
118
113
isBiometricsOverlayVisible ,
114
+ isSuspiciousDeviceScreenFocused ,
119
115
navigation ,
120
116
isDeviceInitialized ,
121
117
isPortfolioTrackerDevice ,
@@ -125,7 +121,7 @@ export const useHandleDeviceConnection = () => {
125
121
// At the moment when unauthorized physical device is selected,
126
122
// redirect to the Connecting screen where is handled the connection logic.
127
123
useEffect ( ( ) => {
128
- if ( isFirmwareInstallationRunning || isSuspiciousDeviceScreenFocused ( navigation ) ) return ;
124
+ if ( isFirmwareInstallationRunning || isSuspiciousDeviceScreenFocused ) return ;
129
125
130
126
if (
131
127
isDeviceConnected &&
@@ -166,6 +162,7 @@ export const useHandleDeviceConnection = () => {
166
162
isDeviceInitialized ,
167
163
shouldNavigateToDeviceCompromisedModal ,
168
164
isDeviceSetupSupported ,
165
+ isSuspiciousDeviceScreenFocused ,
169
166
] ) ;
170
167
171
168
// In case that the physical device is disconnected, redirect to the home screen and
@@ -181,7 +178,7 @@ export const useHandleDeviceConnection = () => {
181
178
// TODO: this hook is getting very complex, and it's hard to understand the logic when it navigates there and back again.
182
179
// Ideally there'd be a single source of truth, a function returning "where we should be as per current state"
183
180
// rather than multiple useEffects with imperative instructions "go there when X changes"
184
- if ( isDeviceCompromisedModalFocused || isSuspiciousDeviceScreenFocused ( navigation ) ) {
181
+ if ( isDeviceCompromisedModalFocused || isSuspiciousDeviceScreenFocused ) {
185
182
return ;
186
183
}
187
184
navigation . navigate ( RootStackRoutes . AppTabs , {
@@ -198,6 +195,7 @@ export const useHandleDeviceConnection = () => {
198
195
shouldBlockSendReviewRedirect ,
199
196
isFirmwareInstallationRunning ,
200
197
isDeviceCompromisedModalFocused ,
198
+ isSuspiciousDeviceScreenFocused ,
201
199
] ) ;
202
200
203
201
// When trezor gets locked, it is necessary to display a PIN matrix for T1 so that it can be unlocked
0 commit comments