1
1
// TradingViewWidget.jsx
2
- import { useEffect , useRef , memo } from "react" ;
2
+ import { getAgent } from "@kite9/fdc3" ;
3
+ import { useEffect , useRef , memo , useState } from "react" ;
3
4
4
- export function TradingViewWidget ( ) {
5
+ export const TradingViewWidget = ( ) => {
5
6
const container : any = useRef ( ) ;
6
7
8
+ const [ state , setState ] = useState ( "MSFT" ) ;
9
+
10
+ useEffect ( ( ) => {
11
+ getAgent ( ) . then ( ( fdc3 ) => {
12
+ fdc3 . addIntentListener ( "ViewChart" , async ( context ) => {
13
+ setState ( context ?. id ?. ticker ) ;
14
+ } ) ;
15
+ } ) ;
16
+ } , [ ] ) ;
17
+
7
18
useEffect ( ( ) => {
8
- const script = document . createElement ( "script" ) ;
19
+ var script : HTMLScriptElement | null = null ;
20
+
21
+ script = document . getElementById (
22
+ "tradingview-widget-script" ,
23
+ ) as HTMLScriptElement ;
24
+
25
+ if ( script ) {
26
+ container . current . removeChild ( script ) ;
27
+ }
28
+
29
+ script = document . createElement ( "script" ) ;
30
+ container . current . appendChild ( script ) ;
31
+
32
+ script ! ! . id = "tradingview-widget-script" ;
9
33
script . src =
10
34
"https://s3.tradingview.com/external-embedding/embed-widget-advanced-chart.js" ;
11
35
script . type = "text/javascript" ;
12
36
script . async = true ;
13
37
script . innerHTML = `
14
38
{
15
39
"autosize": true,
16
- "symbol": "NASDAQ:AAPL ",
40
+ "symbol": "NASDAQ:${ state } ",
17
41
"interval": "D",
18
42
"timezone": "Etc/UTC",
19
43
"theme": "light",
20
44
"style": "1",
21
45
"locale": "en",
22
- "allow_symbol_change": true ,
46
+ "allow_symbol_change": false ,
23
47
"calendar": false,
24
48
"support_host": "https://www.tradingview.com"
25
49
}` ;
26
- container . current . appendChild ( script ) ;
27
- } , [ ] ) ;
50
+ } , [ state ] ) ;
28
51
29
52
return (
30
53
< div
@@ -47,6 +70,6 @@ export function TradingViewWidget() {
47
70
</ div >
48
71
</ div >
49
72
) ;
50
- }
73
+ } ;
51
74
52
75
export default memo ( TradingViewWidget ) ;
0 commit comments