fix(trading): trading view on multiple tabs (#5748)

This commit is contained in:
Art 2024-02-06 12:31:12 +01:00 committed by GitHub
parent b4e98e285e
commit 75e7cea32a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 6 deletions

View File

@ -41,16 +41,17 @@ export const TradingView = ({
const chartContainerRef = useRef<HTMLDivElement>(null); const chartContainerRef = useRef<HTMLDivElement>(null);
const widgetRef = useRef<IChartingLibraryWidget>(); const widgetRef = useRef<IChartingLibraryWidget>();
const datafeed = useDatafeed();
const prevMarketId = usePrevious(marketId); const prevMarketId = usePrevious(marketId);
const prevTheme = usePrevious(theme); const prevTheme = usePrevious(theme);
const datafeed = useDatafeed(marketId);
useEffect(() => { useEffect(() => {
// Widget already created // Widget already created
if (widgetRef.current !== undefined) { if (widgetRef.current !== undefined) {
// Update the symbol if changed // Update the symbol if changed
if (marketId !== prevMarketId) { if (marketId !== prevMarketId) {
datafeed.setSymbol(marketId);
widgetRef.current.setSymbol( widgetRef.current.setSymbol(
marketId, marketId,
(interval ? interval : '15') as TVResolutionString, (interval ? interval : '15') as TVResolutionString,

View File

@ -44,14 +44,22 @@ const configurationData: DatafeedConfiguration = {
supported_resolutions: supportedResolutions as ResolutionString[], supported_resolutions: supportedResolutions as ResolutionString[],
} as const; } as const;
export const useDatafeed = () => { // HACK: local handle for market id
let requestedSymbol: string | undefined = undefined;
export const useDatafeed = (marketId: string) => {
const hasHistory = useRef(false); const hasHistory = useRef(false);
const subRef = useRef<Subscription>(); const subRef = useRef<Subscription>();
const client = useApolloClient(); const client = useApolloClient();
const datafeed = useMemo(() => { const datafeed = useMemo(() => {
const feed: IBasicDataFeed = { const feed: IBasicDataFeed & { setSymbol: (symbol: string) => void } = {
setSymbol: (symbol: string) => {
// re-setting the symbol so it could be consumed by `resolveSymbol`
requestedSymbol = symbol;
},
onReady: (callback) => { onReady: (callback) => {
requestedSymbol = marketId;
setTimeout(() => callback(configurationData)); setTimeout(() => callback(configurationData));
}, },
@ -68,7 +76,7 @@ export const useDatafeed = () => {
const result = await client.query<SymbolQuery, SymbolQueryVariables>({ const result = await client.query<SymbolQuery, SymbolQueryVariables>({
query: SymbolDocument, query: SymbolDocument,
variables: { variables: {
marketId, marketId: requestedSymbol || marketId,
}, },
}); });
@ -242,7 +250,7 @@ export const useDatafeed = () => {
}; };
return feed; return feed;
}, [client]); }, [client, marketId]);
useEffect(() => { useEffect(() => {
return () => { return () => {