2019-05-01 18:05:11 +01:00
|
|
|
/*
|
2021-05-13 14:13:00 +01:00
|
|
|
Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
|
2019-05-01 18:05:11 +01: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 14:13:00 +01:00
|
|
|
import React from "react";
|
|
|
|
|
import classNames from "classnames";
|
2024-03-25 12:48:48 +00:00
|
|
|
import { EventType, MatrixEvent, RelationType } from "matrix-js-sdk/src/matrix";
|
2019-05-01 18:05:11 +01:00
|
|
|
|
2023-09-01 04:16:24 -06:00
|
|
|
import { mediaFromMxc } from "../../../customisations/Media";
|
2019-12-06 15:48:34 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2023-09-25 12:18:15 +01:00
|
|
|
import { formatList } from "../../../utils/FormattingUtils";
|
2020-05-24 13:08:29 +01:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-05-13 14:13:00 +01:00
|
|
|
import ReactionsRowButtonTooltip from "./ReactionsRowButtonTooltip";
|
|
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2023-09-01 04:16:24 -06:00
|
|
|
import { REACTION_SHORTCODE_KEY } from "./ReactionsRow";
|
2024-03-25 12:48:48 +00:00
|
|
|
|
2023-09-01 04:16:24 -06:00
|
|
|
export interface IProps {
|
2021-05-13 14:13:00 +01:00
|
|
|
// 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;
|
2023-07-31 15:42:23 +01:00
|
|
|
// A list of Matrix reaction events for this key
|
|
|
|
|
reactionEvents: MatrixEvent[];
|
2021-05-13 14:13:00 +01:00
|
|
|
// A possible Matrix event if the current user has voted for this type
|
|
|
|
|
myReactionEvent?: MatrixEvent;
|
2021-12-23 12:01:40 +00:00
|
|
|
// Whether to prevent quick-reactions by clicking on this reaction
|
|
|
|
|
disabled?: boolean;
|
2023-09-01 04:16:24 -06:00
|
|
|
// Whether to render custom image reactions
|
|
|
|
|
customReactionImagesEnabled?: boolean;
|
2021-05-13 14:13:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
tooltipRendered: boolean;
|
|
|
|
|
tooltipVisible: boolean;
|
|
|
|
|
}
|
2019-05-09 19:11:40 +01:00
|
|
|
|
2021-05-13 14:13:00 +01:00
|
|
|
export default class ReactionsRowButton extends React.PureComponent<IProps, IState> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static contextType = MatrixClientContext;
|
2022-06-10 20:41:05 +02:00
|
|
|
public context!: React.ContextType<typeof MatrixClientContext>;
|
2019-05-02 11:48:32 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public state = {
|
2021-05-13 14:13:00 +01:00
|
|
|
tooltipRendered: false,
|
|
|
|
|
tooltipVisible: false,
|
|
|
|
|
};
|
2019-05-17 11:52:03 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public onClick = (): void => {
|
2019-05-09 19:11:40 +01:00
|
|
|
const { mxEvent, myReactionEvent, content } = this.props;
|
|
|
|
|
if (myReactionEvent) {
|
2023-04-06 11:10:14 +01:00
|
|
|
this.context.redactEvent(mxEvent.getRoomId()!, myReactionEvent.getId()!);
|
2019-05-09 19:11:40 +01:00
|
|
|
} else {
|
2024-03-25 12:48:48 +00:00
|
|
|
this.context.sendEvent(mxEvent.getRoomId()!, EventType.Reaction, {
|
2019-05-09 19:11:40 +01:00
|
|
|
"m.relates_to": {
|
2024-03-25 12:48:48 +00:00
|
|
|
rel_type: RelationType.Annotation,
|
|
|
|
|
event_id: mxEvent.getId()!,
|
2019-05-09 19:11:40 +01:00
|
|
|
key: content,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-06-29 13:11:58 +01:00
|
|
|
dis.dispatch({ action: "message_sent" });
|
2019-05-09 19:11:40 +01:00
|
|
|
}
|
2019-05-02 11:48:32 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public onMouseOver = (): void => {
|
2019-05-17 11:52:03 +01:00
|
|
|
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 13:11:58 +01:00
|
|
|
};
|
2019-05-17 11:52:03 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public onMouseLeave = (): void => {
|
2019-05-17 11:52:03 +01:00
|
|
|
this.setState({
|
|
|
|
|
tooltipVisible: false,
|
|
|
|
|
});
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2019-05-17 11:52:03 +01:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2019-12-06 15:48:34 +01:00
|
|
|
const { mxEvent, content, count, reactionEvents, myReactionEvent } = this.props;
|
2019-05-01 18:05:11 +01:00
|
|
|
|
2019-05-02 11:48:32 +01:00
|
|
|
const classes = classNames({
|
|
|
|
|
mx_ReactionsRowButton: true,
|
2019-05-09 19:11:40 +01:00
|
|
|
mx_ReactionsRowButton_selected: !!myReactionEvent,
|
2019-05-02 11:48:32 +01:00
|
|
|
});
|
|
|
|
|
|
2023-05-05 09:26:11 +01:00
|
|
|
let tooltip: JSX.Element | undefined;
|
2019-05-17 11:52:03 +01:00
|
|
|
if (this.state.tooltipRendered) {
|
|
|
|
|
tooltip = (
|
|
|
|
|
<ReactionsRowButtonTooltip
|
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
|
content={content}
|
|
|
|
|
reactionEvents={reactionEvents}
|
|
|
|
|
visible={this.state.tooltipVisible}
|
2023-09-01 04:16:24 -06:00
|
|
|
customReactionImagesEnabled={this.props.customReactionImagesEnabled}
|
2019-05-17 11:52:03 +01:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:13:00 +01:00
|
|
|
const room = this.context.getRoom(mxEvent.getRoomId());
|
2023-03-13 15:07:20 +00:00
|
|
|
let label: string | undefined;
|
2023-09-01 04:16:24 -06:00
|
|
|
let customReactionName: string | undefined;
|
2019-12-06 15:48:34 +01:00
|
|
|
if (room) {
|
2023-03-13 15:07:20 +00:00
|
|
|
const senders: string[] = [];
|
2019-12-06 15:48:34 +01:00
|
|
|
for (const reactionEvent of reactionEvents) {
|
2023-03-29 08:22:35 +01:00
|
|
|
const member = room.getMember(reactionEvent.getSender()!);
|
|
|
|
|
senders.push(member?.name || reactionEvent.getSender()!);
|
2023-09-01 04:16:24 -06:00
|
|
|
customReactionName =
|
|
|
|
|
(this.props.customReactionImagesEnabled &&
|
|
|
|
|
REACTION_SHORTCODE_KEY.findIn(reactionEvent.getContent())) ||
|
|
|
|
|
undefined;
|
2021-09-17 11:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-25 12:18:15 +01:00
|
|
|
const reactors = formatList(senders, 6);
|
2021-09-17 11:36:22 +01:00
|
|
|
if (content) {
|
2023-09-19 17:16:38 +01:00
|
|
|
label = _t("timeline|reactions|label", {
|
2023-09-01 04:16:24 -06:00
|
|
|
reactors,
|
|
|
|
|
content: customReactionName || content,
|
|
|
|
|
});
|
2021-09-17 11:36:22 +01:00
|
|
|
} else {
|
|
|
|
|
label = reactors;
|
2019-12-06 15:48:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-23 12:01:40 +00:00
|
|
|
|
2023-09-01 04:16:24 -06:00
|
|
|
let reactionContent = (
|
|
|
|
|
<span className="mx_ReactionsRowButton_content" aria-hidden="true">
|
|
|
|
|
{content}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
if (this.props.customReactionImagesEnabled && content.startsWith("mxc://")) {
|
|
|
|
|
const imageSrc = mediaFromMxc(content).srcHttp;
|
|
|
|
|
if (imageSrc) {
|
|
|
|
|
reactionContent = (
|
|
|
|
|
<img
|
|
|
|
|
className="mx_ReactionsRowButton_content"
|
2023-10-03 19:17:26 +01:00
|
|
|
alt={customReactionName || _t("timeline|reactions|custom_reaction_fallback_label")}
|
2023-09-01 04:16:24 -06:00
|
|
|
src={imageSrc}
|
|
|
|
|
width="16"
|
|
|
|
|
height="16"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-18 12:01:51 +01:00
|
|
|
return (
|
|
|
|
|
<AccessibleButton
|
|
|
|
|
className={classes}
|
2019-12-06 15:48:34 +01:00
|
|
|
aria-label={label}
|
2019-05-02 11:48:32 +01:00
|
|
|
onClick={this.onClick}
|
2021-12-23 12:01:40 +00:00
|
|
|
disabled={this.props.disabled}
|
2019-05-17 11:52:03 +01:00
|
|
|
onMouseOver={this.onMouseOver}
|
2020-07-18 12:01:51 +01:00
|
|
|
onMouseLeave={this.onMouseLeave}
|
2019-05-02 11:48:32 +01:00
|
|
|
>
|
2023-09-01 04:16:24 -06:00
|
|
|
{reactionContent}
|
2019-12-06 15:48:34 +01:00
|
|
|
<span className="mx_ReactionsRowButton_count" aria-hidden="true">
|
2021-07-19 22:43:11 +01:00
|
|
|
{count}
|
2019-07-05 16:17:17 +01:00
|
|
|
</span>
|
2021-07-19 22:43:11 +01:00
|
|
|
{tooltip}
|
2019-12-07 12:20:06 +00:00
|
|
|
</AccessibleButton>
|
|
|
|
|
);
|
2019-05-01 18:05:11 +01:00
|
|
|
}
|
|
|
|
|
}
|