Simplify routing logic
This commit is contained in:
parent
35c2d682e3
commit
c1ca29511e
File diff suppressed because one or more lines are too long
@ -1,5 +1,4 @@
|
|||||||
import Cookie from 'js-cookie';
|
import { updateSelection } from 'state/tab';
|
||||||
import { select, updateSelection, tabExists } from 'state/tab';
|
|
||||||
import { observe, when } from 'utils/observe';
|
import { observe, when } from 'utils/observe';
|
||||||
|
|
||||||
export default function route({ store }) {
|
export default function route({ store }) {
|
||||||
@ -14,43 +13,7 @@ export default function route({ store }) {
|
|||||||
state => state.router,
|
state => state.router,
|
||||||
router => {
|
router => {
|
||||||
if (!router.route || router.route === 'chat') {
|
if (!router.route || router.route === 'chat') {
|
||||||
const state = store.getState();
|
store.dispatch(updateSelection(first));
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
|
import Cookie from 'js-cookie';
|
||||||
import createReducer from 'utils/createReducer';
|
import createReducer from 'utils/createReducer';
|
||||||
import { push, replace, LOCATION_CHANGED } from 'utils/router';
|
import { push, replace, LOCATION_CHANGED } from 'utils/router';
|
||||||
import * as actions from './actions';
|
import * as actions from './actions';
|
||||||
@ -67,17 +68,33 @@ export function tabExists(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateSelection() {
|
function parseTabCookie() {
|
||||||
|
const cookie = Cookie.get('tab');
|
||||||
|
if (cookie) {
|
||||||
|
const [server, name = null] = cookie.split(/;(.+)/);
|
||||||
|
return { server, name };
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateSelection(tryCookie) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
const { history } = state.tab;
|
|
||||||
const { servers } = state;
|
|
||||||
const { server, name } = state.tab.selected;
|
|
||||||
|
|
||||||
if (tabExists({ server, name }, state)) {
|
if (tabExists(state.tab.selected, state)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (tryCookie) {
|
||||||
|
const tab = parseTabCookie();
|
||||||
|
if (tab && tabExists(tab, state)) {
|
||||||
|
return dispatch(select(tab.server, tab.name, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const { servers } = state;
|
||||||
|
const { history } = state.tab;
|
||||||
|
const { server } = state.tab.selected;
|
||||||
const serverAddrs = Object.keys(servers);
|
const serverAddrs = Object.keys(servers);
|
||||||
|
|
||||||
if (serverAddrs.length === 0) {
|
if (serverAddrs.length === 0) {
|
||||||
|
Loading…
Reference in New Issue
Block a user