dispatch/client/src/js/modules/initialState.js

64 lines
1.7 KiB
JavaScript
Raw Normal View History

import Cookie from 'js-cookie';
import { socket as socketActions } from 'state/actions';
import { getWrapWidth, setConnectDefaults } from 'state/app';
import { addMessages } from 'state/messages';
import { select, updateSelection } from 'state/tab';
import { find } from 'util';
import { when } from 'util/observe';
import { replace } from 'util/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({
type: socketActions.SERVERS,
2017-05-19 05:29:44 +00:00
data: env.servers
});
if (!store.getState().router.route) {
const tab = Cookie.get('tab');
if (tab) {
2017-06-21 07:45:47 +00:00
const [server, name = null] = tab.split(/;(.+)/);
if (name && find(env.channels, chan => chan.name === name)) {
store.dispatch(select(server, name, true));
} else if (find(env.servers, srv => srv.host === server)) {
store.dispatch(select(server, null, true));
} 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({
type: socketActions.CHANNELS,
2017-05-19 05:29:44 +00:00
data: env.channels
});
}
if (env.users) {
store.dispatch({
type: socketActions.USERS,
2017-05-19 05:29:44 +00:00
...env.users
});
}
// 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));
}
});
}