2017-05-26 06:20:00 +00:00
|
|
|
import { createSelector } from 'reselect';
|
2018-04-25 03:36:27 +00:00
|
|
|
import get from 'lodash/get';
|
2018-04-05 19:13:32 +00:00
|
|
|
import createReducer from 'utils/createReducer';
|
2017-05-26 06:20:00 +00:00
|
|
|
import { getSelectedTab, updateSelection } from './tab';
|
|
|
|
import * as actions from './actions';
|
|
|
|
|
|
|
|
export const getServers = state => state.servers;
|
|
|
|
|
|
|
|
export const getCurrentNick = createSelector(
|
|
|
|
getServers,
|
|
|
|
getSelectedTab,
|
2017-06-21 05:23:07 +00:00
|
|
|
(servers, tab) => {
|
2018-04-25 03:36:27 +00:00
|
|
|
if (!servers[tab.server]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const { editedNick } = servers[tab.server];
|
2018-05-16 03:02:48 +00:00
|
|
|
if (editedNick === null) {
|
2018-04-25 03:36:27 +00:00
|
|
|
return servers[tab.server].nick;
|
2017-06-21 05:23:07 +00:00
|
|
|
}
|
|
|
|
return editedNick;
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export const getCurrentServerName = createSelector(
|
|
|
|
getServers,
|
|
|
|
getSelectedTab,
|
2018-04-25 03:36:27 +00:00
|
|
|
(servers, tab) => get(servers, [tab.server, 'name'])
|
2017-05-26 06:20:00 +00:00
|
|
|
);
|
|
|
|
|
2017-07-02 01:31:00 +00:00
|
|
|
export const getCurrentServerStatus = createSelector(
|
|
|
|
getServers,
|
|
|
|
getSelectedTab,
|
2018-04-25 03:36:27 +00:00
|
|
|
(servers, tab) => get(servers, [tab.server, 'status'], {})
|
2017-07-02 01:31:00 +00:00
|
|
|
);
|
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
export default createReducer(
|
|
|
|
{},
|
|
|
|
{
|
2018-05-16 03:02:48 +00:00
|
|
|
[actions.CONNECT](state, { host, nick, name }) {
|
2018-04-25 03:36:27 +00:00
|
|
|
if (!state[host]) {
|
|
|
|
state[host] = {
|
2018-04-05 23:46:22 +00:00
|
|
|
nick,
|
2018-04-25 03:36:27 +00:00
|
|
|
editedNick: null,
|
2018-05-16 03:02:48 +00:00
|
|
|
name: name || host,
|
2018-04-25 03:36:27 +00:00
|
|
|
status: {
|
|
|
|
connected: false,
|
|
|
|
error: null
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.DISCONNECT](state, { server }) {
|
|
|
|
delete state[server];
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.SET_SERVER_NAME](state, { server, name }) {
|
|
|
|
state[server].name = name;
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.SET_NICK](state, { server, nick, editing }) {
|
|
|
|
if (editing) {
|
|
|
|
state[server].editedNick = nick;
|
|
|
|
} else if (nick === '') {
|
|
|
|
state[server].editedNick = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.socket.NICK](state, { server, oldNick, newNick }) {
|
|
|
|
if (!oldNick || oldNick === state[server].nick) {
|
|
|
|
state[server].nick = newNick;
|
|
|
|
state[server].editedNick = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.socket.NICK_FAIL](state, { server }) {
|
|
|
|
state[server].editedNick = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.socket.SERVERS](state, { data }) {
|
|
|
|
if (data) {
|
|
|
|
data.forEach(({ host, name, nick, status }) => {
|
2018-05-16 03:02:48 +00:00
|
|
|
state[host] = { name, nick, status, editedNick: null };
|
2018-04-25 03:36:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[actions.socket.CONNECTION_UPDATE](state, { server, connected, error }) {
|
|
|
|
if (state[server]) {
|
|
|
|
state[server].status.connected = connected;
|
|
|
|
state[server].status.error = error;
|
|
|
|
}
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 03:36:27 +00:00
|
|
|
);
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2018-05-16 03:02:48 +00:00
|
|
|
export function connect(config) {
|
2017-05-26 06:20:00 +00:00
|
|
|
return {
|
|
|
|
type: actions.CONNECT,
|
2018-05-16 03:02:48 +00:00
|
|
|
...config,
|
2017-05-26 06:20:00 +00:00
|
|
|
socket: {
|
|
|
|
type: 'connect',
|
2018-05-16 03:02:48 +00:00
|
|
|
data: config
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function disconnect(server) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: actions.DISCONNECT,
|
|
|
|
server,
|
|
|
|
socket: {
|
|
|
|
type: 'quit',
|
|
|
|
data: { server }
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dispatch(updateSelection());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-04 09:28:56 +00:00
|
|
|
export function reconnect(server, settings) {
|
|
|
|
return {
|
|
|
|
type: actions.RECONNECT,
|
|
|
|
server,
|
|
|
|
settings,
|
|
|
|
socket: {
|
|
|
|
type: 'reconnect',
|
|
|
|
data: {
|
|
|
|
...settings,
|
|
|
|
server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-26 06:20:00 +00:00
|
|
|
export function whois(user, server) {
|
|
|
|
return {
|
|
|
|
type: actions.WHOIS,
|
|
|
|
user,
|
|
|
|
server,
|
|
|
|
socket: {
|
|
|
|
type: 'whois',
|
|
|
|
data: { user, server }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function away(message, server) {
|
|
|
|
return {
|
|
|
|
type: actions.AWAY,
|
|
|
|
message,
|
|
|
|
server,
|
|
|
|
socket: {
|
|
|
|
type: 'away',
|
|
|
|
data: { message, server }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
export function setNick(nick, server, editing) {
|
|
|
|
nick = nick.trim().replace(' ', '');
|
|
|
|
|
|
|
|
const action = {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: actions.SET_NICK,
|
|
|
|
nick,
|
|
|
|
server,
|
2017-06-21 05:23:07 +00:00
|
|
|
editing
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!editing && nick !== '') {
|
|
|
|
action.socket = {
|
2017-05-26 06:20:00 +00:00
|
|
|
type: 'nick',
|
|
|
|
data: {
|
2017-06-21 05:23:07 +00:00
|
|
|
newNick: nick,
|
2017-05-26 06:20:00 +00:00
|
|
|
server
|
|
|
|
}
|
2017-06-21 05:23:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return action;
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
2017-06-12 04:18:32 +00:00
|
|
|
|
|
|
|
export function isValidServerName(name) {
|
|
|
|
return name.trim() !== '';
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setServerName(name, server) {
|
|
|
|
const action = {
|
|
|
|
type: actions.SET_SERVER_NAME,
|
|
|
|
name,
|
|
|
|
server
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isValidServerName(name)) {
|
|
|
|
action.socket = {
|
|
|
|
type: 'set_server_name',
|
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
server
|
|
|
|
},
|
|
|
|
debounce: {
|
2018-10-15 06:56:17 +00:00
|
|
|
delay: 500,
|
|
|
|
key: `server_name:${server}`
|
2017-06-12 04:18:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|