forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrade-chart.tsx
194 lines (181 loc) · 7.6 KB
/
trade-chart.tsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import React from 'react';
import { ActiveSymbols, TickSpotData } from '@deriv/api-types';
import { useDevice } from '@deriv-com/ui';
import { ChartBarrierStore, isAccumulatorContract } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { useTraderStore } from 'Stores/useTraderStores';
import { SmartChart } from 'Modules/SmartChart';
import AccumulatorsChartElements from 'Modules/SmartChart/Components/Markers/accumulators-chart-elements';
import ToolbarWidgets from 'Modules/SmartChart/Components/toolbar-widgets';
import useActiveSymbols from 'AppV2/Hooks/useActiveSymbols';
type TBottomWidgetsParams = {
digits: number[];
tick: TickSpotData | null;
};
type TBottomWidgetsMobile = TBottomWidgetsParams & {
setDigitStats: (digits: number[]) => void;
setTickData: (tick: TickSpotData | null) => void;
};
const BottomWidgetsMobile = ({ digits, tick, setTickData, setDigitStats }: TBottomWidgetsMobile) => {
// Using bottom widgets in V2 to get tick data for all trade types and to get digit stats for Digit trade types
React.useEffect(() => {
setTickData(tick);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tick]);
React.useEffect(() => {
setDigitStats(digits);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [digits]);
// render no bottom widgets on chart
return null;
};
const TradeChart = observer(() => {
const { ui, common, contract_trade, portfolio } = useStore();
const { isMobile } = useDevice();
const {
accumulator_barriers_data,
accumulator_contract_barriers_data,
chart_type,
granularity,
has_crossed_accu_barriers,
markers_array,
updateChartType,
updateGranularity,
} = contract_trade;
const ref = React.useRef<{ hasPredictionIndicators(): void; triggerPopup(arg: () => void): void }>(null);
const { all_positions } = portfolio;
const { is_chart_countdown_visible, is_chart_layout_default, is_dark_mode_on, is_positions_drawer_on } = ui;
const { current_language, is_socket_opened } = common;
const { default_symbol, activeSymbols: active_symbols } = useActiveSymbols();
const {
barriers_flattened: extra_barriers,
chartStateChange,
chart_layout,
contract_type,
exportLayout,
has_alternative_source,
has_barrier,
main_barrier_flattened: main_barrier,
setChartStatus,
setDigitStats,
setTickData,
show_digits_stats,
symbol: symbol_from_store,
onChange,
prev_contract_type,
wsForget,
wsForgetStream,
wsSendRequest,
wsSubscribe,
} = useTraderStore();
const symbol = symbol_from_store ?? default_symbol;
const is_accumulator = isAccumulatorContract(contract_type);
const settings = {
countdown: is_chart_countdown_visible,
isHighestLowestMarkerEnabled: false, // TODO: Pending UI,
language: current_language.toLowerCase(),
position: is_chart_layout_default ? 'bottom' : 'left',
theme: is_dark_mode_on ? 'dark' : 'light',
...(is_accumulator ? { whitespace: 190, minimumLeftBars: isMobile ? 3 : undefined } : {}),
...(has_barrier ? { whitespace: 110 } : {}),
};
const { current_spot, current_spot_time } = accumulator_barriers_data || {};
const bottomWidgets = React.useCallback(({ digits, tick }: TBottomWidgetsParams) => {
return (
<BottomWidgetsMobile digits={digits} tick={tick} setTickData={setTickData} setDigitStats={setDigitStats} />
);
}, []);
React.useEffect(() => {
if ((is_accumulator || show_digits_stats) && ref.current?.hasPredictionIndicators()) {
const cancelCallback = () => onChange({ target: { name: 'contract_type', value: prev_contract_type } });
ref.current?.triggerPopup(cancelCallback);
}
}, [is_accumulator, onChange, prev_contract_type, show_digits_stats]);
const getMarketsOrder = (active_symbols: ActiveSymbols): string[] => {
const synthetic_index = 'synthetic_index';
const has_synthetic_index = active_symbols.some(s => s.market === synthetic_index);
return active_symbols
.slice()
.sort((a, b) => (a.display_name < b.display_name ? -1 : 1))
.map(s => s.market)
.reduce(
(arr, market) => {
if (arr.indexOf(market) === -1) arr.push(market);
return arr;
},
has_synthetic_index ? [synthetic_index] : []
);
};
const barriers: ChartBarrierStore[] = main_barrier ? [main_barrier, ...extra_barriers] : extra_barriers;
// max ticks to display for mobile view for tick chart
const max_ticks = granularity === 0 ? 8 : 24;
if (!symbol || !active_symbols.length) return null;
return (
<SmartChart
ref={ref}
barriers={barriers}
contracts_array={markers_array}
bottomWidgets={bottomWidgets}
crosshair={isMobile ? 0 : undefined}
crosshairTooltipLeftAllow={560}
showLastDigitStats
chartControlsWidgets={null}
chartStatusListener={(v: boolean) => setChartStatus(!v, true)}
chartType={chart_type}
initialData={{
activeSymbols: JSON.parse(JSON.stringify(active_symbols)),
}}
chartData={{
activeSymbols: JSON.parse(JSON.stringify(active_symbols)),
}}
feedCall={{
activeSymbols: false,
}}
enabledNavigationWidget={!isMobile}
enabledChartFooter={false}
id='trade'
isMobile={isMobile}
isVerticalScrollEnabled={false}
maxTick={isMobile ? max_ticks : undefined}
granularity={show_digits_stats || is_accumulator ? 0 : granularity}
requestAPI={wsSendRequest}
requestForget={wsForget}
requestForgetStream={wsForgetStream}
requestSubscribe={wsSubscribe}
settings={settings}
allowTickChartTypeOnly={show_digits_stats || is_accumulator}
stateChangeListener={chartStateChange}
symbol={symbol}
topWidgets={() => <div /> /* to hide the original chart market dropdown */}
isConnectionOpened={is_socket_opened}
clearChart={false}
toolbarWidget={() => {
return <ToolbarWidgets updateChartType={updateChartType} updateGranularity={updateGranularity} />;
}}
importedLayout={chart_layout}
onExportLayout={exportLayout}
shouldFetchTradingTimes={false}
hasAlternativeSource={has_alternative_source}
getMarketsOrder={getMarketsOrder}
should_zoom_out_on_yaxis={is_accumulator}
yAxisMargin={{
top: isMobile ? 76 : 106,
}}
isLive
leftMargin={!isMobile && is_positions_drawer_on ? 328 : 80}
>
{is_accumulator && (
<AccumulatorsChartElements
all_positions={all_positions}
current_spot={current_spot}
current_spot_time={current_spot_time}
has_crossed_accu_barriers={has_crossed_accu_barriers}
should_show_profit_text={!!accumulator_contract_barriers_data.accumulators_high_barrier}
symbol={symbol}
is_mobile={isMobile}
/>
)}
</SmartChart>
);
});
export default TradeChart;