-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.tsx
executable file
·153 lines (141 loc) · 5.65 KB
/
index.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
import { useEffect, useRef } from 'react'
import { useTheme } from 'next-themes'
import {
widget,
ChartingLibraryWidgetOptions,
IChartingLibraryWidget,
ResolutionString,
} from '../../public/charting_library' // Make sure to follow step 1 of the README
// import { useMarket } from '../../utils/markets';
import { CHART_DATA_FEED } from '../../utils/chartDataConnector'
import useMangoStore from '../../stores/useMangoStore'
// This is a basic example of how to create a TV widget
// You can add more feature such as storing charts in localStorage
export interface ChartContainerProps {
symbol: ChartingLibraryWidgetOptions['symbol']
interval: ChartingLibraryWidgetOptions['interval']
datafeedUrl: string
libraryPath: ChartingLibraryWidgetOptions['library_path']
chartsStorageUrl: ChartingLibraryWidgetOptions['charts_storage_url']
chartsStorageApiVersion: ChartingLibraryWidgetOptions['charts_storage_api_version']
clientId: ChartingLibraryWidgetOptions['client_id']
userId: ChartingLibraryWidgetOptions['user_id']
fullscreen: ChartingLibraryWidgetOptions['fullscreen']
autosize: ChartingLibraryWidgetOptions['autosize']
studiesOverrides: ChartingLibraryWidgetOptions['studies_overrides']
containerId: ChartingLibraryWidgetOptions['container_id']
theme: string
}
// export interface ChartContainerState {}
const TVChartContainer = () => {
const selectedMarketName = useMangoStore((s) => s.selectedMarket.name)
const { theme } = useTheme()
// @ts-ignore
const defaultProps: ChartContainerProps = {
symbol: selectedMarketName,
interval: '60' as ResolutionString,
theme: 'Dark',
containerId: 'tv_chart_container',
datafeedUrl: CHART_DATA_FEED,
libraryPath: '/charting_library/',
fullscreen: false,
autosize: true,
studiesOverrides: {
'volume.volume.color.0': theme === 'Mango' ? '#E54033' : '#CC2929',
'volume.volume.color.1': theme === 'Mango' ? '#AFD803' : '#5EBF4D',
},
}
const tvWidgetRef = useRef<IChartingLibraryWidget | null>(null)
// TODO: fetch market from store and wire up to chart
// const { market, marketName } = useMarket()
useEffect(() => {
const widgetOptions: ChartingLibraryWidgetOptions = {
symbol: selectedMarketName,
// BEWARE: no trailing slash is expected in feed URL
// tslint:disable-next-line:no-any
datafeed: new (window as any).Datafeeds.UDFCompatibleDatafeed(
defaultProps.datafeedUrl
),
interval:
defaultProps.interval as ChartingLibraryWidgetOptions['interval'],
container_id:
defaultProps.containerId as ChartingLibraryWidgetOptions['container_id'],
library_path: defaultProps.libraryPath as string,
locale: 'en',
disabled_features: [
'use_localstorage_for_settings',
'timeframes_toolbar',
// 'volume_force_overlay',
// 'left_toolbar',
'show_logo_on_all_charts',
'caption_buttons_text_if_possible',
'header_settings',
'header_chart_type',
'header_compare',
'compare_symbol',
'header_screenshot',
// 'header_widget_dom_node',
'header_saveload',
'header_undo_redo',
'header_interval_dialog_button',
'show_interval_dialog_on_key_press',
'header_symbol_search',
// 'header_resolutions',
// 'header_widget',
],
load_last_chart: true,
client_id: defaultProps.clientId,
user_id: defaultProps.userId,
fullscreen: defaultProps.fullscreen,
autosize: defaultProps.autosize,
studies_overrides: defaultProps.studiesOverrides,
theme: theme === 'Light' ? 'Light' : 'Dark',
custom_css_url: '/tradingview-chart.css',
loading_screen: { backgroundColor: 'rgba(0,0,0,0.1)' },
overrides: {
'paneProperties.background':
theme === 'Dark' ? '#2B2B2B' : theme === 'Light' ? '#fff' : '#1D1832',
'mainSeriesProperties.candleStyle.upColor':
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
'mainSeriesProperties.candleStyle.downColor':
theme === 'Mango' ? '#E54033' : '#CC2929',
'mainSeriesProperties.candleStyle.drawWick': true,
'mainSeriesProperties.candleStyle.drawBorder': true,
'mainSeriesProperties.candleStyle.borderColor':
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
'mainSeriesProperties.candleStyle.borderUpColor':
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
'mainSeriesProperties.candleStyle.borderDownColor':
theme === 'Mango' ? '#E54033' : '#CC2929',
'mainSeriesProperties.candleStyle.wickUpColor':
theme === 'Mango' ? '#AFD803' : '#5EBF4D',
'mainSeriesProperties.candleStyle.wickDownColor':
theme === 'Mango' ? '#E54033' : '#CC2929',
},
}
const tvWidget = new widget(widgetOptions)
tvWidgetRef.current = tvWidget
tvWidget.onChartReady(() => {
// tvWidget.headerReady().then(() => {
// const button = tvWidget.createButton()
// button.setAttribute('title', 'Click to show a notification popup')
// button.classList.add('apply-common-tooltip')
// button.addEventListener('click', () =>
// tvWidget.showNoticeDialog({
// title: 'Notification',
// body: 'TradingView Charting Library API works correctly',
// callback: () => {
// // console.log('It works!!');
// },
// })
// )
// button.innerHTML = 'Check API'
// })
})
//eslint-disable-next-line
}, [selectedMarketName, theme])
// TODO: add market back to dep array
// }, [market])
return <div id={defaultProps.containerId} className="tradingview-chart" />
}
export default TVChartContainer