2016-11-09 19:03:35 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-11-10 17:08:11 +03:00
|
|
|
import React from 'react';
|
2016-11-09 19:03:35 +03:00
|
|
|
const MemberAvatar = require('../avatars/MemberAvatar.js');
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'MemberEventListSummary',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
// An array of member events to summarise
|
2016-11-10 17:09:40 +03:00
|
|
|
events: React.PropTypes.array.isRequired,
|
2016-11-10 17:12:45 +03:00
|
|
|
// An array of EventTiles to render when expanded
|
|
|
|
children: React.PropTypes.array.isRequired,
|
2016-11-09 19:03:35 +03:00
|
|
|
// The maximum number of names to show in either the join or leave summaries
|
|
|
|
summaryLength: React.PropTypes.number,
|
|
|
|
// The maximum number of avatars to display in the summary
|
|
|
|
avatarsMaxLength: React.PropTypes.number,
|
|
|
|
// The minimum number of events needed to trigger summarisation
|
|
|
|
threshold: React.PropTypes.number,
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
expanded: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
summaryLength: 3,
|
|
|
|
threshold: 3,
|
2016-12-14 18:31:35 +03:00
|
|
|
avatarsMaxLength: 5,
|
2016-11-09 19:03:35 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
_toggleSummary: function() {
|
2016-11-09 19:03:35 +03:00
|
|
|
this.setState({
|
|
|
|
expanded: !this.state.expanded,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
_getEventSenderName: function(ev) {
|
2016-11-09 19:03:35 +03:00
|
|
|
if (!ev) {
|
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
return ev.sender.name || ev.event.content.displayname || ev.getSender();
|
|
|
|
},
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
_renderNameList: function(events) {
|
2016-11-09 19:03:35 +03:00
|
|
|
if (events.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let originalNumber = events.length;
|
|
|
|
events = events.slice(0, this.props.summaryLength);
|
|
|
|
let lastEvent = events.pop();
|
|
|
|
|
|
|
|
let names = events.map((ev) => {
|
2016-11-10 17:12:05 +03:00
|
|
|
return this._getEventSenderName(ev);
|
2016-11-09 19:03:35 +03:00
|
|
|
}).join(', ');
|
|
|
|
|
2016-11-10 20:36:22 +03:00
|
|
|
let lastName = this._getEventSenderName(lastEvent);
|
|
|
|
if (names.length === 0) {
|
|
|
|
// special-case for a single event
|
|
|
|
return lastName;
|
2016-11-09 19:03:35 +03:00
|
|
|
}
|
2016-11-10 17:33:41 +03:00
|
|
|
|
|
|
|
let remaining = originalNumber - this.props.summaryLength;
|
2016-11-10 20:36:22 +03:00
|
|
|
if (remaining > 0) {
|
|
|
|
// name1, name2, name3, and 100 others
|
|
|
|
return names + ', ' + lastName + ', and ' + remaining + ' others';
|
|
|
|
} else {
|
|
|
|
// name1, name2 and name3
|
|
|
|
return names + ' and ' + lastName;
|
|
|
|
}
|
2016-11-09 19:03:35 +03:00
|
|
|
},
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
_renderSummary: function(joinEvents, leaveEvents) {
|
|
|
|
let joiners = this._renderNameList(joinEvents);
|
|
|
|
let leavers = this._renderNameList(leaveEvents);
|
2016-11-10 17:33:41 +03:00
|
|
|
|
2016-11-09 19:03:35 +03:00
|
|
|
let joinSummary = null;
|
|
|
|
if (joiners) {
|
|
|
|
joinSummary = (
|
|
|
|
<span>
|
2016-11-10 17:33:41 +03:00
|
|
|
{joiners} joined the room
|
2016-11-09 19:03:35 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2016-11-10 18:38:20 +03:00
|
|
|
let leaveSummary = null;
|
2016-11-09 19:03:35 +03:00
|
|
|
if (leavers) {
|
|
|
|
leaveSummary = (
|
|
|
|
<span>
|
2016-11-10 17:33:41 +03:00
|
|
|
{leavers} left the room
|
2016-11-09 19:03:35 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2016-11-16 17:42:30 +03:00
|
|
|
|
|
|
|
// The joinEvents and leaveEvents are representative of the net movement
|
|
|
|
// per-user, and so it is possible that the total net movement is nil,
|
|
|
|
// whilst there are some events in the expanded list. If the total net
|
|
|
|
// movement is nil, then neither joinSummary nor leaveSummary will be
|
|
|
|
// truthy, so return null.
|
|
|
|
if (!joinSummary && !leaveSummary) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-09 19:03:35 +03:00
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{joinSummary}{joinSummary && leaveSummary?'; ':''}
|
2016-11-16 17:42:30 +03:00
|
|
|
{leaveSummary}.
|
2016-11-09 19:03:35 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
_renderAvatars: function(events) {
|
2016-11-09 19:03:35 +03:00
|
|
|
let avatars = events.slice(0, this.props.avatarsMaxLength).map((e) => {
|
|
|
|
return (
|
|
|
|
<MemberAvatar
|
|
|
|
key={e.getId()}
|
|
|
|
member={e.sender}
|
|
|
|
width={14}
|
|
|
|
height={14}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{avatars}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-12-14 18:31:35 +03:00
|
|
|
shouldComponentUpdate: function(nextProps, nextState) {
|
2016-12-15 17:43:44 +03:00
|
|
|
// Update if
|
|
|
|
// - The number of summarised events has changed
|
|
|
|
// - or if the summary is currently expanded
|
|
|
|
// - or if the summary is about to toggle to become collapsed
|
2016-12-14 18:31:35 +03:00
|
|
|
return (
|
|
|
|
nextProps.events.length !== this.props.events.length ||
|
2016-12-15 17:43:44 +03:00
|
|
|
this.state.expanded || nextState.expanded
|
2016-12-14 18:31:35 +03:00
|
|
|
);
|
|
|
|
},
|
2016-11-09 19:03:35 +03:00
|
|
|
|
2016-12-14 18:31:35 +03:00
|
|
|
render: function() {
|
2016-11-09 19:03:35 +03:00
|
|
|
let eventsToRender = this.props.events;
|
2016-12-14 18:31:35 +03:00
|
|
|
let fewEvents = eventsToRender.length < this.props.threshold;
|
|
|
|
let expanded = this.state.expanded || fewEvents;
|
|
|
|
|
|
|
|
let expandedEvents = null;
|
|
|
|
if (expanded) {
|
|
|
|
expandedEvents = this.props.children;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fewEvents) {
|
|
|
|
return (
|
|
|
|
<div className="mx_MemberEventListSummary">
|
|
|
|
{expandedEvents}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map user IDs to the first and last member events in eventsToRender for each user
|
|
|
|
let userEvents = {
|
|
|
|
// $userId : {first : e0, last : e1}
|
|
|
|
};
|
|
|
|
|
|
|
|
eventsToRender.forEach((e) => {
|
|
|
|
const userId = e.getSender();
|
|
|
|
// Initialise a user's events
|
|
|
|
if (!userEvents[userId]) {
|
|
|
|
userEvents[userId] = {first: null, last: null};
|
|
|
|
}
|
|
|
|
if (!userEvents[userId].first) {
|
|
|
|
userEvents[userId].first = e;
|
|
|
|
}
|
2016-12-15 16:15:00 +03:00
|
|
|
userEvents[userId].last = e;
|
2016-12-14 18:31:35 +03:00
|
|
|
});
|
2016-11-09 19:03:35 +03:00
|
|
|
|
2016-12-14 18:31:35 +03:00
|
|
|
// Populate the join/leave event arrays with events that represent what happened
|
|
|
|
// overall to a user's membership. If no events are added to either array for a
|
|
|
|
// particular user, they will be considered a user that "joined and left".
|
|
|
|
let joinEvents = [];
|
|
|
|
let leaveEvents = [];
|
|
|
|
let joinedAndLeft = 0;
|
|
|
|
let senders = Object.keys(userEvents);
|
2016-11-16 17:42:30 +03:00
|
|
|
senders.forEach(
|
|
|
|
(userId) => {
|
2016-12-14 18:31:35 +03:00
|
|
|
let firstEvent = userEvents[userId].first;
|
|
|
|
let lastEvent = userEvents[userId].last;
|
2016-11-16 17:42:30 +03:00
|
|
|
|
2016-11-16 19:26:24 +03:00
|
|
|
// Membership BEFORE eventsToRender
|
|
|
|
let previousMembership = firstEvent.getPrevContent().membership || "leave";
|
|
|
|
|
2016-12-14 18:31:35 +03:00
|
|
|
// If the last membership event differs from previousMembership, use that.
|
2016-11-16 19:26:24 +03:00
|
|
|
if (previousMembership !== lastEvent.getContent().membership) {
|
2016-12-14 18:31:35 +03:00
|
|
|
if (lastEvent.event.content.membership === 'join') {
|
|
|
|
joinEvents.push(lastEvent);
|
|
|
|
} else if (lastEvent.event.content.membership === 'leave') {
|
|
|
|
leaveEvents.push(lastEvent);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Increment the number of users whose membership change was nil overall
|
|
|
|
joinedAndLeft++;
|
2016-11-16 17:42:30 +03:00
|
|
|
}
|
2016-11-09 19:03:35 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-11-10 17:12:05 +03:00
|
|
|
let avatars = this._renderAvatars(joinEvents.concat(leaveEvents));
|
2016-12-14 18:31:35 +03:00
|
|
|
let summary = this._renderSummary(joinEvents, leaveEvents);
|
|
|
|
let toggleButton = (
|
|
|
|
<a className="mx_MemberEventListSummary_toggle" onClick={this._toggleSummary}>
|
|
|
|
{expanded ? 'collapse' : 'expand'}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
let plural = (joinEvents.length + leaveEvents.length > 0) ? 'others' : 'users';
|
|
|
|
let noun = (joinedAndLeft === 1 ? 'user' : plural);
|
|
|
|
|
|
|
|
let summaryContainer = (
|
|
|
|
<div className="mx_EventTile_line">
|
|
|
|
<div className="mx_EventTile_info">
|
|
|
|
<span className="mx_MemberEventListSummary_avatars">
|
|
|
|
{avatars}
|
|
|
|
</span>
|
|
|
|
<span className="mx_TextualEvent mx_MemberEventListSummary_summary">
|
|
|
|
{summary}{joinedAndLeft ? joinedAndLeft + ' ' + noun + ' joined and left' : ''}
|
|
|
|
</span>
|
|
|
|
{toggleButton}
|
2016-11-09 19:03:35 +03:00
|
|
|
</div>
|
2016-12-14 18:31:35 +03:00
|
|
|
</div>
|
|
|
|
);
|
2016-11-09 19:03:35 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mx_MemberEventListSummary">
|
|
|
|
{summaryContainer}
|
|
|
|
{expandedEvents}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|