2018-04-25 03:36:27 +00:00
|
|
|
import sortBy from 'lodash/sortBy';
|
2020-06-21 03:13:38 +00:00
|
|
|
import { isChannel } from 'utils';
|
2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { updateSelection } from './tab';
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
|
|
|
export const getPrivateChats = state => state.privateChats;
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
function open(state, network, nick) {
|
|
|
|
if (!state[network]) {
|
|
|
|
state[network] = [];
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2020-06-15 08:58:51 +00:00
|
|
|
if (!state[network].includes(nick)) {
|
|
|
|
state[network].push(nick);
|
|
|
|
state[network] = sortBy(state[network], v => v.toLowerCase());
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
[actions.OPEN_PRIVATE_CHAT](state, action) {
|
2020-06-15 08:58:51 +00:00
|
|
|
open(state, action.network, action.nick);
|
2018-04-25 03:36:27 +00:00
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.CLOSE_PRIVATE_CHAT](state, { network, nick }) {
|
|
|
|
const i = state[network]?.findIndex(n => n === nick);
|
2018-04-25 03:36:27 +00:00
|
|
|
if (i !== -1) {
|
2020-06-15 08:58:51 +00:00
|
|
|
state[network].splice(i, 1);
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
[actions.PRIVATE_CHATS](state, { privateChats }) {
|
2020-06-15 08:58:51 +00:00
|
|
|
privateChats.forEach(({ network, name }) => {
|
|
|
|
if (!state[network]) {
|
|
|
|
state[network] = [];
|
2020-05-06 02:19:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
state[network].push(name);
|
2020-05-06 02:19:55 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2020-06-21 03:13:38 +00:00
|
|
|
[actions.ADD_MESSAGE](state, { message }) {
|
|
|
|
const { network, from } = message;
|
|
|
|
if (from !== network && !isChannel(from)) {
|
|
|
|
open(state, network, from);
|
2018-04-25 03:36:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
[actions.DISCONNECT](state, { network }) {
|
|
|
|
delete state[network];
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
);
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function openPrivateChat(network, nick) {
|
2020-05-08 00:56:54 +00:00
|
|
|
return (dispatch, getState) => {
|
2020-06-15 08:58:51 +00:00
|
|
|
if (!getState().privateChats[network]?.includes(nick)) {
|
2020-05-08 00:56:54 +00:00
|
|
|
dispatch({
|
|
|
|
type: actions.OPEN_PRIVATE_CHAT,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-05-08 00:56:54 +00:00
|
|
|
nick,
|
|
|
|
socket: {
|
|
|
|
type: 'open_dm',
|
2020-06-15 08:58:51 +00:00
|
|
|
data: { network, name: nick }
|
2020-05-08 00:56:54 +00:00
|
|
|
}
|
|
|
|
});
|
2020-05-06 02:19:55 +00:00
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:58:51 +00:00
|
|
|
export function closePrivateChat(network, nick) {
|
2017-05-26 06:20:00 +00:00
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: actions.CLOSE_PRIVATE_CHAT,
|
2020-06-15 08:58:51 +00:00
|
|
|
network,
|
2020-05-06 02:19:55 +00:00
|
|
|
nick,
|
|
|
|
socket: {
|
|
|
|
type: 'close_dm',
|
2020-06-15 08:58:51 +00:00
|
|
|
data: { network, name: nick }
|
2020-05-06 02:19:55 +00:00
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
});
|
|
|
|
dispatch(updateSelection());
|
|
|
|
};
|
|
|
|
}
|