2019-05-01 20:05:11 +03:00
|
|
|
/*
|
2021-05-13 16:13:00 +03:00
|
|
|
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
|
2019-05-01 20:05:11 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
import React from "react";
|
|
|
|
import classNames from "classnames";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2019-05-01 20:05:11 +03:00
|
|
|
|
2019-12-06 17:48:34 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import { formatCommaSeparatedList } from '../../../utils/FormattingUtils';
|
2020-05-24 15:08:29 +03:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-05-13 16:13:00 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
|
|
|
import ReactionsRowButtonTooltip from "./ReactionsRowButtonTooltip";
|
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2019-05-09 21:11:40 +03:00
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
interface IProps {
|
|
|
|
// The event we're displaying reactions for
|
|
|
|
mxEvent: MatrixEvent;
|
|
|
|
// The reaction content / key / emoji
|
|
|
|
content: string;
|
|
|
|
// The count of votes for this key
|
|
|
|
count: number;
|
2021-05-13 16:14:01 +03:00
|
|
|
// A Set of Matrix reaction events for this key
|
2021-05-13 16:13:00 +03:00
|
|
|
reactionEvents: Set<MatrixEvent>;
|
|
|
|
// A possible Matrix event if the current user has voted for this type
|
|
|
|
myReactionEvent?: MatrixEvent;
|
|
|
|
}
|
2019-05-02 13:48:32 +03:00
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
interface IState {
|
|
|
|
tooltipRendered: boolean;
|
|
|
|
tooltipVisible: boolean;
|
|
|
|
}
|
2019-05-17 13:52:03 +03:00
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
@replaceableComponent("views.messages.ReactionsRowButton")
|
|
|
|
export default class ReactionsRowButton extends React.PureComponent<IProps, IState> {
|
|
|
|
static contextType = MatrixClientContext;
|
|
|
|
|
|
|
|
state = {
|
|
|
|
tooltipRendered: false,
|
|
|
|
tooltipVisible: false,
|
|
|
|
};
|
2019-05-17 13:52:03 +03:00
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
onClick = () => {
|
2019-05-09 21:11:40 +03:00
|
|
|
const { mxEvent, myReactionEvent, content } = this.props;
|
|
|
|
if (myReactionEvent) {
|
2021-05-13 16:13:00 +03:00
|
|
|
this.context.redactEvent(
|
2019-05-09 21:11:40 +03:00
|
|
|
mxEvent.getRoomId(),
|
|
|
|
myReactionEvent.getId(),
|
|
|
|
);
|
|
|
|
} else {
|
2021-05-13 16:13:00 +03:00
|
|
|
this.context.sendEvent(mxEvent.getRoomId(), "m.reaction", {
|
2019-05-09 21:11:40 +03:00
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": "m.annotation",
|
|
|
|
"event_id": mxEvent.getId(),
|
|
|
|
"key": content,
|
|
|
|
},
|
|
|
|
});
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: "message_sent" });
|
2019-05-09 21:11:40 +03:00
|
|
|
}
|
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,
|
|
|
|
});
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2019-05-17 13:52:03 +03:00
|
|
|
|
2020-07-18 14:01:51 +03:00
|
|
|
onMouseLeave = () => {
|
2019-05-17 13:52:03 +03:00
|
|
|
this.setState({
|
|
|
|
tooltipVisible: false,
|
|
|
|
});
|
2021-06-29 15:11:58 +03:00
|
|
|
};
|
2019-05-17 13:52:03 +03:00
|
|
|
|
2019-05-01 20:05:11 +03:00
|
|
|
render() {
|
2019-12-06 17:48:34 +03:00
|
|
|
const { mxEvent, content, count, reactionEvents, myReactionEvent } = this.props;
|
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}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
|
2021-05-13 16:13:00 +03:00
|
|
|
const room = this.context.getRoom(mxEvent.getRoomId());
|
2019-12-06 17:48:34 +03:00
|
|
|
let label;
|
|
|
|
if (room) {
|
|
|
|
const senders = [];
|
|
|
|
for (const reactionEvent of reactionEvents) {
|
|
|
|
const member = room.getMember(reactionEvent.getSender());
|
|
|
|
const name = member ? member.name : reactionEvent.getSender();
|
|
|
|
senders.push(name);
|
|
|
|
}
|
|
|
|
label = _t(
|
|
|
|
"<reactors/><reactedWith> reacted with %(content)s</reactedWith>",
|
|
|
|
{
|
|
|
|
content,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
reactors: () => {
|
|
|
|
return formatCommaSeparatedList(senders, 6);
|
|
|
|
},
|
|
|
|
reactedWith: (sub) => {
|
|
|
|
if (!content) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2021-04-29 20:11:57 +03:00
|
|
|
const isPeeking = room.getMyMembership() !== "join";
|
2020-07-18 14:01:51 +03:00
|
|
|
return <AccessibleButton
|
|
|
|
className={classes}
|
2019-12-06 17:48:34 +03:00
|
|
|
aria-label={label}
|
2019-05-02 13:48:32 +03:00
|
|
|
onClick={this.onClick}
|
2021-04-29 20:11:57 +03:00
|
|
|
disabled={isPeeking}
|
2019-05-17 13:52:03 +03:00
|
|
|
onMouseOver={this.onMouseOver}
|
2020-07-18 14:01:51 +03:00
|
|
|
onMouseLeave={this.onMouseLeave}
|
2019-05-02 13:48:32 +03:00
|
|
|
>
|
2019-12-06 17:48:34 +03:00
|
|
|
<span className="mx_ReactionsRowButton_content" aria-hidden="true">
|
2019-07-05 18:17:17 +03:00
|
|
|
{content}
|
|
|
|
</span>
|
2019-12-06 17:48:34 +03:00
|
|
|
<span className="mx_ReactionsRowButton_count" aria-hidden="true">
|
2019-07-05 18:17:17 +03:00
|
|
|
{count}
|
|
|
|
</span>
|
2019-05-17 13:52:03 +03:00
|
|
|
{tooltip}
|
2019-12-07 15:20:06 +03:00
|
|
|
</AccessibleButton>;
|
2019-05-01 20:05:11 +03:00
|
|
|
}
|
|
|
|
}
|