Combine init actions

This commit is contained in:
Ken-Håvard Lieng 2020-06-25 01:50:10 +02:00
parent 02e9df865e
commit d844f6ee1a
13 changed files with 228 additions and 232 deletions

File diff suppressed because one or more lines are too long

View File

@ -343,14 +343,18 @@ i[class*=' icon-']:before {
}
.tab-prefix {
color: #777;
color: #999;
}
.error .tab-prefix {
color: #f6546a;
}
.tab-label {
margin: 5px;
margin-left: 15px;
font-size: 12px;
color: #777;
color: #999;
display: flex;
align-items: center;
height: 25px;
@ -369,7 +373,7 @@ i[class*=' icon-']:before {
height: 100%;
font-size: 20px;
background: none;
color: #777;
color: #999;
}
.tab-label:hover button {
@ -388,7 +392,7 @@ i[class*=' icon-']:before {
.side-buttons button {
background: #222;
color: #777;
color: #999;
flex: 1;
}

View File

@ -41,7 +41,7 @@ const Settings = ({
<Checkbox
name="coloredNicks"
label="Colored nicks"
checked={settings.coloredNicks}
checked={!!settings.coloredNicks}
onChange={e => setSetting('coloredNicks', e.target.checked)}
/>
</div>

View File

@ -1,56 +1,24 @@
import { socket as socketActions } from 'state/actions';
import { getConnected, getWrapWidth, appSet } from 'state/app';
import { INIT } from 'state/actions';
import { getConnected, getWrapWidth } from 'state/app';
import { searchChannels } from 'state/channelSearch';
import { addMessages } from 'state/messages';
import { setSettings } from 'state/settings';
import { when } from 'utils/observe';
function loadState({ store }, env) {
store.dispatch(setSettings(env.settings, true));
if (env.networks) {
store.dispatch({
type: socketActions.NETWORKS,
data: env.networks
});
when(store, getConnected, () =>
// Cache top channels for each network
env.networks.forEach(({ host }) =>
store.dispatch(searchChannels(host, ''))
)
);
}
if (env.channels) {
store.dispatch({
type: socketActions.CHANNELS,
data: env.channels
});
}
if (env.openDMs) {
store.dispatch({
type: 'PRIVATE_CHATS',
privateChats: env.openDMs
});
}
if (env.users) {
store.dispatch({
type: socketActions.USERS,
...env.users
});
}
store.dispatch(
appSet({
type: INIT,
settings: env.settings,
networks: env.networks,
channels: env.channels,
openDMs: env.openDMs,
users: env.users,
app: {
connectDefaults: env.defaults,
initialized: true,
hexIP: env.hexIP,
version: env.version
})
);
}
});
if (env.messages) {
// Wait until wrapWidth gets initialized so that height calculations
@ -60,6 +28,15 @@ function loadState({ store }, env) {
store.dispatch(addMessages(messages, network, to, false, next));
});
}
if (env.networks) {
when(store, getConnected, () =>
// Cache top channels for each network
env.networks.forEach(({ host }) =>
store.dispatch(searchChannels(host, ''))
)
);
}
}
/* eslint-disable no-underscore-dangle */

View File

@ -276,31 +276,31 @@ describe('channel reducer', () => {
});
});
it('handles SOCKET_CHANNELS', () => {
it('handles channels from INIT', () => {
const state = reducer(undefined, {
type: actions.socket.CHANNELS,
data: [
type: actions.INIT,
channels: [
{ network: 'srv', name: 'chan1', topic: 'the topic' },
{ network: 'srv', name: 'chan2' },
{ network: 'srv', name: 'chan2', joined: true },
{ network: 'srv2', name: 'chan1' }
]
});
expect(state).toEqual({
srv: {
chan1: { name: 'chan1', joined: true, topic: 'the topic', users: [] },
chan1: { name: 'chan1', topic: 'the topic', users: [] },
chan2: { name: 'chan2', joined: true, users: [] }
},
srv2: {
chan1: { name: 'chan1', joined: true, users: [] }
chan1: { name: 'chan1', users: [] }
}
});
});
it('handles SOCKET_NETWORKS', () => {
it('handles networks from INIT', () => {
const state = reducer(undefined, {
type: actions.socket.NETWORKS,
data: [{ host: '127.0.0.1' }, { host: 'thehost' }]
type: actions.INIT,
networks: [{ host: '127.0.0.1' }, { host: 'thehost' }]
});
expect(state).toEqual({

View File

@ -182,10 +182,10 @@ describe('network reducer', () => {
});
});
it('adds the networks on SOCKET_NETWORKS', () => {
it('adds the networks on INIT', () => {
let state = reducer(undefined, {
type: actions.socket.NETWORKS,
data: [
type: actions.INIT,
networks: [
{
host: '127.0.0.1',
name: 'stuff',

View File

@ -1,3 +1,4 @@
export const INIT = 'INIT';
export const APP_SET = 'APP_SET';
export const INVITE = 'INVITE';
@ -27,7 +28,6 @@ export const CLOSE_MODAL = 'CLOSE_MODAL';
export const CLOSE_PRIVATE_CHAT = 'CLOSE_PRIVATE_CHAT';
export const OPEN_PRIVATE_CHAT = 'OPEN_PRIVATE_CHAT';
export const PRIVATE_CHATS = 'PRIVATE_CHATS';
export const SEARCH_MESSAGES = 'SEARCH_MESSAGES';
export const TOGGLE_SEARCH = 'TOGGLE_SEARCH';
@ -67,7 +67,6 @@ function createSocketActions(types) {
export const socket = createSocketActions([
'cert_fail',
'cert_success',
'channels',
'channel_forward',
'channel_search',
'connected',
@ -84,7 +83,6 @@ export const socket = createSocketActions([
'pm',
'quit',
'search',
'networks',
'topic',
'users'
]);

View File

@ -45,6 +45,10 @@ export default createReducer(initialState, {
state.wrapWidth = action.wrapWidth;
state.charWidth = action.charWidth;
state.windowWidth = action.windowWidth;
},
[actions.INIT](state, { app }) {
Object.assign(state, app);
}
});

View File

@ -183,14 +183,6 @@ export default createReducer(
});
},
[actions.socket.USERS](state, { network, channel, users }) {
state[network][channel].users = users.map(nick => loadUser(nick));
},
[actions.socket.TOPIC](state, { network, channel, topic }) {
state[network][channel].topic = topic;
},
[actions.socket.MODE](state, { network, channel, user, remove, add }) {
const u = find(state[network][channel].users, v => v.nick === user);
if (u) {
@ -209,19 +201,31 @@ export default createReducer(
}
},
[actions.socket.CHANNELS](state, { data }) {
if (data) {
data.forEach(({ network, name, topic }) => {
[actions.socket.TOPIC](state, { network, channel, topic }) {
state[network][channel].topic = topic;
},
[actions.socket.USERS](state, { network, channel, users }) {
state[network][channel].users = users.map(nick => loadUser(nick));
},
[actions.INIT](state, { networks, channels, users }) {
if (networks) {
networks.forEach(({ host }) => init(state, host));
}
if (channels) {
channels.forEach(({ network, name, topic, joined }) => {
const chan = init(state, network, name);
chan.joined = true;
chan.joined = joined;
chan.topic = topic;
});
}
},
[actions.socket.NETWORKS](state, { data }) {
if (data) {
data.forEach(({ host }) => init(state, host));
if (users) {
state[users.network][users.channel].users = users.users.map(nick =>
loadUser(nick)
);
}
},

View File

@ -76,9 +76,9 @@ export default createReducer(
state[network].editedNick = null;
},
[actions.socket.NETWORKS](state, { data }) {
if (data) {
data.forEach(
[actions.INIT](state, { networks }) {
if (networks) {
networks.forEach(
({ host, name = host, nick, connected, error, features = {} }) => {
state[host] = {
name,

View File

@ -1,5 +1,5 @@
import sortBy from 'lodash/sortBy';
import { isChannel } from 'utils';
import { isDM } from 'utils';
import createReducer from 'utils/createReducer';
import { updateSelection } from './tab';
import * as actions from './actions';
@ -30,20 +30,21 @@ export default createReducer(
}
},
[actions.PRIVATE_CHATS](state, { privateChats }) {
privateChats.forEach(({ network, name }) => {
[actions.INIT](state, { openDMs }) {
if (openDMs) {
openDMs.forEach(({ network, name }) => {
if (!state[network]) {
state[network] = [];
}
state[network].push(name);
});
}
},
[actions.ADD_MESSAGE](state, { message }) {
const { network, from, to } = message;
if (!to && from.indexOf('.') === -1 && !isChannel(from)) {
open(state, network, from);
if (isDM(message)) {
open(state, message.network, message.from);
}
},

View File

@ -44,6 +44,10 @@ export default createReducer(
} else {
state[key] = value;
}
},
[actions.INIT](state, { settings }) {
return settings;
}
}
);

View File

@ -34,6 +34,10 @@ function isString(s, maxLength) {
return true;
}
export function isDM({ from, to }) {
return !to && from?.indexOf('.') === -1 && !isChannel(from);
}
export function trimPrefixChar(str, char) {
if (!isString(str)) {
return str;