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';
|
2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
|
|
|
import { push, replace, LOCATION_CHANGED } from 'utils/router';
|
2017-05-26 06:20:00 +00:00
|
|
|
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
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
function selectTab(state, action) {
|
2018-04-25 03:36:27 +00:00
|
|
|
state.selected = {
|
2020-06-15 08:58:51 +00:00
|
|
|
network: action.network,
|
2018-04-25 03:36:27 +00:00
|
|
|
name: action.name
|
|
|
|
};
|
|
|
|
state.history.push(state.selected);
|
2017-05-07 20:19:15 +00:00
|
|
|
}
|
|
|
|
|
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, {
|
2017-05-07 20:19:15 +00:00
|
|
|
[actions.SELECT_TAB]: selectTab,
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2020-06-21 03:33:02 +00:00
|
|
|
[actions.JOIN](state, { network, channels, selectFirst }) {
|
|
|
|
if (selectFirst) {
|
|
|
|
state.selected = {
|
|
|
|
network,
|
|
|
|
name: channels[0]
|
|
|
|
};
|
|
|
|
state.history.push(state.selected);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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(
|
2020-06-15 08:58:51 +00:00
|
|
|
tab =>
|
|
|
|
!(tab.network === action.network && 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(
|
2020-06-15 08:58:51 +00:00
|
|
|
tab => !(tab.network === action.network && tab.name === action.nick)
|
2018-04-05 23:46:22 +00:00
|
|
|
);
|
2015-12-28 23:34:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
[actions.DISCONNECT](state, action) {
|
2020-06-15 08:58:51 +00:00
|
|
|
state.history = state.history.filter(tab => tab.network !== action.network);
|
2015-12-28 23:34:32 +00:00
|
|
|
},
|
|
|
|
|
2017-05-07 20:19:15 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function select(network, name, doReplace) {
|
2017-05-26 06:20:00 +00:00
|
|
|
const navigate = doReplace ? replace : push;
|
|
|
|
if (name) {
|
2020-06-15 08:58:51 +00:00
|
|
|
return navigate(`/${network}/${encodeURIComponent(name)}`);
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
2020-06-15 08:58:51 +00:00
|
|
|
return navigate(`/${network}`);
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:27:53 +00:00
|
|
|
export function tabExists(
|
2020-06-15 08:58:51 +00:00
|
|
|
{ network, name },
|
|
|
|
{ networks, channels, privateChats }
|
2018-12-06 12:27:53 +00:00
|
|
|
) {
|
|
|
|
return (
|
2020-06-15 08:58:51 +00:00
|
|
|
(name && get(channels, [network, name])) ||
|
|
|
|
(!name && network && networks[network]) ||
|
|
|
|
(name && find(privateChats[network], nick => nick === name))
|
2018-12-06 12:27:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-08 10:08:01 +00:00
|
|
|
function parseTabCookie() {
|
|
|
|
const cookie = Cookie.get('tab');
|
|
|
|
if (cookie) {
|
2020-06-15 08:58:51 +00:00
|
|
|
const [network, name = null] = cookie.split(/;(.+)/);
|
|
|
|
return { network, name };
|
2018-12-08 10:08:01 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateSelection(tryCookie) {
|
2017-05-26 06:20:00 +00:00
|
|
|
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)) {
|
2020-06-15 08:58:51 +00:00
|
|
|
return dispatch(select(tab.network, tab.name, true));
|
2018-12-08 10:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
const { networks } = state;
|
2018-12-08 10:08:01 +00:00
|
|
|
const { history } = state.tab;
|
2020-06-15 08:58:51 +00:00
|
|
|
const { network } = state.tab.selected;
|
|
|
|
const networkAddrs = Object.keys(networks);
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
if (networkAddrs.length === 0) {
|
2017-05-26 06:20:00 +00:00
|
|
|
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];
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(select(tab.network, tab.name, true));
|
|
|
|
} else if (networks[network]) {
|
|
|
|
dispatch(select(network, null, true));
|
2017-05-26 06:20:00 +00:00
|
|
|
} else {
|
2020-06-15 08:58:51 +00:00
|
|
|
dispatch(select(networkAddrs.sort()[0], null, true));
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function setSelectedTab(network, name = null) {
|
2017-05-26 06:20:00 +00:00
|
|
|
return {
|
|
|
|
type: actions.SELECT_TAB,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2017-05-26 06:20:00 +00:00
|
|
|
name
|
|
|
|
};
|
|
|
|
}
|