Fix chart: lazy load lightweight-charts on client only
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useState, useEffect, useRef } from 'react'
|
import { useState, useEffect, useRef } from 'react'
|
||||||
import { createChart, IChartApi, ISeriesApi, CandlestickData, Time } from 'lightweight-charts'
|
|
||||||
|
// Lazy load lightweight-charts only on client
|
||||||
|
let createChart: any = null
|
||||||
|
let lightweightCharts: any = null
|
||||||
|
|
||||||
interface Trade {
|
interface Trade {
|
||||||
id: string
|
id: string
|
||||||
@@ -87,10 +90,30 @@ export default function TradingChart() {
|
|||||||
const [indicators, setIndicators] = useState<IndicatorState>({
|
const [indicators, setIndicators] = useState<IndicatorState>({
|
||||||
ema20: false, ema50: false, ema200: false, bb: false, rsi: false, macd: false, thoth: true, volume: true, srZones: true, news: false, patterns: false, fib: false, countdown: false, calendar: false, correlation: false, funding: false
|
ema20: false, ema50: false, ema200: false, bb: false, rsi: false, macd: false, thoth: true, volume: true, srZones: true, news: false, patterns: false, fib: false, countdown: false, calendar: false, correlation: false, funding: false
|
||||||
})
|
})
|
||||||
|
const [chartReady, setChartReady] = useState(false)
|
||||||
|
|
||||||
// Initialize chart
|
// Initialize chart - run only on client
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!chartContainerRef.current) return
|
let mounted = true
|
||||||
|
|
||||||
|
const initChart = async () => {
|
||||||
|
try {
|
||||||
|
const charts = await import('lightweight-charts')
|
||||||
|
if (!mounted) return
|
||||||
|
createChart = charts.createChart
|
||||||
|
setChartReady(true)
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to load charts:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initChart()
|
||||||
|
return () => { mounted = false }
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Setup chart after library loads
|
||||||
|
useEffect(() => {
|
||||||
|
if (!chartReady || !chartContainerRef.current || !createChart) return
|
||||||
|
|
||||||
const chart = createChart(chartContainerRef.current, {
|
const chart = createChart(chartContainerRef.current, {
|
||||||
layout: {
|
layout: {
|
||||||
@@ -145,9 +168,9 @@ export default function TradingChart() {
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('resize', handleResize)
|
window.removeEventListener('resize', handleResize)
|
||||||
chart.remove()
|
if (chartRef.current) chartRef.current.remove()
|
||||||
}
|
}
|
||||||
}, [])
|
}, [chartReady])
|
||||||
|
|
||||||
// Fetch data when asset/timeframe changes
|
// Fetch data when asset/timeframe changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user