dispatch/client/js/modules/initialState.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-11-06 10:13:32 +00:00
/* 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';
2018-10-15 06:56:17 +00:00
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';
2017-05-19 05:29:44 +00:00
2018-11-06 10:13:32 +00:00
function loadState({ store }, env) {
2017-06-06 23:03:35 +00:00
store.dispatch(setConnectDefaults(env.defaults));
2018-11-22 11:00:18 +00:00
store.dispatch(
appSet({
hexIP: env.hexIP,
version: env.version
})
);
2018-10-15 06:56:17 +00:00
store.dispatch(setSettings(env.settings, true));
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.server === server && 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));
}
});
}
2018-11-06 10:13:32 +00:00
export default async function initialState(ctx) {
const env = await window.__init__;
ctx.socket.connect();
loadState(ctx, env);
2018-11-06 10:13:32 +00:00
}