Only count joined channels

This commit is contained in:
Ken-Håvard Lieng 2019-01-30 04:48:37 +01:00
parent 71bfe92dae
commit 3e90e6c86d
5 changed files with 178 additions and 154 deletions

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ import classnames from 'classnames';
import get from 'lodash/get';
import Button from 'components/ui/Button';
import TabListItem from 'containers/TabListItem';
import { count } from 'utils';
export default class TabList extends PureComponent {
handleTabClick = (server, target) => this.props.select(server, target);
@ -40,13 +41,19 @@ export default class TabList extends PureComponent {
/>
);
let chanLabel;
const chanCount = count(server.channels, c => c.joined);
const chanLimit =
get(srv.features, ['CHANLIMIT', '#'], 0) || srv.features.MAXCHANNELS;
let chanLabel;
if (chanLimit > 0) {
chanLabel = `CHANNELS (${server.channels.length}/${chanLimit})`;
} else {
chanLabel = `CHANNELS (${server.channels.length})`;
chanLabel = (
<span>
<span className="success">{chanCount}</span>/{chanLimit}
</span>
);
} else if (chanCount > 0) {
chanLabel = <span className="success">{chanCount}</span>;
}
tabs.push(
@ -55,18 +62,19 @@ export default class TabList extends PureComponent {
className="tab-label"
onClick={() => openModal('channel', { server: address })}
>
<span>{chanLabel}</span>
<Button>+</Button>
<span>CHANNELS {chanLabel}</span>
<Button title="Join Channel">+</Button>
</div>
);
server.channels.forEach(name =>
server.channels.forEach(({ name, joined }) =>
tabs.push(
<TabListItem
key={address + name}
server={address}
target={name}
content={name}
joined={joined}
selected={tab.server === address && tab.name === name}
onClick={this.handleTabClick}
/>
@ -76,7 +84,12 @@ export default class TabList extends PureComponent {
if (privateChats[address] && privateChats[address].length > 0) {
tabs.push(
<div key={`${address}-pm}`} className="tab-label">
<span>DIRECT MESSAGES ({privateChats[address].length})</span>
<span>
DIRECT MESSAGES{' '}
<span style={{ color: '#6bb758' }}>
{privateChats[address].length}
</span>
</span>
{/*<Button>+</Button>*/}
</div>
);

View File

@ -4,9 +4,6 @@ import TabListItem from 'components/TabListItem';
import connect from 'utils/connect';
const mapState = createStructuredSelector({
joined: (state, { server, target }) =>
get(state, ['channels', server, target, 'joined']),
error: (state, { server, target }) => {
const messages = get(state, ['messages', server, target]);

View File

@ -97,8 +97,8 @@ export const getSortedChannels = createSelector(
sortBy(
Object.keys(channels).map(server => ({
address: server,
channels: sortBy(Object.keys(channels[server]), channel =>
channel.toLowerCase()
channels: sortBy(channels[server], channel =>
channel.name.toLowerCase()
)
})),
server => server.address.toLowerCase()

View File

@ -170,7 +170,7 @@ export function measureScrollBarWidth() {
}
export function findIndex(arr, pred) {
if (!arr) {
if (!Array.isArray(arr) || typeof pred !== 'function') {
return -1;
}
@ -190,3 +190,17 @@ export function find(arr, pred) {
}
return null;
}
export function count(arr, pred) {
if (!Array.isArray(arr) || typeof pred !== 'function') {
return 0;
}
let c = 0;
for (let i = 0; i < arr.length; i++) {
if (pred(arr[i])) {
c++;
}
}
return c;
}