OpenStock is an open-source alternative to expensive market platforms. Track real-time prices, set personalized alerts, and explore detailed company insights — built openly, for everyone, forever free.
fix: keep chart indicators when toggling fullscreen
ravixalgorithm0e69274a8cee
diff --git a/components/TradingViewWidget.tsx b/components/TradingViewWidget.tsxindex 9640ed3..0176020 100644--- a/components/TradingViewWidget.tsx+++ b/components/TradingViewWidget.tsx@@ -1,7 +1,6 @@'use client';import React, { memo, useState, useEffect } from 'react';-import { createPortal } from 'react-dom';import useTradingViewWidget from "@/hooks/useTradingViewWidget";import { cn } from "@/lib/utils";import { Maximize2, Minimize2 } from 'lucide-react';@@ -14,16 +13,9 @@ interface TradingViewWidgetProps {allowExpand?: boolean;}+// Fullscreen resizes the same element in place: moving or re-creating the embed would reset the chart.const TradingViewWidget = ({ scriptUrl, config, height = 600, className, allowExpand = false }: TradingViewWidgetProps) => {const [isExpanded, setIsExpanded] = useState(false);- const [windowHeight, setWindowHeight] = useState(0);-- useEffect(() => {- setWindowHeight(window.innerHeight);- const handleResize = () => setWindowHeight(window.innerHeight);- window.addEventListener('resize', handleResize);- return () => window.removeEventListener('resize', handleResize);- }, []);useEffect(() => {if (!isExpanded) return;@@ -36,39 +28,32 @@ const TradingViewWidget = ({ scriptUrl, config, height = 600, className, allowEx};}, [isExpanded]);- const currentHeight = isExpanded ? windowHeight - 24 : height;-- const widgetConfig = {- ...config,- height: currentHeight,- width: "100%",- autosize: true,- };-- const containerRef = useTradingViewWidget(scriptUrl, widgetConfig, currentHeight);-- const widget = (- <div className={cn("tv-frame group", isExpanded && "fixed inset-3 z-50 shadow-[0_0_0_4px_var(--frame),0_24px_60px_oklch(0_0_0/0.6)]")}>- {allowExpand && (- <button- type="button"- onClick={() => setIsExpanded(!isExpanded)}- className={cn("icon-btn absolute top-2 right-2 z-10 bg-card/80", !isExpanded && "opacity-0 group-hover:opacity-100 focus-visible:opacity-100")}- aria-label={isExpanded ? "Exit full screen" : "Full screen"}- title={isExpanded ? "Exit full screen (Esc)" : "Full screen"}- >- {isExpanded ? <Minimize2 /> : <Maximize2 />}- </button>- )}-- <div className={cn('tradingview-widget-container', className)} ref={containerRef} style={{ height: currentHeight }}>- <div className="tradingview-widget-container__widget" style={{ height: currentHeight, width: "100%" }} />+ const containerRef = useTradingViewWidget(scriptUrl, { ...config, width: "100%", height: "100%", autosize: true });++ return (+ <>+ {isExpanded && <div className="fixed inset-0 z-50 bg-page/70 backdrop-blur-[3px]" onClick={() => setIsExpanded(false)} />}+ <div className={cn("tv-frame group", isExpanded && "fixed inset-3 z-50 shadow-[0_0_0_4px_var(--frame),0_24px_60px_oklch(0_0_0/0.6)]")}>+ {allowExpand && (+ <button+ type="button"+ onClick={() => setIsExpanded(!isExpanded)}+ className={cn("icon-btn absolute top-2 right-2 z-10 bg-card/80", !isExpanded && "opacity-0 group-hover:opacity-100 focus-visible:opacity-100")}+ aria-label={isExpanded ? "Exit full screen" : "Full screen"}+ title={isExpanded ? "Exit full screen (Esc)" : "Full screen"}+ >+ {isExpanded ? <Minimize2 /> : <Maximize2 />}+ </button>+ )}++ <div+ ref={containerRef}+ className={cn('tradingview-widget-container', className)}+ style={{ height: isExpanded ? '100%' : height, width: "100%" }}+ /></div>- </div>+ </>);-- // Full screen escapes the shell grid instead of fighting its overflow- return isExpanded ? createPortal(<><div className="fixed inset-0 z-50 bg-page/70 backdrop-blur-[3px]" />{widget}</>, document.body) : widget;}export default memo(TradingViewWidget);diff --git a/hooks/useTradingViewWidget.tsx b/hooks/useTradingViewWidget.tsxindex 2bfe03d..a35fe26 100644--- a/hooks/useTradingViewWidget.tsx+++ b/hooks/useTradingViewWidget.tsx@@ -1,36 +1,29 @@'use client';import { useEffect, useRef } from "react";-const useTradingViewWidget = (scriptUrl: string, config: Record<string, unknown>, height: number | string = 600) => {+// Mounts a TradingView embed once per script + config. Size changes (e.g. fullscreen) must not+// re-run this: re-creating the embed wipes indicators the user added.+const useTradingViewWidget = (scriptUrl: string, config: Record<string, unknown>) => {const containerRef = useRef<HTMLDivElement | null>(null);+ const serializedConfig = JSON.stringify(config);useEffect(() => {- if (!containerRef.current) return;+ const container = containerRef.current;+ if (!container) return;- // Clean up previous instance- containerRef.current.innerHTML = '';-- // Create wrapper with dynamic height support- // If autosize is true in config, we want 100% height/width- const isAutosize = config.autosize === true;- const styleHeight = isAutosize ? '100%' : `${height}px`;-- containerRef.current.innerHTML = `<div class="tradingview-widget-container__widget" style="width: 100%; height: ${styleHeight};"></div>`;+ container.innerHTML = '<div class="tradingview-widget-container__widget" style="width: 100%; height: 100%;"></div>';const script = document.createElement("script");script.src = scriptUrl;script.async = true;- script.innerHTML = JSON.stringify(config);-- containerRef.current.appendChild(script);+ script.innerHTML = serializedConfig;+ container.appendChild(script);return () => {- if (containerRef.current) {- containerRef.current.innerHTML = '';- }+ container.innerHTML = '';}- }, [scriptUrl, JSON.stringify(config), height]) // Use stringified config to avoid ref issues+ }, [scriptUrl, serializedConfig])return containerRef;}-export default useTradingViewWidget\ No newline at end of file+export default useTradingViewWidget