dispatch/client/js/state/privateChats.js

86 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-04-25 03:36:27 +00:00
import sortBy from 'lodash/sortBy';
import { isChannel } from 'utils';
import createReducer from 'utils/createReducer';
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
}
},
[actions.PRIVATE_CHATS](state, { privateChats }) {
2020-06-15 08:58:51 +00:00
privateChats.forEach(({ network, name }) => {
if (!state[network]) {
state[network] = [];
}
2020-06-15 08:58:51 +00:00
state[network].push(name);
});
},
[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
);
2020-06-15 08:58:51 +00:00
export function openPrivateChat(network, nick) {
return (dispatch, getState) => {
2020-06-15 08:58:51 +00:00
if (!getState().privateChats[network]?.includes(nick)) {
dispatch({
type: actions.OPEN_PRIVATE_CHAT,
2020-06-15 08:58:51 +00:00
network,
nick,
socket: {
type: 'open_dm',
2020-06-15 08:58:51 +00:00
data: { network, name: nick }
}
});
}
};
}
2020-06-15 08:58:51 +00:00
export function closePrivateChat(network, nick) {
return dispatch => {
dispatch({
type: actions.CLOSE_PRIVATE_CHAT,
2020-06-15 08:58:51 +00:00
network,
nick,
socket: {
type: 'close_dm',
2020-06-15 08:58:51 +00:00
data: { network, name: nick }
}
});
dispatch(updateSelection());
};
}