'use client'; import React, { memo, useState, useEffect } from 'react'; import useTradingViewWidget from "@/hooks/useTradingViewWidget"; import { cn } from "@/lib/utils"; import { Maximize2, Minimize2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; interface TradingViewWidgetProps { title?: string; scriptUrl: string; config: Record; height?: number; className?: string; allowExpand?: boolean; } const TradingViewWidget = ({ title, scriptUrl, config, height = 600, className, allowExpand = false }: TradingViewWidgetProps) => { const [isExpanded, setIsExpanded] = useState(false); const [windowHeight, setWindowHeight] = useState(0); useEffect(() => { if (typeof window !== 'undefined') { setWindowHeight(window.innerHeight); const handleResize = () => setWindowHeight(window.innerHeight); window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); } }, []); const currentHeight = isExpanded ? windowHeight : height; const widgetConfig = { ...config, height: currentHeight, width: "100%", autosize: true, }; const containerRef = useTradingViewWidget(scriptUrl, widgetConfig, currentHeight); const toggleExpand = () => { setIsExpanded(!isExpanded); }; return (
{title && !isExpanded &&

{title}

} {allowExpand && ( )}
); } export default memo(TradingViewWidget);