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: present hw pairing sheet during swap #6275

Merged
merged 9 commits into from
Dec 3, 2024
38 changes: 34 additions & 4 deletions src/__swaps__/screens/Swap/components/SwapBottomPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ import { LIGHT_SEPARATOR_COLOR, SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '@/__
import { NavigationSteps, useSwapContext } from '@/__swaps__/screens/Swap/providers/swap-provider';
import { opacity } from '@/__swaps__/utils/swaps';
import { Box, Separator, globalColors, useColorMode } from '@/design-system';
import React from 'react';
import React, { useCallback } from 'react';
import { StyleSheet } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import Animated, { Easing, useAnimatedStyle, useDerivedValue, useSharedValue, withSpring, withTiming } from 'react-native-reanimated';
import Animated, {
Easing,
runOnJS,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withSpring,
withTiming,
} from 'react-native-reanimated';
import { triggerHaptics } from 'react-native-turbo-haptics';
import { useBottomPanelGestureHandler } from '../hooks/useBottomPanelGestureHandler';
import { GasButton } from './GasButton';
Expand All @@ -14,6 +22,10 @@ import { ReviewPanel } from './ReviewPanel';
import { SwapActionButton } from './SwapActionButton';
import { SettingsPanel } from './SettingsPanel';
import { SPRING_CONFIGS } from '@/components/animations/animationConfigs';
import { useWallets } from '@/hooks';
import { useNavigation } from '@/navigation';
import Routes from '@/navigation/routesNames';
import { logger, RainbowError } from '@/logger';

const HOLD_TO_SWAP_DURATION_MS = 400;

Expand Down Expand Up @@ -56,6 +68,24 @@ export function SwapBottomPanel() {
const opacity = useDerivedValue(() => confirmButtonProps.value.opacity);
const type = useDerivedValue(() => confirmButtonProps.value.type);

const { isHardwareWallet } = useWallets();
const { navigate } = useNavigation();
const handleHwConnectionAndSwap = useCallback(() => {
try {
if (isHardwareWallet && configProgress.value === NavigationSteps.SHOW_REVIEW) {
navigate(Routes.HARDWARE_WALLET_TX_NAVIGATOR, {
submit: SwapNavigation.handleSwapAction,
});
} else {
SwapNavigation.handleSwapAction();
}
} catch (e) {
logger.error(new RainbowError('[SwapBottomPanel.tsx]: handleHwConnectionAndSwap Error: '), {
message: (e as Error).message,
});
}
}, [configProgress.value, navigate, isHardwareWallet, SwapNavigation]);

return (
<PanGestureHandler maxPointers={1} onGestureEvent={swipeToDismissGestureHandler}>
<Animated.View
Expand Down Expand Up @@ -102,7 +132,7 @@ export function SwapBottomPanel() {
onPressWorklet={() => {
'worklet';
if (type.value !== 'hold') {
SwapNavigation.handleSwapAction();
runOnJS(handleHwConnectionAndSwap)();
derHowie marked this conversation as resolved.
Show resolved Hide resolved
}
}}
onLongPressEndWorklet={success => {
Expand All @@ -116,7 +146,7 @@ export function SwapBottomPanel() {
'worklet';
if (type.value === 'hold') {
triggerHaptics('notificationSuccess');
SwapNavigation.handleSwapAction();
runOnJS(handleHwConnectionAndSwap)();
}
}}
onPressStartWorklet={() => {
Expand Down
12 changes: 9 additions & 3 deletions src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
},
},
});
const isHardwareWallet = wallet instanceof LedgerSigner;

if (!wallet) {
isSwapping.value = false;
Expand Down Expand Up @@ -306,7 +307,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
degenMode: isDegenModeEnabled,
isSwappingToPopularAsset,
errorMessage,
isHardwareWallet: wallet instanceof LedgerSigner,
isHardwareWallet,
});

if (errorMessage !== 'handled') {
Expand Down Expand Up @@ -338,7 +339,12 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
performanceTracking.getState().executeFn({
fn: () => {
const { routes, index } = Navigation.getState();
if (index === 0 || routes[index - 1].name === Routes.EXPANDED_ASSET_SHEET) {
const activeRoute = Navigation.getActiveRoute();
if (
index === 0 ||
routes[index - 1].name === Routes.EXPANDED_ASSET_SHEET ||
activeRoute.name === Routes.PAIR_HARDWARE_WALLET_AGAIN_SHEET
) {
Navigation.handleAction(Routes.WALLET_SCREEN, {});
} else {
Navigation.goBack();
Expand Down Expand Up @@ -372,7 +378,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
tradeAmountUSD: parameters.quote.tradeAmountUSD,
degenMode: isDegenModeEnabled,
isSwappingToPopularAsset,
isHardwareWallet: wallet instanceof LedgerSigner,
isHardwareWallet,
});
} catch (error) {
isSwapping.value = false;
Expand Down
Loading