Unverified Commit 56c6cfea authored by Lijiaoa's avatar Lijiaoa Committed by GitHub
Browse files

improve localStorage for better user experience (#5145)

parent b22f192c
...@@ -82,6 +82,8 @@ class App extends React.Component<{}, AppState> { ...@@ -82,6 +82,8 @@ class App extends React.Component<{}, AppState> {
} }
async componentDidMount(): Promise<void> { async componentDidMount(): Promise<void> {
localStorage.removeItem('columns');
localStorage.removeItem('paraColumns');
await Promise.all([EXPERIMENT.init(), TRIALS.init()]); await Promise.all([EXPERIMENT.init(), TRIALS.init()]);
this.setState(state => ({ this.setState(state => ({
experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1, experimentUpdateBroadcast: state.experimentUpdateBroadcast + 1,
......
import * as React from 'react'; import * as React from 'react';
import { Dialog, DialogType, DialogFooter, Checkbox, PrimaryButton, DefaultButton } from '@fluentui/react'; import { Dialog, DialogType, DialogFooter, Checkbox, PrimaryButton, DefaultButton } from '@fluentui/react';
import { EXPERIMENT } from '@static/datamodel';
import { Storage } from '@model/localStorage';
/** /**
* changeColumnComponent file is for [customized table column, customized hyper-parameter graph yAxis] * changeColumnComponent file is for [customized table column, customized hyper-parameter graph yAxis]
...@@ -67,9 +69,19 @@ class ChangeColumnComponent extends React.Component<ChangeColumnProps, ChangeCol ...@@ -67,9 +69,19 @@ class ChangeColumnComponent extends React.Component<ChangeColumnProps, ChangeCol
const selectedColumns = allColumns.map(column => column.key).filter(key => currentSelected.includes(key)); const selectedColumns = allColumns.map(column => column.key).filter(key => currentSelected.includes(key));
onSelectedChange(selectedColumns); onSelectedChange(selectedColumns);
if (whichComponent === 'table') { if (whichComponent === 'table') {
localStorage.setItem('columns', JSON.stringify(selectedColumns)); const storage = new Storage(
`${EXPERIMENT.profile.id}_columns`,
JSON.stringify(selectedColumns),
30 * 24 * 60 * 60 * 1000
);
storage.setValue();
} else { } else {
localStorage.setItem('paraColumns', JSON.stringify(selectedColumns)); const storage = new Storage(
`${EXPERIMENT.profile.id}_paraColumns`,
JSON.stringify(selectedColumns),
30 * 24 * 60 * 60 * 1000
);
storage.setValue();
} }
this.hideDialog(); this.hideDialog();
}; };
......
...@@ -8,6 +8,7 @@ import { SingleAxis, MultipleAxes } from '@static/interface'; ...@@ -8,6 +8,7 @@ import { SingleAxis, MultipleAxes } from '@static/interface';
import { Trial } from '@model/trial'; import { Trial } from '@model/trial';
import ChangeColumnComponent from '../ChangeColumnComponent'; import ChangeColumnComponent from '../ChangeColumnComponent';
import { optimizeModeValue } from './optimizeMode'; import { optimizeModeValue } from './optimizeMode';
import { getValue } from '@model/localStorage';
import 'parcoord-es/dist/parcoords.css'; import 'parcoord-es/dist/parcoords.css';
import '@style/button.scss'; import '@style/button.scss';
...@@ -56,9 +57,10 @@ class Para extends React.Component<ParaProps, ParaState> { ...@@ -56,9 +57,10 @@ class Para extends React.Component<ParaProps, ParaState> {
customizeColumnsDialogVisible: false, customizeColumnsDialogVisible: false,
availableDimensions: [], availableDimensions: [],
chosenDimensions: chosenDimensions:
localStorage.getItem('paraColumns') !== null localStorage.getItem(`${EXPERIMENT.profile.id}_paraColumns`) !== null &&
getValue(`${EXPERIMENT.profile.id}_paraColumns`) !== null
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
JSON.parse(localStorage.getItem('paraColumns')!) JSON.parse(getValue(`${EXPERIMENT.profile.id}_paraColumns`)!)
: [] : []
}; };
} }
......
...@@ -16,6 +16,7 @@ import { getTrialsBySearchFilters } from './tableFunction/search/searchFunction' ...@@ -16,6 +16,7 @@ import { getTrialsBySearchFilters } from './tableFunction/search/searchFunction'
import PaginationTable from '@components/common/PaginationTable'; import PaginationTable from '@components/common/PaginationTable';
import CopyButton from '@components/common/CopyButton'; import CopyButton from '@components/common/CopyButton';
import TooltipHostIndex from '@components/common/TooltipHostIndex'; import TooltipHostIndex from '@components/common/TooltipHostIndex';
import { getValue } from '@model/localStorage';
require('echarts/lib/chart/line'); require('echarts/lib/chart/line');
require('echarts/lib/component/tooltip'); require('echarts/lib/component/tooltip');
...@@ -54,9 +55,10 @@ class TableList extends React.Component<TableListProps, TableListState> { ...@@ -54,9 +55,10 @@ class TableList extends React.Component<TableListProps, TableListState> {
this.state = { this.state = {
displayedItems: [], displayedItems: [],
displayedColumns: displayedColumns:
localStorage.getItem('columns') !== null localStorage.getItem(`${EXPERIMENT.profile.id}_columns`) !== null &&
getValue(`${EXPERIMENT.profile.id}_columns`) !== null
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
JSON.parse(localStorage.getItem('columns')!) JSON.parse(getValue(`${EXPERIMENT.profile.id}_columns`)!)
: defaultDisplayedColumns, : defaultDisplayedColumns,
columns: [], columns: [],
searchType: 'id', searchType: 'id',
......
export interface StorageFormat {
expire: number;
time: number;
value: string;
}
export const getValue = (key): null | string => {
const val = localStorage.getItem(key);
if (!val) {
return null;
}
const data = JSON.parse(val) as StorageFormat;
if (Date.now() - data.time > data.expire) {
localStorage.removeItem(key);
return null;
}
return data.value;
};
class Storage {
key: string = '';
value: string = '';
expire: number = 0;
constructor(key: string, value: string, expire: number) {
this.key = key;
this.value = value;
this.expire = expire;
}
public setValue(): void {
const obj: StorageFormat = {
value: this.value,
time: Date.now(),
expire: this.expire
};
localStorage.setItem(this.key, JSON.stringify(obj));
}
}
export { Storage };
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment