Improve routing
This commit is contained in:
parent
aca380629f
commit
35c2d682e3
7 changed files with 223 additions and 179 deletions
|
@ -1,6 +1,7 @@
|
|||
import documentTitle from './documentTitle';
|
||||
import fonts from './fonts';
|
||||
import initialState from './initialState';
|
||||
import route from './route';
|
||||
import socket from './socket';
|
||||
import storage from './storage';
|
||||
import widthUpdates from './widthUpdates';
|
||||
|
@ -8,6 +9,7 @@ import widthUpdates from './widthUpdates';
|
|||
export default function runModules(ctx) {
|
||||
fonts(ctx);
|
||||
initialState(ctx);
|
||||
route(ctx);
|
||||
|
||||
documentTitle(ctx);
|
||||
socket(ctx);
|
||||
|
|
|
@ -1,22 +1,13 @@
|
|||
/* eslint-disable no-underscore-dangle */
|
||||
import Cookie from 'js-cookie';
|
||||
|
||||
import { socket as socketActions } from 'state/actions';
|
||||
import { getWrapWidth, setConnectDefaults, appSet } from 'state/app';
|
||||
import { addMessages } from 'state/messages';
|
||||
import { setSettings } from 'state/settings';
|
||||
import { select, updateSelection } from 'state/tab';
|
||||
import { find } from 'utils';
|
||||
import { when } from 'utils/observe';
|
||||
import { replace } from 'utils/router';
|
||||
|
||||
function loadState({ store }, env) {
|
||||
store.dispatch(setConnectDefaults(env.defaults));
|
||||
store.dispatch(
|
||||
appSet({
|
||||
hexIP: env.hexIP,
|
||||
version: env.version
|
||||
})
|
||||
);
|
||||
store.dispatch(setSettings(env.settings, true));
|
||||
|
||||
if (env.servers) {
|
||||
|
@ -24,53 +15,6 @@ function loadState({ store }, env) {
|
|||
type: socketActions.SERVERS,
|
||||
data: env.servers
|
||||
});
|
||||
|
||||
const { router } = store.getState();
|
||||
|
||||
if (!router.route || router.route === 'chat') {
|
||||
const tabs = [];
|
||||
|
||||
if (router.route === 'chat') {
|
||||
tabs.push(router.params);
|
||||
}
|
||||
|
||||
const cookie = Cookie.get('tab');
|
||||
if (cookie) {
|
||||
const [server, name = null] = cookie.split(/;(.+)/);
|
||||
tabs.push({
|
||||
server,
|
||||
name
|
||||
});
|
||||
}
|
||||
|
||||
let found = false;
|
||||
let i = 0;
|
||||
|
||||
while (!found) {
|
||||
const tab = tabs[i];
|
||||
i++;
|
||||
|
||||
if (
|
||||
tab.name &&
|
||||
find(
|
||||
env.channels,
|
||||
chan => chan.server === tab.server && chan.name === tab.name
|
||||
)
|
||||
) {
|
||||
found = true;
|
||||
store.dispatch(select(tab.server, tab.name, true));
|
||||
} else if (find(env.servers, srv => srv.host === tab.server)) {
|
||||
found = true;
|
||||
store.dispatch(select(tab.server, null, true));
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
store.dispatch(updateSelection());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
store.dispatch(replace('/connect'));
|
||||
}
|
||||
|
||||
if (env.channels) {
|
||||
|
@ -87,6 +31,14 @@ function loadState({ store }, env) {
|
|||
});
|
||||
}
|
||||
|
||||
store.dispatch(
|
||||
appSet({
|
||||
initialized: true,
|
||||
hexIP: env.hexIP,
|
||||
version: env.version
|
||||
})
|
||||
);
|
||||
|
||||
// Wait until wrapWidth gets initialized so that height calculations
|
||||
// only happen once for these messages
|
||||
when(store, getWrapWidth, () => {
|
||||
|
|
59
client/js/modules/route.js
Normal file
59
client/js/modules/route.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import Cookie from 'js-cookie';
|
||||
import { select, updateSelection, tabExists } from 'state/tab';
|
||||
import { observe, when } from 'utils/observe';
|
||||
|
||||
export default function route({ store }) {
|
||||
let first = true;
|
||||
|
||||
when(
|
||||
store,
|
||||
state => state.app.initialized,
|
||||
() =>
|
||||
observe(
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue