-
Notifications
You must be signed in to change notification settings - Fork 0
/
useTradingView.ts
40 lines (33 loc) · 1.09 KB
/
useTradingView.ts
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
import { ChartingLibraryWidgetOptions } from "public/lib/trading_view/charting_library/charting_library";
import { useEffect } from "react";
import { useTradingViewType } from "src/globalTypes";
// import { widget } from "../../public/trading_view/charting_library/charting_library.esm";
import { widget } from "../../public/lib/trading_view/charting_library";
export const useTradingView = ({
widgetOptions,
depsArr,
applyOverrides,
}: useTradingViewType) => {
const chartInit = (config: ChartingLibraryWidgetOptions) => {
const tvWidget = new widget(config);
// console.log("tv: chartInit ", tvWidget);
window.tvWidget = tvWidget;
window.tvWidget.onChartReady(() => {
// console.log("tv: onChartReady ");
window.tvWidget?.applyOverrides(applyOverrides || {});
});
};
// on mount
useEffect(() => {
window.tvWidget = null;
chartInit(widgetOptions);
// componentWillUnmount
return () => {
if (window.tvWidget !== null) {
window.tvWidget?.remove();
window.tvWidget = null;
}
};
}, depsArr);
return { depsArr };
};