refactor(suite-native): use THP pairing events for THP screen dismissal

This commit is contained in:
yanas
2026-02-18 16:18:32 +01:00
committed by Szymon Lesisz
parent 65ae4bb342
commit cdf129f525
7 changed files with 51 additions and 53 deletions

View File

@@ -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:*",

View File

@@ -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]),
);
};

View File

@@ -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 (
<Screen header={<ThpScreenHeader />}>

View File

@@ -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<AuthorizeDeviceStackParamList>;
}) => {
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 (

View File

@@ -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<ThpRootState & DeviceRootState>();
export const selectIsThpScreenDismissable = createMemoizedSelector(
[selectThpStep, selectThpAutoconnectStep, selectThpLastResult, selectIsDeviceThpLocked],
(thpStep, thpAutoconnectStep, thpLastResult, isDeviceThpLocked) =>
thpStep === null &&
thpAutoconnectStep === null &&
(thpLastResult === 'canceled' || !isDeviceThpLocked),
);

View File

@@ -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"

View File

@@ -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:*"