2017-05-21 07:53:47 +00:00
|
|
|
import Cookie from 'js-cookie';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { socket as socketActions } from '../state/actions';
|
|
|
|
import { getWrapWidth, setEnvironment } from '../state/environment';
|
|
|
|
import { addMessages } from '../state/messages';
|
|
|
|
import { select, updateSelection } from '../state/tab';
|
2017-05-19 06:36:13 +00:00
|
|
|
import { find } from '../util';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { when } from '../util/observe';
|
2017-05-19 05:29:44 +00:00
|
|
|
import { replace } from '../util/router';
|
|
|
|
|
|
|
|
export default function initialState({ store }) {
|
|
|
|
const env = JSON.parse(document.getElementById('env').innerHTML);
|
|
|
|
|
|
|
|
store.dispatch(setEnvironment('connect_defaults', env.defaults));
|
|
|
|
|
|
|
|
if (env.servers) {
|
|
|
|
store.dispatch({
|
2017-05-26 06:20:00 +00:00
|
|
|
type: socketActions.SERVERS,
|
2017-05-19 05:29:44 +00:00
|
|
|
data: env.servers
|
|
|
|
});
|
2017-05-19 06:36:13 +00:00
|
|
|
|
|
|
|
if (!store.getState().router.route) {
|
2017-05-21 07:53:47 +00:00
|
|
|
const tab = Cookie.get('tab');
|
2017-05-19 06:36:13 +00:00
|
|
|
if (tab) {
|
2017-05-21 07:53:47 +00:00
|
|
|
const [server, name = null] = tab.split(':');
|
2017-05-19 06:36:13 +00:00
|
|
|
|
2017-05-21 07:53:47 +00:00
|
|
|
if (find(env.servers, srv => srv.host === server)) {
|
|
|
|
store.dispatch(select(server, name, true));
|
2017-05-19 06:36:13 +00:00
|
|
|
} else {
|
|
|
|
store.dispatch(updateSelection());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
store.dispatch(updateSelection());
|
|
|
|
}
|
|
|
|
}
|
2017-05-19 05:29:44 +00:00
|
|
|
} else {
|
|
|
|
store.dispatch(replace('/connect'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env.channels) {
|
|
|
|
store.dispatch({
|
2017-05-26 06:20:00 +00:00
|
|
|
type: socketActions.CHANNELS,
|
2017-05-19 05:29:44 +00:00
|
|
|
data: env.channels
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env.users) {
|
|
|
|
store.dispatch({
|
2017-05-26 06:20:00 +00:00
|
|
|
type: socketActions.USERS,
|
2017-05-19 05:29:44 +00:00
|
|
|
...env.users
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
// Wait until wrapWidth gets initialized so that height calculations
|
|
|
|
// only happen once for these messages
|
|
|
|
when(store, getWrapWidth, () => {
|
2017-05-19 05:29:44 +00:00
|
|
|
if (env.messages) {
|
|
|
|
const { messages, server, to, next } = env.messages;
|
|
|
|
store.dispatch(addMessages(messages, server, to, false, next));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|