Redirect to stored tab if the route is not found

This commit is contained in:
Ken-Håvard Lieng 2017-05-19 08:36:13 +02:00
parent 2afbf2359c
commit 5487ecdb57
5 changed files with 62 additions and 19 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,12 @@
import documentTitle from './documentTitle';
import handleSocket from './handleSocket';
import initialState from './initialState';
import storage from './storage';
export default function runModules(ctx) {
initialState(ctx);
documentTitle(ctx);
handleSocket(ctx);
storage(ctx);
}

View File

@ -1,5 +1,7 @@
import { setEnvironment } from '../actions/environment';
import { addMessages } from '../actions/message';
import { select, updateSelection } from '../actions/tab';
import { find } from '../util';
import { initWidthUpdates } from '../util/messageHeight';
import { replace } from '../util/router';
@ -13,6 +15,21 @@ export default function initialState({ store }) {
type: 'SOCKET_SERVERS',
data: env.servers
});
if (!store.getState().router.route) {
let tab = localStorage.tab;
if (tab) {
tab = JSON.parse(tab);
if (find(env.servers, server => server.host === tab.server)) {
store.dispatch(select(tab.server, tab.name, true));
} else {
store.dispatch(updateSelection());
}
} else {
store.dispatch(updateSelection());
}
}
} else {
store.dispatch(replace('/connect'));
}

View File

@ -0,0 +1,15 @@
import debounce from 'lodash/debounce';
import observe from '../util/observe';
import { getSelectedTab } from '../reducers/tab';
const saveTab = debounce(tab => {
localStorage.tab = JSON.stringify(tab);
}, 3000);
export default function storage({ store }) {
observe(store, getSelectedTab, tab => {
if (tab.server) {
saveTab(tab);
}
});
}

View File

@ -45,3 +45,12 @@ export function measureScrollBarWidth() {
return widthNoScroll - widthWithScroll;
}
export function find(arr, pred) {
for (let i = 0; i < arr.length; i++) {
if (pred(arr[i])) {
return arr[i];
}
}
return undefined;
}