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';
|
2019-05-02 13:48:32 +03:00
|
|
|
import classNames from 'classnames';
|
2019-05-01 20:05:11 +03:00
|
|
|
|
2019-05-09 21:11:40 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2019-05-17 13:52:03 +03:00
|
|
|
import sdk from '../../../index';
|
2019-05-09 21:11:40 +03:00
|
|
|
|
2019-05-01 20:05:11 +03:00
|
|
|
export default class ReactionsRowButton extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2019-05-09 21:11:40 +03:00
|
|
|
// The event we're displaying reactions for
|
|
|
|
mxEvent: PropTypes.object.isRequired,
|
2019-05-17 13:52:03 +03:00
|
|
|
// The reaction content / key / emoji
|
2019-05-01 20:05:11 +03:00
|
|
|
content: PropTypes.string.isRequired,
|
2019-05-17 13:52:03 +03:00
|
|
|
// A Set of Martix reaction events for this key
|
|
|
|
reactionEvents: PropTypes.object.isRequired,
|
2019-05-09 21:11:40 +03:00
|
|
|
// A possible Matrix event if the current user has voted for this type
|
|
|
|
myReactionEvent: PropTypes.object,
|
2019-05-02 13:48:32 +03:00
|
|
|
}
|
|
|
|
|
2019-05-17 13:52:03 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
tooltipVisible: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:48:32 +03:00
|
|
|
onClick = (ev) => {
|
2019-05-09 21:11:40 +03:00
|
|
|
const { mxEvent, myReactionEvent, content } = this.props;
|
|
|
|
if (myReactionEvent) {
|
|
|
|
MatrixClientPeg.get().redactEvent(
|
|
|
|
mxEvent.getRoomId(),
|
|
|
|
myReactionEvent.getId(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
MatrixClientPeg.get().sendEvent(mxEvent.getRoomId(), "m.reaction", {
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": "m.annotation",
|
|
|
|
"event_id": mxEvent.getId(),
|
|
|
|
"key": content,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2019-05-02 13:48:32 +03:00
|
|
|
};
|
|
|
|
|
2019-05-17 13:52:03 +03:00
|
|
|
onMouseOver = () => {
|
|
|
|
this.setState({
|
|
|
|
// To avoid littering the DOM with a tooltip for every reaction,
|
|
|
|
// only render it on first use.
|
|
|
|
tooltipRendered: true,
|
|
|
|
tooltipVisible: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseOut = () => {
|
|
|
|
this.setState({
|
|
|
|
tooltipVisible: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-01 20:05:11 +03:00
|
|
|
render() {
|
2019-05-17 13:52:03 +03:00
|
|
|
const ReactionsRowButtonTooltip =
|
|
|
|
sdk.getComponent('messages.ReactionsRowButtonTooltip');
|
|
|
|
const { content, reactionEvents, myReactionEvent } = this.props;
|
|
|
|
|
|
|
|
const count = reactionEvents.size;
|
|
|
|
if (!count) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-02 13:48:32 +03:00
|
|
|
|
|
|
|
const classes = classNames({
|
|
|
|
mx_ReactionsRowButton: true,
|
2019-05-09 21:11:40 +03:00
|
|
|
mx_ReactionsRowButton_selected: !!myReactionEvent,
|
2019-05-02 13:48:32 +03:00
|
|
|
});
|
|
|
|
|
2019-05-17 13:52:03 +03:00
|
|
|
let tooltip;
|
|
|
|
if (this.state.tooltipRendered) {
|
|
|
|
tooltip = <ReactionsRowButtonTooltip
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
content={content}
|
|
|
|
reactionEvents={reactionEvents}
|
|
|
|
visible={this.state.tooltipVisible}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:48:32 +03:00
|
|
|
return <span className={classes}
|
|
|
|
onClick={this.onClick}
|
2019-05-17 13:52:03 +03:00
|
|
|
onMouseOver={this.onMouseOver}
|
|
|
|
onMouseOut={this.onMouseOut}
|
2019-05-02 13:48:32 +03:00
|
|
|
>
|
2019-05-09 21:11:40 +03:00
|
|
|
{content} {count}
|
2019-05-17 13:52:03 +03:00
|
|
|
{tooltip}
|
2019-05-01 20:05:11 +03:00
|
|
|
</span>;
|
|
|
|
}
|
|
|
|
}
|