2019-10-15 19:07:04 +03:00
|
|
|
/*
|
2019-10-20 12:13:42 +03:00
|
|
|
Copyright 2019 Tulir Asokan <tulir@maunium.net>
|
2020-09-28 17:47:03 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2019-10-15 19:07:04 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2020-09-28 17:47:03 +03:00
|
|
|
import {MatrixEvent} from "matrix-js-sdk/src/models/event";
|
|
|
|
|
2019-10-15 19:07:04 +03:00
|
|
|
import EmojiPicker from "./EmojiPicker";
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2020-05-24 15:08:29 +03:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-03-09 06:04:46 +03:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2019-10-15 19:07:04 +03:00
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
interface IProps {
|
|
|
|
mxEvent: MatrixEvent;
|
|
|
|
reactions: any; // TODO type this once js-sdk is more typescripted
|
|
|
|
onFinished(): void;
|
|
|
|
}
|
2019-10-15 19:07:04 +03:00
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
interface IState {
|
|
|
|
selectedEmojis: Set<string>;
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:04:46 +03:00
|
|
|
@replaceableComponent("views.emojipicker.ReactionPicker")
|
2020-09-28 17:47:03 +03:00
|
|
|
class ReactionPicker extends React.Component<IProps, IState> {
|
2019-10-15 19:07:04 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
selectedEmojis: new Set(Object.keys(this.getReactions())),
|
|
|
|
};
|
|
|
|
this.addListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.reactions !== this.props.reactions) {
|
|
|
|
this.addListeners();
|
|
|
|
this.onReactionsChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
private addListeners() {
|
2019-10-15 19:07:04 +03:00
|
|
|
if (this.props.reactions) {
|
|
|
|
this.props.reactions.on("Relations.add", this.onReactionsChange);
|
|
|
|
this.props.reactions.on("Relations.remove", this.onReactionsChange);
|
|
|
|
this.props.reactions.on("Relations.redaction", this.onReactionsChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.props.reactions) {
|
2020-09-28 17:47:03 +03:00
|
|
|
this.props.reactions.removeListener("Relations.add", this.onReactionsChange);
|
|
|
|
this.props.reactions.removeListener("Relations.remove", this.onReactionsChange);
|
|
|
|
this.props.reactions.removeListener("Relations.redaction", this.onReactionsChange);
|
2019-10-15 19:07:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
private getReactions() {
|
2019-10-15 19:07:04 +03:00
|
|
|
if (!this.props.reactions) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const userId = MatrixClientPeg.get().getUserId();
|
2019-10-16 21:35:08 +03:00
|
|
|
const myAnnotations = this.props.reactions.getAnnotationsBySender()[userId] || [];
|
2019-10-15 19:07:04 +03:00
|
|
|
return Object.fromEntries([...myAnnotations]
|
|
|
|
.filter(event => !event.isRedacted())
|
|
|
|
.map(event => [event.getRelation().key, event.getId()]));
|
2019-10-22 15:49:02 +03:00
|
|
|
}
|
2019-10-15 19:07:04 +03:00
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
private onReactionsChange = () => {
|
2019-10-15 19:07:04 +03:00
|
|
|
this.setState({
|
2019-10-22 15:49:02 +03:00
|
|
|
selectedEmojis: new Set(Object.keys(this.getReactions())),
|
2019-10-15 19:07:04 +03:00
|
|
|
});
|
2020-09-28 17:47:03 +03:00
|
|
|
};
|
2019-10-15 19:07:04 +03:00
|
|
|
|
2020-09-28 17:47:03 +03:00
|
|
|
onChoose = (reaction: string) => {
|
2019-10-15 19:07:04 +03:00
|
|
|
this.componentWillUnmount();
|
|
|
|
this.props.onFinished();
|
|
|
|
const myReactions = this.getReactions();
|
|
|
|
if (myReactions.hasOwnProperty(reaction)) {
|
|
|
|
MatrixClientPeg.get().redactEvent(
|
|
|
|
this.props.mxEvent.getRoomId(),
|
|
|
|
myReactions[reaction],
|
|
|
|
);
|
|
|
|
// Tell the emoji picker not to bump this in the more frequently used list.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
MatrixClientPeg.get().sendEvent(this.props.mxEvent.getRoomId(), "m.reaction", {
|
|
|
|
"m.relates_to": {
|
|
|
|
"rel_type": "m.annotation",
|
|
|
|
"event_id": this.props.mxEvent.getId(),
|
|
|
|
"key": reaction,
|
|
|
|
},
|
|
|
|
});
|
2020-05-24 15:08:29 +03:00
|
|
|
dis.dispatch({action: "message_sent"});
|
2019-10-15 19:07:04 +03:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-28 17:47:03 +03:00
|
|
|
};
|
2019-10-15 19:07:04 +03:00
|
|
|
|
|
|
|
render() {
|
2019-10-22 15:49:02 +03:00
|
|
|
return <EmojiPicker
|
|
|
|
onChoose={this.onChoose}
|
|
|
|
selectedEmojis={this.state.selectedEmojis}
|
|
|
|
showQuickReactions={true}
|
|
|
|
/>;
|
2019-10-15 19:07:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:49:02 +03:00
|
|
|
export default ReactionPicker;
|