TradingViewChart.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use client';
  2. import { useEffect, useRef } from 'react';
  3. import useTheme from '@/hooks/useTheme';
  4. // TradingView 고급차트 임베드 — 세계지수 드릴다운(실시간 보완). 시세 표시 책임은 TradingView 측.
  5. // symbol 은 types/worldIndex.ts TV_SYMBOL 매핑값(예: "KRX:KOSPI", "TVC:NI225").
  6. type Props = {
  7. symbol: string;
  8. };
  9. export default function TradingViewChart({ symbol }: Props)
  10. {
  11. const containerRef = useRef<HTMLDivElement>(null);
  12. const { isDark } = useTheme();
  13. useEffect(() => {
  14. const container = containerRef.current;
  15. if (!container) {
  16. return;
  17. }
  18. container.innerHTML = '';
  19. const widget = document.createElement('div');
  20. widget.className = 'tradingview-widget-container__widget';
  21. widget.style.height = '100%';
  22. widget.style.width = '100%';
  23. container.appendChild(widget);
  24. const script = document.createElement('script');
  25. script.type = 'text/javascript';
  26. script.src = 'https://s3.tradingview.com/external-embedding/embed-widget-advanced-chart.js';
  27. script.async = true;
  28. script.innerHTML = JSON.stringify({
  29. symbol,
  30. autosize: true,
  31. interval: 'D',
  32. timezone: 'Asia/Seoul',
  33. theme: isDark ? 'dark' : 'light',
  34. style: '1',
  35. locale: 'kr',
  36. hide_side_toolbar: true,
  37. allow_symbol_change: false,
  38. calendar: false,
  39. support_host: 'https://www.tradingview.com'
  40. });
  41. container.appendChild(script);
  42. return () => {
  43. container.innerHTML = '';
  44. };
  45. }, [symbol, isDark]);
  46. return (
  47. <div className='tradingview-widget-container' ref={containerRef} style={{ height: '100%', width: '100%' }} />
  48. );
  49. }