From cdf129f52536feb3c2936dd0c96a2aba30828cea Mon Sep 17 00:00:00 2001 From: yanas Date: Wed, 18 Feb 2026 16:18:32 +0100 Subject: [PATCH] refactor(suite-native): use THP pairing events for THP screen dismissal --- .../module-authorize-device/package.json | 1 - .../src/hooks/useThpScreenDismissal.ts | 44 +++++++++++++++++++ .../src/screens/thp/ThpCodeEntryScreen.tsx | 20 ++------- .../src/screens/thp/ThpConfirmationScreen.tsx | 17 ++----- .../module-authorize-device/src/selectors.ts | 18 -------- .../module-authorize-device/tsconfig.json | 3 -- yarn.lock | 1 - 7 files changed, 51 insertions(+), 53 deletions(-) create mode 100644 suite-native/module-authorize-device/src/hooks/useThpScreenDismissal.ts delete mode 100644 suite-native/module-authorize-device/src/selectors.ts diff --git a/suite-native/module-authorize-device/package.json b/suite-native/module-authorize-device/package.json index 0487e21e23..077eb8ff9e 100644 --- a/suite-native/module-authorize-device/package.json +++ b/suite-native/module-authorize-device/package.json @@ -16,7 +16,6 @@ "@suite-common/analytics": "workspace:*", "@suite-common/bluetooth": "workspace:*", "@suite-common/device": "workspace:*", - "@suite-common/redux-utils": "workspace:*", "@suite-common/thp": "workspace:*", "@suite-common/wallet-core": "workspace:*", "@suite-native/alerts": "workspace:*", diff --git a/suite-native/module-authorize-device/src/hooks/useThpScreenDismissal.ts b/suite-native/module-authorize-device/src/hooks/useThpScreenDismissal.ts new file mode 100644 index 0000000000..abc9477a06 --- /dev/null +++ b/suite-native/module-authorize-device/src/hooks/useThpScreenDismissal.ts @@ -0,0 +1,44 @@ +import { useCallback } from 'react'; +import { useSelector } from 'react-redux'; + +import { useFocusEffect } from '@react-navigation/native'; + +import { selectIsDeviceThpLocked } from '@suite-common/device'; +import { selectThpAutoconnectStep, selectThpStep } from '@suite-common/thp'; +import { useNavigateToInitialScreen } from '@suite-native/navigation'; +import TrezorConnect, { DEVICE, DeviceThpPairingStatus } from '@trezor/connect'; + +export const useThpScreenDismissal = () => { + const navigateToInitialScreen = useNavigateToInitialScreen(); + + const thpStep = useSelector(selectThpStep); + const thpAutoconnectStep = useSelector(selectThpAutoconnectStep); + const isDeviceThpLocked = useSelector(selectIsDeviceThpLocked); + + const onThpPairingStatusChange = useCallback( + (e: DeviceThpPairingStatus) => { + if (e.status === 'canceled') { + navigateToInitialScreen(); + } + }, + [navigateToInitialScreen], + ); + + useFocusEffect( + useCallback(() => { + TrezorConnect.on(DEVICE.THP_PAIRING_STATUS_CHANGED, onThpPairingStatusChange); + + return () => { + TrezorConnect.off(DEVICE.THP_PAIRING_STATUS_CHANGED, onThpPairingStatusChange); + }; + }, [onThpPairingStatusChange]), + ); + + useFocusEffect( + useCallback(() => { + if (thpStep === null && thpAutoconnectStep === null && !isDeviceThpLocked) { + navigateToInitialScreen(); + } + }, [thpStep, thpAutoconnectStep, isDeviceThpLocked, navigateToInitialScreen]), + ); +}; diff --git a/suite-native/module-authorize-device/src/screens/thp/ThpCodeEntryScreen.tsx b/suite-native/module-authorize-device/src/screens/thp/ThpCodeEntryScreen.tsx index 896f34a4f2..a330ceadda 100644 --- a/suite-native/module-authorize-device/src/screens/thp/ThpCodeEntryScreen.tsx +++ b/suite-native/module-authorize-device/src/screens/thp/ThpCodeEntryScreen.tsx @@ -1,28 +1,16 @@ -import React, { useCallback } from 'react'; -import { useSelector } from 'react-redux'; +import React from 'react'; -import { useFocusEffect } from '@react-navigation/native'; - -import { Screen, useNavigateToInitialScreen } from '@suite-native/navigation'; +import { Screen } from '@suite-native/navigation'; import { ThpCodeEntryScreenContent } from '@suite-native/thp'; import { ThpScreenHeader } from '../../components/thp/ThpScreenHeader'; import { useInitiateThpConnection } from '../../hooks/useInitiateThpConnection'; -import { selectIsThpScreenDismissable } from '../../selectors'; +import { useThpScreenDismissal } from '../../hooks/useThpScreenDismissal'; export const ThpCodeEntryScreen = () => { - const navigateToInitialScreen = useNavigateToInitialScreen(); const { initiateThpConnection } = useInitiateThpConnection(); - const isThpScreenDismissable = useSelector(selectIsThpScreenDismissable); - - useFocusEffect( - useCallback(() => { - if (isThpScreenDismissable) { - navigateToInitialScreen(); - } - }, [isThpScreenDismissable, navigateToInitialScreen]), - ); + useThpScreenDismissal(); return ( }> diff --git a/suite-native/module-authorize-device/src/screens/thp/ThpConfirmationScreen.tsx b/suite-native/module-authorize-device/src/screens/thp/ThpConfirmationScreen.tsx index 476c24f982..fea8839872 100644 --- a/suite-native/module-authorize-device/src/screens/thp/ThpConfirmationScreen.tsx +++ b/suite-native/module-authorize-device/src/screens/thp/ThpConfirmationScreen.tsx @@ -11,12 +11,11 @@ import { AuthorizeDeviceStackRoutes, Screen, useInterceptNativeNavigation, - useNavigateToInitialScreen, } from '@suite-native/navigation'; import { useThpAutoconnectAlert } from '@suite-native/thp'; import { ThpScreenHeader } from '../../components/thp/ThpScreenHeader'; -import { selectIsThpScreenDismissable } from '../../selectors'; +import { useThpScreenDismissal } from '../../hooks/useThpScreenDismissal'; export const ThpConfirmationScreen = ({ navigation, @@ -24,13 +23,12 @@ export const ThpConfirmationScreen = ({ navigation: NativeStackNavigationProp; }) => { const { showEnableThpAutoconnectAlert } = useThpAutoconnectAlert(); - const navigateToInitialScreen = useNavigateToInitialScreen(); const thpStep = useSelector(selectThpStep); const thpAutoconnectStep = useSelector(selectThpAutoconnectStep); - const isThpScreenDismissable = useSelector(selectIsThpScreenDismissable); useInterceptNativeNavigation(); + useThpScreenDismissal(); useFocusEffect( useCallback(() => { @@ -38,17 +36,8 @@ export const ThpConfirmationScreen = ({ navigation.replace(AuthorizeDeviceStackRoutes.ThpCodeEntry); } else if (thpAutoconnectStep === 'AutoconnectInfo') { showEnableThpAutoconnectAlert(); - } else if (isThpScreenDismissable) { - navigateToInitialScreen(); } - }, [ - thpStep, - thpAutoconnectStep, - isThpScreenDismissable, - showEnableThpAutoconnectAlert, - navigateToInitialScreen, - navigation, - ]), + }, [thpStep, thpAutoconnectStep, showEnableThpAutoconnectAlert, navigation]), ); return ( diff --git a/suite-native/module-authorize-device/src/selectors.ts b/suite-native/module-authorize-device/src/selectors.ts deleted file mode 100644 index 03bbd90277..0000000000 --- a/suite-native/module-authorize-device/src/selectors.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { DeviceRootState, selectIsDeviceThpLocked } from '@suite-common/device'; -import { createWeakMapSelector } from '@suite-common/redux-utils'; -import { - ThpRootState, - selectThpAutoconnectStep, - selectThpLastResult, - selectThpStep, -} from '@suite-common/thp'; - -const createMemoizedSelector = createWeakMapSelector.withTypes(); - -export const selectIsThpScreenDismissable = createMemoizedSelector( - [selectThpStep, selectThpAutoconnectStep, selectThpLastResult, selectIsDeviceThpLocked], - (thpStep, thpAutoconnectStep, thpLastResult, isDeviceThpLocked) => - thpStep === null && - thpAutoconnectStep === null && - (thpLastResult === 'canceled' || !isDeviceThpLocked), -); diff --git a/suite-native/module-authorize-device/tsconfig.json b/suite-native/module-authorize-device/tsconfig.json index ead7bcf330..8096ecb97b 100644 --- a/suite-native/module-authorize-device/tsconfig.json +++ b/suite-native/module-authorize-device/tsconfig.json @@ -9,9 +9,6 @@ "path": "../../suite-common/bluetooth" }, { "path": "../../suite-common/device" }, - { - "path": "../../suite-common/redux-utils" - }, { "path": "../../suite-common/thp" }, { "path": "../../suite-common/wallet-core" diff --git a/yarn.lock b/yarn.lock index 7ec8671268..fe786a2bd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13012,7 +13012,6 @@ __metadata: "@suite-common/analytics": "workspace:*" "@suite-common/bluetooth": "workspace:*" "@suite-common/device": "workspace:*" - "@suite-common/redux-utils": "workspace:*" "@suite-common/thp": "workspace:*" "@suite-common/wallet-core": "workspace:*" "@suite-native/alerts": "workspace:*"