2019-05-01 20:05:11 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import sdk from '../../../index';
|
2019-06-27 15:05:04 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-05-01 20:05:11 +03:00
|
|
|
import { isContentActionable } from '../../../utils/EventUtils';
|
2019-05-09 21:11:40 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2019-05-01 20:05:11 +03:00
|
|
|
|
2019-06-27 15:05:04 +03:00
|
|
|
// The maximum number of reactions to initially show on a message.
|
|
|
|
const MAX_ITEMS_WHEN_LIMITED = 8;
|
|
|
|
|
2019-05-01 20:05:11 +03:00
|
|
|
export default class ReactionsRow extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
// The event we're displaying reactions for
|
|
|
|
mxEvent: PropTypes.object.isRequired,
|
2019-05-08 20:16:27 +03:00
|
|
|
// The Relations model from the JS SDK for reactions to `mxEvent`
|
|
|
|
reactions: PropTypes.object,
|
2019-05-01 20:05:11 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:14:58 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
if (props.reactions) {
|
2019-05-09 20:37:52 +03:00
|
|
|
props.reactions.on("Relations.add", this.onReactionsChange);
|
2019-05-16 15:03:05 +03:00
|
|
|
props.reactions.on("Relations.remove", this.onReactionsChange);
|
2019-05-09 17:14:58 +03:00
|
|
|
props.reactions.on("Relations.redaction", this.onReactionsChange);
|
|
|
|
}
|
2019-05-09 21:11:40 +03:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
myReactions: this.getMyReactions(),
|
2019-06-27 15:05:04 +03:00
|
|
|
showAll: false,
|
2019-05-09 21:11:40 +03:00
|
|
|
};
|
2019-05-09 17:14:58 +03:00
|
|
|
}
|
|
|
|
|
2019-05-10 19:59:17 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.reactions !== this.props.reactions) {
|
|
|
|
this.props.reactions.on("Relations.add", this.onReactionsChange);
|
2019-05-16 15:03:05 +03:00
|
|
|
this.props.reactions.on("Relations.remove", this.onReactionsChange);
|
2019-05-10 19:59:17 +03:00
|
|
|
this.props.reactions.on("Relations.redaction", this.onReactionsChange);
|
|
|
|
this.onReactionsChange();
|
2019-05-09 17:14:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.props.reactions) {
|
2019-05-09 20:37:52 +03:00
|
|
|
this.props.reactions.removeListener(
|
|
|
|
"Relations.add",
|
|
|
|
this.onReactionsChange,
|
|
|
|
);
|
2019-05-16 15:03:05 +03:00
|
|
|
this.props.reactions.removeListener(
|
|
|
|
"Relations.remove",
|
|
|
|
this.onReactionsChange,
|
|
|
|
);
|
2019-05-09 17:14:58 +03:00
|
|
|
this.props.reactions.removeListener(
|
|
|
|
"Relations.redaction",
|
|
|
|
this.onReactionsChange,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onReactionsChange = () => {
|
|
|
|
// TODO: Call `onHeightChanged` as needed
|
2019-05-09 21:11:40 +03:00
|
|
|
this.setState({
|
|
|
|
myReactions: this.getMyReactions(),
|
|
|
|
});
|
2019-05-13 17:00:43 +03:00
|
|
|
// Using `forceUpdate` for the moment, since we know the overall set of reactions
|
|
|
|
// has changed (this is triggered by events for that purpose only) and
|
|
|
|
// `PureComponent`s shallow state / props compare would otherwise filter this out.
|
2019-05-09 17:14:58 +03:00
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
|
2019-05-09 21:11:40 +03:00
|
|
|
getMyReactions() {
|
|
|
|
const reactions = this.props.reactions;
|
|
|
|
if (!reactions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const userId = MatrixClientPeg.get().getUserId();
|
2019-05-16 15:57:02 +03:00
|
|
|
const myReactions = reactions.getAnnotationsBySender()[userId];
|
|
|
|
if (!myReactions) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return [...myReactions.values()];
|
2019-05-09 21:11:40 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 15:05:04 +03:00
|
|
|
onShowAllClick = () => {
|
|
|
|
this.setState({
|
|
|
|
showAll: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:05:11 +03:00
|
|
|
render() {
|
2019-05-08 20:16:27 +03:00
|
|
|
const { mxEvent, reactions } = this.props;
|
2019-06-27 15:05:04 +03:00
|
|
|
const { myReactions, showAll } = this.state;
|
2019-05-01 20:05:11 +03:00
|
|
|
|
2019-05-08 20:16:27 +03:00
|
|
|
if (!reactions || !isContentActionable(mxEvent)) {
|
2019-05-01 20:05:11 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReactionsRowButton = sdk.getComponent('messages.ReactionsRowButton');
|
2019-06-27 15:05:04 +03:00
|
|
|
let items = reactions.getSortedAnnotationsByKey().map(([content, events]) => {
|
2019-05-09 17:14:58 +03:00
|
|
|
const count = events.size;
|
|
|
|
if (!count) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-09 21:11:40 +03:00
|
|
|
const myReactionEvent = myReactions && myReactions.find(mxEvent => {
|
|
|
|
if (mxEvent.isRedacted()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-15 15:46:32 +03:00
|
|
|
return mxEvent.getRelation().key === content;
|
2019-05-09 21:11:40 +03:00
|
|
|
});
|
2019-05-01 20:05:11 +03:00
|
|
|
return <ReactionsRowButton
|
|
|
|
key={content}
|
|
|
|
content={content}
|
2019-06-05 16:54:14 +03:00
|
|
|
count={count}
|
2019-05-09 21:11:40 +03:00
|
|
|
mxEvent={mxEvent}
|
2019-05-17 13:52:03 +03:00
|
|
|
reactionEvents={events}
|
2019-05-09 21:11:40 +03:00
|
|
|
myReactionEvent={myReactionEvent}
|
2019-05-01 20:05:11 +03:00
|
|
|
/>;
|
2019-06-27 15:05:04 +03:00
|
|
|
}).filter(item => !!item);
|
|
|
|
|
2019-06-28 16:46:57 +03:00
|
|
|
// Show the first MAX_ITEMS if there are MAX_ITEMS + 1 or more items.
|
|
|
|
// The "+ 1" ensure that the "show all" reveals something that takes up
|
|
|
|
// more space than the button itself.
|
2019-06-28 16:47:41 +03:00
|
|
|
let showAllButton;
|
2019-06-28 16:46:57 +03:00
|
|
|
if ((items.length > MAX_ITEMS_WHEN_LIMITED + 1) && !showAll) {
|
2019-06-27 15:05:04 +03:00
|
|
|
items = items.slice(0, MAX_ITEMS_WHEN_LIMITED);
|
2019-06-28 16:47:41 +03:00
|
|
|
showAllButton = <a
|
2019-06-27 15:05:04 +03:00
|
|
|
className="mx_ReactionsRow_showAll"
|
|
|
|
href="#"
|
|
|
|
onClick={this.onShowAllClick}
|
|
|
|
>
|
|
|
|
{_t("Show all")}
|
|
|
|
</a>;
|
|
|
|
}
|
2019-05-01 20:05:11 +03:00
|
|
|
|
|
|
|
return <div className="mx_ReactionsRow">
|
|
|
|
{items}
|
2019-06-28 16:47:41 +03:00
|
|
|
{showAllButton}
|
2019-05-01 20:05:11 +03:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
}
|