dispatch/client/js/state/tab.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-12-06 12:27:53 +00:00
import get from 'lodash/get';
2018-12-08 10:08:01 +00:00
import Cookie from 'js-cookie';
import createReducer from 'utils/createReducer';
import { push, replace, LOCATION_CHANGED } from 'utils/router';
import * as actions from './actions';
2018-12-06 12:27:53 +00:00
import { find } from '../utils';
2015-12-28 23:34:32 +00:00
2018-04-25 03:36:27 +00:00
const initialState = {
selected: {},
history: []
};
2015-12-28 23:34:32 +00:00
function selectTab(state, action) {
2018-04-25 03:36:27 +00:00
state.selected = {
server: action.server,
name: action.name
};
state.history.push(state.selected);
}
2017-05-02 21:21:25 +00:00
export const getSelectedTab = state => state.tab.selected;
2018-04-25 03:36:27 +00:00
export default createReducer(initialState, {
[actions.SELECT_TAB]: selectTab,
2015-12-28 23:34:32 +00:00
[actions.PART](state, action) {
2018-04-25 03:36:27 +00:00
state.history = state.history.filter(
tab => !(tab.server === action.server && tab.name === action.channels[0])
2018-04-05 23:46:22 +00:00
);
2015-12-28 23:34:32 +00:00
},
[actions.CLOSE_PRIVATE_CHAT](state, action) {
2018-04-25 03:36:27 +00:00
state.history = state.history.filter(
tab => !(tab.server === action.server && tab.name === action.nick)
2018-04-05 23:46:22 +00:00
);
2015-12-28 23:34:32 +00:00
},
[actions.DISCONNECT](state, action) {
2018-04-25 03:36:27 +00:00
state.history = state.history.filter(tab => tab.server !== action.server);
2015-12-28 23:34:32 +00:00
},
[LOCATION_CHANGED](state, action) {
const { route, params } = action;
if (route === 'chat') {
2018-04-25 03:36:27 +00:00
selectTab(state, params);
} else {
state.selected = {};
2015-12-28 23:34:32 +00:00
}
}
});
export function select(server, name, doReplace) {
const navigate = doReplace ? replace : push;
if (name) {
return navigate(`/${server}/${encodeURIComponent(name)}`);
}
return navigate(`/${server}`);
}
2018-12-06 12:27:53 +00:00
export function tabExists(
{ server, name },
{ servers, channels, privateChats }
) {
return (
(name && get(channels, [server, name])) ||
(!name && server && servers[server]) ||
(name && find(privateChats[server], nick => nick === name))
);
}
2018-12-08 10:08:01 +00:00
function parseTabCookie() {
const cookie = Cookie.get('tab');
if (cookie) {
const [server, name = null] = cookie.split(/;(.+)/);
return { server, name };
}
return null;
}
export function updateSelection(tryCookie) {
return (dispatch, getState) => {
const state = getState();
2018-12-06 12:27:53 +00:00
2018-12-08 10:08:01 +00:00
if (tabExists(state.tab.selected, state)) {
2018-12-06 12:27:53 +00:00
return;
}
2018-12-08 10:08:01 +00:00
if (tryCookie) {
const tab = parseTabCookie();
if (tab && tabExists(tab, state)) {
return dispatch(select(tab.server, tab.name, true));
}
}
const { servers } = state;
const { history } = state.tab;
const { server } = state.tab.selected;
2018-04-25 03:36:27 +00:00
const serverAddrs = Object.keys(servers);
2018-04-25 03:36:27 +00:00
if (serverAddrs.length === 0) {
dispatch(replace('/connect'));
2018-11-29 12:06:37 +00:00
} else if (
history.length > 0 &&
2018-12-06 12:27:53 +00:00
tabExists(history[history.length - 1], state)
2018-11-29 12:06:37 +00:00
) {
2018-04-25 03:36:27 +00:00
const tab = history[history.length - 1];
dispatch(select(tab.server, tab.name, true));
2018-04-25 03:36:27 +00:00
} else if (servers[server]) {
dispatch(select(server, null, true));
} else {
2018-04-25 03:36:27 +00:00
dispatch(select(serverAddrs.sort()[0], null, true));
}
};
}
export function setSelectedTab(server, name = null) {
return {
type: actions.SELECT_TAB,
server,
name
};
}