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

Prefix local storage access with user email address #4217

Merged
merged 5 commits into from
Aug 7, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ For upgrade instructions, please check the [migration guide](MIGRATIONS.md).
- Fixed a bug where for uint24 color layers, scrambled data was shown for missing magnifications. [#4188](https://github.com/scalableminds/webknossos/pull/4188)
- Fixed a bug where collapsing/expanding all tree groups would trigger when toggling a single tree [#4178](https://github.com/scalableminds/webknossos/pull/4178)
- Fixed performance for time logging. [#4196](https://github.com/scalableminds/webknossos/pull/4196)
- Personal tracing layouts are saved per user now. [#4217](https://github.com/scalableminds/webknossos/pull/4217)
- Fixed an error message when quickly resizing the browser window. [#4205](https://github.com/scalableminds/webknossos/pull/4205)

### Removed
Expand Down
5 changes: 3 additions & 2 deletions frontend/javascripts/dashboard/dashboard_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DatasetView from "dashboard/dataset_view";
import ExplorativeAnnotationsView from "dashboard/explorative_annotations_view";
import NmlUploadZoneContainer from "oxalis/view/nml_upload_zone_container";
import Request from "libs/request";
import UserLocalStorage from "libs/user_local_storage";

const { TabPane } = Tabs;

Expand Down Expand Up @@ -45,7 +46,7 @@ class DashboardView extends React.PureComponent<PropsWithRouter, State> {

const validTabKeys = this.getValidTabKeys();
const { initialTabKey } = this.props;
const lastUsedTabKey = localStorage.getItem("lastUsedDashboardTab");
const lastUsedTabKey = UserLocalStorage.getItem("lastUsedDashboardTab");
const defaultTabKey = this.props.isAdminView ? "tasks" : "datasets";

// Flow doesn't allow validTabKeys[key] where key may be null, so check that first
Expand Down Expand Up @@ -132,7 +133,7 @@ class DashboardView extends React.PureComponent<PropsWithRouter, State> {
const tabKeyToURLMap = _.invert(urlTokenToTabKeyMap);
const url = tabKeyToURLMap[activeTabKey];
if (url) {
localStorage.setItem("lastUsedDashboardTab", activeTabKey);
UserLocalStorage.setItem("lastUsedDashboardTab", activeTabKey);
if (!this.props.isAdminView) {
this.props.history.push(`/dashboard/${url}`);
}
Expand Down
7 changes: 4 additions & 3 deletions frontend/javascripts/dashboard/dataset_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Persistence from "libs/persistence";
import SampleDatasetsModal from "dashboard/dataset/sample_datasets_modal";
import * as Utils from "libs/utils";
import renderIndependently from "libs/render_independently";
import UserLocalStorage from "libs/user_local_storage";

const { Search, Group: InputGroup } = Input;

Expand Down Expand Up @@ -46,13 +47,13 @@ const persistence: Persistence<State> = new Persistence(
export const wkDatasetsCacheKey = "wk.datasets";
export const datasetCache = {
set(datasets: APIMaybeUnimportedDataset[]): void {
localStorage.setItem(wkDatasetsCacheKey, JSON.stringify(datasets));
UserLocalStorage.setItem(wkDatasetsCacheKey, JSON.stringify(datasets));
},
get(): APIMaybeUnimportedDataset[] {
return Utils.parseAsMaybe(localStorage.getItem(wkDatasetsCacheKey)).getOrElse([]);
return Utils.parseAsMaybe(UserLocalStorage.getItem(wkDatasetsCacheKey)).getOrElse([]);
},
clear(): void {
localStorage.removeItem(wkDatasetsCacheKey);
UserLocalStorage.removeItem(wkDatasetsCacheKey);
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Toast from "libs/toast";
import * as Utils from "libs/utils";
import messages from "messages";
import { trackAction } from "oxalis/model/helpers/analytics";
import UserLocalStorage from "libs/user_local_storage";

const { Column } = Table;
const { Search } = Input;
Expand Down Expand Up @@ -99,7 +100,7 @@ class ExplorativeAnnotationsView extends React.PureComponent<Props, State> {

restoreSearchTags() {
// restore the search query tags from the last session
const searchTagString = localStorage.getItem("lastDashboardSearchTags");
const searchTagString = UserLocalStorage.getItem("lastDashboardSearchTags");
if (searchTagString) {
try {
const searchTags = JSON.parse(searchTagString);
Expand Down Expand Up @@ -297,7 +298,7 @@ class ExplorativeAnnotationsView extends React.PureComponent<Props, State> {
if (!this.state.tags.includes(tag)) {
this.setState(prevState => {
const newTags = update(prevState.tags, { $push: [tag] });
localStorage.setItem("lastDashboardSearchTags", JSON.stringify(newTags));
UserLocalStorage.setItem("lastDashboardSearchTags", JSON.stringify(newTags));
return { tags: newTags };
});
}
Expand All @@ -306,7 +307,7 @@ class ExplorativeAnnotationsView extends React.PureComponent<Props, State> {
removeTagFromSearch = (tag: string): void => {
this.setState(prevState => {
const newTags = prevState.tags.filter(t => t !== tag);
localStorage.setItem("lastDashboardSearchTags", JSON.stringify(newTags));
UserLocalStorage.setItem("lastDashboardSearchTags", JSON.stringify(newTags));
return { tags: newTags };
});
};
Expand Down
29 changes: 29 additions & 0 deletions frontend/javascripts/libs/user_local_storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @flow

import Store from "oxalis/store";

function prefixKey(key) {
const { activeUser } = Store.getState();
const prefix = !activeUser ? "Anonymous" : activeUser.email;
return `${prefix}-${key}`;
}

const UserLocalStorage = {
getItem(key: string): ?string {
return localStorage.getItem(prefixKey(key));
},

setItem(key: string, value: string): void {
return localStorage.setItem(prefixKey(key), value);
},

removeItem(key: string): void {
return localStorage.removeItem(prefixKey(key));
},

clear(): void {
localStorage.clear();
},
};

export default UserLocalStorage;
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ class AddNewLayoutModal extends React.PureComponent<Props, State> {
value: "",
};

onConfirm = () => {
const value = this.state.value;
this.setState({ value: "" });
this.props.addLayout(value);
};

render() {
return (
<Modal
title="Add a new layout"
visible={this.props.visible}
onOk={() => {
const value = this.state.value;
this.setState({ value: "" });
this.props.addLayout(value);
}}
onOk={this.onConfirm}
onCancel={this.props.onCancel}
>
<Input
Expand All @@ -36,6 +38,8 @@ class AddNewLayoutModal extends React.PureComponent<Props, State> {
onChange={evt => {
this.setState({ value: evt.target.value });
}}
autoFocus
onPressEnter={this.onConfirm}
/>
</Modal>
);
Expand Down
21 changes: 16 additions & 5 deletions frontend/javascripts/oxalis/view/layouting/layout_persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import NanoEvents from "nanoevents";
import _ from "lodash";

import { getIsInIframe } from "libs/utils";
import { listenToStoreProperty } from "oxalis/model/helpers/listener_helpers";
import { setStoredLayoutsAction } from "oxalis/model/actions/ui_actions";
import Store from "oxalis/store";
import Toast from "libs/toast";
import UserLocalStorage from "libs/user_local_storage";

import getDefaultLayouts, {
currentLayoutVersion,
Expand All @@ -25,12 +27,12 @@ const localStorageKeys = {
};

function readStoredLayoutConfigs() {
const storedLayoutVersion = localStorage.getItem(localStorageKeys.currentLayoutVersion);
const storedLayoutVersion = UserLocalStorage.getItem(localStorageKeys.currentLayoutVersion);
const defaultLayoutConfig = getCurrentDefaultLayoutConfig();
if (getIsInIframe() || !storedLayoutVersion || disableLayoutPersistance) {
return defaultLayoutConfig;
}
const layoutString = localStorage.getItem(localStorageKeys.goldenWkLayouts);
const layoutString = UserLocalStorage.getItem(localStorageKeys.goldenWkLayouts);
if (!layoutString) {
return defaultLayoutConfig;
}
Expand Down Expand Up @@ -80,16 +82,25 @@ function readStoredLayoutConfigs() {
return defaultLayoutConfig;
}

Store.dispatch(setStoredLayoutsAction(readStoredLayoutConfigs()));
listenToStoreProperty(
storeState => storeState.activeUser,
() => {
Store.dispatch(setStoredLayoutsAction(readStoredLayoutConfigs()));
},
true,
);

function persistLayoutConfigs() {
if (getIsInIframe()) {
// Don't persist layout in iframe
return;
}
const { storedLayouts } = Store.getState().uiInformation;
localStorage.setItem(localStorageKeys.goldenWkLayouts, JSON.stringify(storedLayouts));
localStorage.setItem(localStorageKeys.currentLayoutVersion, JSON.stringify(currentLayoutVersion));
UserLocalStorage.setItem(localStorageKeys.goldenWkLayouts, JSON.stringify(storedLayouts));
UserLocalStorage.setItem(
localStorageKeys.currentLayoutVersion,
JSON.stringify(currentLayoutVersion),
);
}

layoutEmitter.on("resetLayout", (layoutKey: LayoutKeys, activeLayout: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class TracingLayoutView extends React.PureComponent<Props, State> {
) {
lastActiveLayout = props.storedLayouts.LastActiveLayouts[layoutType];
} else {
// added as a valide fallback when there are no stored last active layouts
// added as a fallback when there are no stored last active layouts
const firstStoredLayout = Object.keys(props.storedLayouts[layoutType])[0];
lastActiveLayout = firstStoredLayout;
}
Expand Down