-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathPerpsSelectModifyActionView.tsx
More file actions
167 lines (152 loc) · 5.41 KB
/
Copy pathPerpsSelectModifyActionView.tsx
File metadata and controls
167 lines (152 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { useCallback, useRef } from 'react';
import {
useNavigation,
useRoute,
type RouteProp,
} from '@react-navigation/native';
import type { AppNavigationProp } from '../../../../../core/NavigationService/types';
import {
PERPS_EVENT_PROPERTY,
PERPS_EVENT_VALUE,
type Position,
} from '@metamask/perps-controller';
import type { PerpsNavigationParamList } from '../../types/navigation';
import PerpsModifyActionSheet, {
type ModifyAction,
} from '../../components/PerpsModifyActionSheet';
import { usePerpsNavigation } from '../../hooks/usePerpsNavigation';
import { type BottomSheetRef } from '@metamask/design-system-react-native';
import { MetaMetricsEvents } from '../../../../../core/Analytics';
import { useAnalytics } from '../../../../hooks/useAnalytics/useAnalytics';
interface PerpsSelectModifyActionViewProps {
sheetRef?: React.RefObject<BottomSheetRef | null>;
position?: Position;
onClose?: () => void;
onReversePosition?: (position: Position) => void;
testID?: string;
}
const PerpsSelectModifyActionView: React.FC<
PerpsSelectModifyActionViewProps
> = ({
sheetRef: externalSheetRef,
position: positionProp,
onClose: onExternalClose,
onReversePosition,
testID,
}) => {
const navigation = useNavigation<AppNavigationProp>();
const route =
useRoute<RouteProp<PerpsNavigationParamList, 'PerpsSelectModifyAction'>>();
const { trackEvent, createEventBuilder } = useAnalytics();
// Support both props and route params
const position = positionProp || route.params?.position;
const internalSheetRef = useRef<BottomSheetRef>(null);
const sheetRef = externalSheetRef || internalSheetRef;
const { navigateToOrder, navigateToClosePosition } = usePerpsNavigation();
const handleClose = useCallback(() => {
if (externalSheetRef) {
onExternalClose?.();
} else {
navigation.goBack();
}
}, [navigation, externalSheetRef, onExternalClose]);
const handleActionSelect = useCallback(
(action: ModifyAction) => {
if (!position) return;
// Track UI interaction based on action type
const getInteractionType = () => {
switch (action) {
case 'add_to_position':
return PERPS_EVENT_VALUE.INTERACTION_TYPE.INCREASE_EXPOSURE;
case 'reduce_position':
return PERPS_EVENT_VALUE.INTERACTION_TYPE.REDUCE_EXPOSURE;
case 'flip_position':
return PERPS_EVENT_VALUE.INTERACTION_TYPE.FLIP_POSITION;
default:
return null;
}
};
const interactionType = getInteractionType();
if (interactionType) {
trackEvent(
createEventBuilder(MetaMetricsEvents.PERPS_UI_INTERACTION)
.addProperties({
[PERPS_EVENT_PROPERTY.INTERACTION_TYPE]: interactionType,
[PERPS_EVENT_PROPERTY.ASSET]: position.symbol,
[PERPS_EVENT_PROPERTY.SOURCE]:
PERPS_EVENT_VALUE.SOURCE.POSITION_SCREEN,
[PERPS_EVENT_PROPERTY.DIRECTION]:
parseFloat(position.size) > 0
? PERPS_EVENT_VALUE.DIRECTION.LONG
: PERPS_EVENT_VALUE.DIRECTION.SHORT,
})
.build(),
);
}
// Navigate BEFORE closing (prevents navigation loss from component unmounting)
switch (action) {
case 'add_to_position':
// Open trade screen in same direction with existing position context
{
const direction = parseFloat(position.size) > 0 ? 'long' : 'short';
navigateToOrder({
direction,
asset: position.symbol,
existingPosition: position, // Pass position to maintain leverage consistency
hideTPSL: true, // Hide TP/SL when adding to existing position
source: PERPS_EVENT_VALUE.SOURCE.POSITION_SCREEN,
});
}
break;
case 'reduce_position':
// Open close position screen
navigateToClosePosition(
position,
PERPS_EVENT_VALUE.SOURCE.POSITION_SCREEN,
);
break;
case 'flip_position':
// If parent provides onReversePosition callback, use it (shows confirmation sheet)
// Otherwise, navigate directly to order screen (legacy behavior)
if (onReversePosition) {
onReversePosition(position);
} else {
const oppositeDirection =
parseFloat(position.size) > 0 ? 'short' : 'long';
const positionSize = Math.abs(parseFloat(position.size));
const positionLeverage = position.leverage?.value;
navigateToOrder({
direction: oppositeDirection,
asset: position.symbol,
amount: positionSize.toString(),
leverage: positionLeverage,
source: PERPS_EVENT_VALUE.SOURCE.POSITION_SCREEN,
});
}
break;
}
// Close bottom sheet AFTER navigation is triggered
sheetRef.current?.onCloseBottomSheet(handleClose);
},
[
position,
navigateToOrder,
navigateToClosePosition,
onReversePosition,
sheetRef,
handleClose,
trackEvent,
createEventBuilder,
],
);
return (
<PerpsModifyActionSheet
onClose={handleClose}
position={position}
onActionSelect={handleActionSelect}
sheetRef={sheetRef}
testID={testID}
/>
);
};
export default PerpsSelectModifyActionView;