fix(trading): stored state causing wrong chart data (#5928)

This commit is contained in:
Matthew Russell 2024-03-05 18:17:18 +00:00 committed by GitHub
parent 7ea7362a7d
commit 654dd1e7b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 2 additions and 41 deletions

View File

@ -30,8 +30,6 @@ export const ChartContainer = ({ marketId }: { marketId: string }) => {
setStudies, setStudies,
setStudySizes, setStudySizes,
setOverlays, setOverlays,
state,
setState,
} = useChartSettings(); } = useChartSettings();
const pennantChart = ( const pennantChart = (
@ -68,10 +66,6 @@ export const ChartContainer = ({ marketId }: { marketId: string }) => {
onIntervalChange={(newInterval) => { onIntervalChange={(newInterval) => {
setInterval(fromTradingViewResolution(newInterval)); setInterval(fromTradingViewResolution(newInterval));
}} }}
onAutoSaveNeeded={(data) => {
setState(data);
}}
state={state}
/> />
); );
} }

View File

@ -9,7 +9,6 @@ type StudySizes = { [S in Study]?: number };
export type Chartlib = 'pennant' | 'tradingview'; export type Chartlib = 'pennant' | 'tradingview';
interface StoredSettings { interface StoredSettings {
state: object | undefined; // Don't see a better type provided from TradingView type definitions
chartlib: Chartlib; chartlib: Chartlib;
// For interval we use the enum from @vegaprotocol/types, this is to make mapping between different // For interval we use the enum from @vegaprotocol/types, this is to make mapping between different
// chart types easier and more consistent // chart types easier and more consistent
@ -30,7 +29,6 @@ const STUDY_ORDER: Study[] = [
]; ];
export const DEFAULT_CHART_SETTINGS = { export const DEFAULT_CHART_SETTINGS = {
state: undefined,
chartlib: 'pennant' as const, chartlib: 'pennant' as const,
interval: Interval.INTERVAL_I15M, interval: Interval.INTERVAL_I15M,
type: ChartType.CANDLE, type: ChartType.CANDLE,
@ -47,7 +45,6 @@ export const useChartSettingsStore = create<
setStudies: (studies?: Study[]) => void; setStudies: (studies?: Study[]) => void;
setStudySizes: (sizes: number[]) => void; setStudySizes: (sizes: number[]) => void;
setChartlib: (lib: Chartlib) => void; setChartlib: (lib: Chartlib) => void;
setState: (state: object) => void;
} }
>()( >()(
persist( persist(
@ -95,9 +92,6 @@ export const useChartSettingsStore = create<
state.chartlib = lib; state.chartlib = lib;
}); });
}, },
setState: (state) => {
set({ state });
},
})), })),
{ {
name: 'vega_candles_chart_store', name: 'vega_candles_chart_store',
@ -151,7 +145,5 @@ export const useChartSettings = () => {
setOverlays: settings.setOverlays, setOverlays: settings.setOverlays,
setStudySizes: settings.setStudySizes, setStudySizes: settings.setStudySizes,
setChartlib: settings.setChartlib, setChartlib: settings.setChartlib,
state: settings.state,
setState: settings.setState,
}; };
}; };

View File

@ -1,7 +1,7 @@
import { useScript } from '@vegaprotocol/react-helpers'; import { useScript } from '@vegaprotocol/react-helpers';
import { Splash } from '@vegaprotocol/ui-toolkit'; import { Splash } from '@vegaprotocol/ui-toolkit';
import { useT } from './use-t'; import { useT } from './use-t';
import { TradingView, type OnAutoSaveNeededCallback } from './trading-view'; import { TradingView } from './trading-view';
import { CHARTING_LIBRARY_FILE, type ResolutionString } from './constants'; import { CHARTING_LIBRARY_FILE, type ResolutionString } from './constants';
export const TradingViewContainer = ({ export const TradingViewContainer = ({
@ -10,16 +10,12 @@ export const TradingViewContainer = ({
marketId, marketId,
interval, interval,
onIntervalChange, onIntervalChange,
onAutoSaveNeeded,
state,
}: { }: {
libraryPath: string; libraryPath: string;
libraryHash: string; libraryHash: string;
marketId: string; marketId: string;
interval: ResolutionString; interval: ResolutionString;
onIntervalChange: (interval: string) => void; onIntervalChange: (interval: string) => void;
onAutoSaveNeeded: OnAutoSaveNeededCallback;
state: object | undefined;
}) => { }) => {
const t = useT(); const t = useT();
const scriptState = useScript( const scriptState = useScript(
@ -49,8 +45,6 @@ export const TradingViewContainer = ({
marketId={marketId} marketId={marketId}
interval={interval} interval={interval}
onIntervalChange={onIntervalChange} onIntervalChange={onIntervalChange}
onAutoSaveNeeded={onAutoSaveNeeded}
state={state}
/> />
); );
}; };

View File

@ -25,15 +25,11 @@ export const TradingView = ({
libraryPath, libraryPath,
interval, interval,
onIntervalChange, onIntervalChange,
onAutoSaveNeeded,
state,
}: { }: {
marketId: string; marketId: string;
libraryPath: string; libraryPath: string;
interval: ResolutionString; interval: ResolutionString;
onIntervalChange: (interval: string) => void; onIntervalChange: (interval: string) => void;
onAutoSaveNeeded: OnAutoSaveNeededCallback;
state: object | undefined;
}) => { }) => {
const { isMobile } = useScreenDimensions(); const { isMobile } = useScreenDimensions();
const { theme } = useThemeSwitcher(); const { theme } = useThemeSwitcher();
@ -108,7 +104,6 @@ export const TradingView = ({
backgroundColor: overrides['paneProperties.background'], backgroundColor: overrides['paneProperties.background'],
}, },
auto_save_delay: 1, auto_save_delay: 1,
saved_data: state,
}; };
widgetRef.current = new window.TradingView.widget(widgetOptions); widgetRef.current = new window.TradingView.widget(widgetOptions);
@ -117,25 +112,12 @@ export const TradingView = ({
if (!widgetRef.current) return; if (!widgetRef.current) return;
const activeChart = widgetRef.current.activeChart(); const activeChart = widgetRef.current.activeChart();
if (!state) {
// If chart has loaded with no state, create a volume study
activeChart.createStudy('Volume'); activeChart.createStudy('Volume');
}
// Subscribe to interval changes so it can be persisted in chart settings // Subscribe to interval changes so it can be persisted in chart settings
activeChart.onIntervalChanged().subscribe(null, onIntervalChange); activeChart.onIntervalChanged().subscribe(null, onIntervalChange);
}); });
widgetRef.current.subscribe('onAutoSaveNeeded', () => {
if (!widgetRef.current) return;
widgetRef.current.save((newState) => {
onAutoSaveNeeded(newState);
});
});
}, [ }, [
state,
datafeed, datafeed,
interval, interval,
prevTheme, prevTheme,
@ -145,7 +127,6 @@ export const TradingView = ({
language, language,
libraryPath, libraryPath,
isMobile, isMobile,
onAutoSaveNeeded,
onIntervalChange, onIntervalChange,
]); ]);