Simplify routing logic

This commit is contained in:
Ken-Håvard Lieng 2018-12-08 11:08:01 +01:00
parent 35c2d682e3
commit c1ca29511e
3 changed files with 131 additions and 151 deletions

View file

@ -1,5 +1,4 @@
import Cookie from 'js-cookie';
import { select, updateSelection, tabExists } from 'state/tab';
import { updateSelection } from 'state/tab';
import { observe, when } from 'utils/observe';
export default function route({ store }) {
@ -14,43 +13,7 @@ export default function route({ store }) {
state => state.router,
router => {
if (!router.route || router.route === 'chat') {
const state = store.getState();
let redirect = true;
const tabs = [];
if (router.route === 'chat') {
if (tabExists(router.params, state)) {
redirect = false;
} else {
tabs.push(router.params);
}
}
if (redirect && first) {
const cookie = Cookie.get('tab');
if (cookie) {
const [server, name = null] = cookie.split(/;(.+)/);
tabs.unshift({ server, name });
}
}
if (redirect) {
let found = false;
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
if (tabExists(tab, state)) {
store.dispatch(select(tab.server, tab.name, true));
found = true;
break;
}
}
if (!found) {
store.dispatch(updateSelection());
}
}
store.dispatch(updateSelection(first));
first = false;
}
}