Skip to content

Commit 9debf4e

Browse files
committed
chore(suite-native): refactor navigation route matching
1 parent 207245e commit 9debf4e

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

suite-native/device/src/hooks/useHandleDeviceConnection.ts

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

4-
import { useNavigation } from '@react-navigation/native';
4+
import { useNavigation, useNavigationState } from '@react-navigation/native';
55

66
import {
77
authorizeDeviceThunk,
@@ -27,6 +27,7 @@ import {
2727
RootStackParamList,
2828
RootStackRoutes,
2929
StackToStackCompositeNavigationProps,
30+
useNavigationRouteMatch,
3031
} from '@suite-native/navigation';
3132
import { selectIsOnboardingFinished } from '@suite-native/settings';
3233

@@ -41,19 +42,6 @@ type NavigationProp = StackToStackCompositeNavigationProps<
4142
RootStackParamList
4243
>;
4344

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-
5745
export const useHandleDeviceConnection = () => {
5846
const isNoPhysicalDeviceConnected = useSelector(selectIsNoPhysicalDeviceConnected);
5947
const isPortfolioTrackerDevice = useSelector(selectIsPortfolioTrackerDevice);
@@ -81,7 +69,14 @@ export const useHandleDeviceConnection = () => {
8169
const navigation = useNavigation<NavigationProp>();
8270
const dispatch = useDispatch();
8371

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);
8580
const isDeviceSettingsStackFocused = lastRoute === RootStackRoutes.DeviceSettingsStack;
8681
const isSendStackFocused = lastRoute === RootStackRoutes.SendStack;
8782
const shouldBlockSendReviewRedirect = isDeviceRemembered && isSendStackFocused;
@@ -90,7 +85,7 @@ export const useHandleDeviceConnection = () => {
9085

9186
// When is an uninitialized device model that supports device setup, navigate to device onboarding.
9287
useEffect(() => {
93-
if (isSuspiciousDeviceScreenFocused(navigation)) return;
88+
if (isSuspiciousDeviceScreenFocused) return;
9489
if (
9590
isDeviceSetupSupported &&
9691
!isDeviceInitialized &&
@@ -116,6 +111,7 @@ export const useHandleDeviceConnection = () => {
116111
isDeviceConnected,
117112
isOnboardingFinished,
118113
isBiometricsOverlayVisible,
114+
isSuspiciousDeviceScreenFocused,
119115
navigation,
120116
isDeviceInitialized,
121117
isPortfolioTrackerDevice,
@@ -125,7 +121,7 @@ export const useHandleDeviceConnection = () => {
125121
// At the moment when unauthorized physical device is selected,
126122
// redirect to the Connecting screen where is handled the connection logic.
127123
useEffect(() => {
128-
if (isFirmwareInstallationRunning || isSuspiciousDeviceScreenFocused(navigation)) return;
124+
if (isFirmwareInstallationRunning || isSuspiciousDeviceScreenFocused) return;
129125

130126
if (
131127
isDeviceConnected &&
@@ -166,6 +162,7 @@ export const useHandleDeviceConnection = () => {
166162
isDeviceInitialized,
167163
shouldNavigateToDeviceCompromisedModal,
168164
isDeviceSetupSupported,
165+
isSuspiciousDeviceScreenFocused,
169166
]);
170167

171168
// In case that the physical device is disconnected, redirect to the home screen and
@@ -181,7 +178,7 @@ export const useHandleDeviceConnection = () => {
181178
// TODO: this hook is getting very complex, and it's hard to understand the logic when it navigates there and back again.
182179
// Ideally there'd be a single source of truth, a function returning "where we should be as per current state"
183180
// rather than multiple useEffects with imperative instructions "go there when X changes"
184-
if (isDeviceCompromisedModalFocused || isSuspiciousDeviceScreenFocused(navigation)) {
181+
if (isDeviceCompromisedModalFocused || isSuspiciousDeviceScreenFocused) {
185182
return;
186183
}
187184
navigation.navigate(RootStackRoutes.AppTabs, {
@@ -198,6 +195,7 @@ export const useHandleDeviceConnection = () => {
198195
shouldBlockSendReviewRedirect,
199196
isFirmwareInstallationRunning,
200197
isDeviceCompromisedModalFocused,
198+
isSuspiciousDeviceScreenFocused,
201199
]);
202200

203201
// When trezor gets locked, it is necessary to display a PIN matrix for T1 so that it can be unlocked

0 commit comments

Comments
 (0)