Skip to content

Commit

Permalink
fix: fixed trackJs error while setting chart-layout in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
vinu-deriv committed Nov 29, 2024
1 parent 80af07d commit 19005aa
Show file tree
Hide file tree
Showing 3 changed files with 30 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
31 changes: 20 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,21 @@ 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}`);
const layout: TLayout = JSON.parse(LZString.decompress(compressedLayout ?? undefined));

if (!layout) {
this.clearLayout();
Expand All @@ -491,12 +497,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 19005aa

Please sign in to comment.