2017-05-21 07:53:47 +00:00
|
|
|
import Cookie from 'js-cookie';
|
2017-06-21 06:40:28 +00:00
|
|
|
import { socket as socketActions } from 'state/actions';
|
|
|
|
import { getWrapWidth, setConnectDefaults } from 'state/app';
|
|
|
|
import { addMessages } from 'state/messages';
|
|
|
|
import { select, updateSelection } from 'state/tab';
|
2018-04-05 19:13:32 +00:00
|
|
|
import { find } from 'utils';
|
|
|
|
import { when } from 'utils/observe';
|
|
|
|
import { replace } from 'utils/router';
|
2017-05-19 05:29:44 +00:00
|
|
|
|
|
|
|
export default function initialState({ store }) {
|
|
|
|
const env = JSON.parse(document.getElementById('env').innerHTML);
|
|
|
|
|
2017-06-06 23:03:35 +00:00
|
|
|
store.dispatch(setConnectDefaults(env.defaults));
|
2017-05-19 05:29:44 +00:00
|
|
|
|
|
|
|
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-06-21 07:45:47 +00:00
|
|
|
const [server, name = null] = tab.split(/;(.+)/);
|
2017-05-19 06:36:13 +00:00
|
|
|
|
2017-06-29 05:36:58 +00:00
|
|
|
if (name && find(env.channels, chan => chan.name === name)) {
|
2017-05-21 07:53:47 +00:00
|
|
|
store.dispatch(select(server, name, true));
|
2017-06-29 05:36:58 +00:00
|
|
|
} else if (find(env.servers, srv => srv.host === server)) {
|
|
|
|
store.dispatch(select(server, null, 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));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|