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

View File

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

View File

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

View File

@ -170,7 +170,7 @@ export function measureScrollBarWidth() {
} }
export function findIndex(arr, pred) { export function findIndex(arr, pred) {
if (!arr) { if (!Array.isArray(arr) || typeof pred !== 'function') {
return -1; return -1;
} }
@ -190,3 +190,17 @@ export function find(arr, pred) {
} }
return null; 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;
}