Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed trackJs error while setting chart-layout in localStorage #1647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading