Skip to content

Commit

Permalink
Merge pull request #1647 from vinu-deriv/Vinu/DTRA-2367/trackjs-error
Browse files Browse the repository at this point in the history
fix: fixed trackJs error while setting chart-layout in localStorage
  • Loading branch information
vinu-deriv authored Dec 9, 2024
2 parents 80af07d + 5385847 commit 4412a08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"html2canvas": "^1.4.1",
"lodash.debounce": "^4.0.8",
"lodash.set": "^4.3.2",
"lz-string": "^1.5.0",
"mobx": "^6.5.0",
"mobx-react-lite": "^3.4.0",
"moment": "^2.24.0",
Expand Down
36 changes: 25 additions & 11 deletions src/store/ChartState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import debounce from 'lodash.debounce';
import { AuditDetailsForExpiredContract, ProposalOpenContract } from '@deriv/api-types';
import { isDeepEqual } from 'src/utils/object';
import LZString from 'lz-string';
import MainStore from '.';
import Theme from '../../sass/_themes.scss';
import { STATE } from '../Constant';
Expand Down Expand Up @@ -461,16 +462,26 @@ class ChartState {
const layoutData: TLayout = this.mainStore.view.getLayout();
const id = this.mainStore.chart.chartId;

saveToLocalStorage(`chart-layout-${id}`, {
studyItems: layoutData.studyItems,
crosshair: layoutData.crosshair,
msPerPx: layoutData.msPerPx,
});
const layoutCompressedData = LZString.compress(
JSON.stringify({
studyItems: layoutData.studyItems,
crosshair: layoutData.crosshair,
msPerPx: layoutData.msPerPx,
})
);

saveToLocalStorage(`chart-layout-${id}`, layoutCompressedData);
}
// returns false if restoring layout fails
restoreLayout() {
const id = this.mainStore.chart.chartId;
const layout: TLayout = createObjectFromLocalStorage(`chart-layout-${id}`);
const compressedLayout = createObjectFromLocalStorage(`chart-layout-${id}`);
let layout: TLayout | null = null;
try {
layout = JSON.parse(LZString.decompress(compressedLayout ?? ''));
} catch (e) {
layout = compressedLayout;
}

if (!layout) {
this.clearLayout();
Expand All @@ -491,12 +502,15 @@ class ChartState {
if (!this.chartStore.chartId) return;
const layoutData: TLayout = this.mainStore.view.getLayout();
const id = this.mainStore.chart.chartId;
const layoutCompressedData = LZString.compress(
JSON.stringify({
crosshair: layoutData.crosshair,
studyItems: layoutData.studyItems,
msPerPx: layoutData.msPerPx,
})
);

saveToLocalStorage(`chart-layout-${id}`, {
crosshair: layoutData.crosshair,
studyItems: layoutData.studyItems,
msPerPx: layoutData.msPerPx,
});
saveToLocalStorage(`chart-layout-${id}`, layoutCompressedData);
}

cleanChart() {
Expand Down

0 comments on commit 4412a08

Please sign in to comment.