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';
|
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 = {
|
|
|
|
server: action.server,
|
|
|
|
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
|
|
|
|
|
|
|
[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
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
export function select(server, name, doReplace) {
|
|
|
|
const navigate = doReplace ? replace : push;
|
|
|
|
if (name) {
|
|
|
|
return navigate(`/${server}/${encodeURIComponent(name)}`);
|
|
|
|
}
|
|
|
|
return navigate(`/${server}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateSelection() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2018-03-25 00:34:41 +00:00
|
|
|
const { history } = state.tab;
|
2017-05-26 06:20:00 +00:00
|
|
|
const { servers } = state;
|
|
|
|
const { server } = state.tab.selected;
|
2018-04-25 03:36:27 +00:00
|
|
|
const serverAddrs = Object.keys(servers);
|
2017-05-26 06:20:00 +00:00
|
|
|
|
2018-04-25 03:36:27 +00:00
|
|
|
if (serverAddrs.length === 0) {
|
2017-05-26 06:20:00 +00:00
|
|
|
dispatch(replace('/connect'));
|
2018-04-25 03:36:27 +00:00
|
|
|
} else if (history.length > 0) {
|
|
|
|
const tab = history[history.length - 1];
|
2017-05-26 06:20:00 +00:00
|
|
|
dispatch(select(tab.server, tab.name, true));
|
2018-04-25 03:36:27 +00:00
|
|
|
} else if (servers[server]) {
|
2017-05-26 06:20:00 +00:00
|
|
|
dispatch(select(server, null, true));
|
|
|
|
} else {
|
2018-04-25 03:36:27 +00:00
|
|
|
dispatch(select(serverAddrs.sort()[0], null, true));
|
2017-05-26 06:20:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setSelectedTab(server, name = null) {
|
|
|
|
return {
|
|
|
|
type: actions.SELECT_TAB,
|
|
|
|
server,
|
|
|
|
name
|
|
|
|
};
|
|
|
|
}
|