2017-09-29 22:14:56 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Travis Ralston
|
|
|
|
|
|
|
|
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";
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2017-09-29 22:14:56 +03:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
|
|
|
import MessageEvent from "../messages/MessageEvent";
|
|
|
|
import MemberAvatar from "../avatars/MemberAvatar";
|
|
|
|
import { _t } from '../../../languageHandler';
|
2018-05-27 04:42:09 +03:00
|
|
|
import {formatFullDate} from '../../../DateUtils';
|
2017-09-29 22:14:56 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class PinnedEventTile extends React.Component {
|
|
|
|
static propTypes = {
|
2017-12-26 04:03:18 +03:00
|
|
|
mxRoom: PropTypes.object.isRequired,
|
|
|
|
mxEvent: PropTypes.object.isRequired,
|
|
|
|
onUnpinned: PropTypes.func,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
onTileClicked = () => {
|
2017-09-29 22:14:56 +03:00
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
event_id: this.props.mxEvent.getId(),
|
|
|
|
highlighted: true,
|
|
|
|
room_id: this.props.mxEvent.getRoomId(),
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
onUnpinClicked = () => {
|
2017-09-29 22:14:56 +03:00
|
|
|
const pinnedEvents = this.props.mxRoom.currentState.getStateEvents("m.room.pinned_events", "");
|
|
|
|
if (!pinnedEvents || !pinnedEvents.getContent().pinned) {
|
|
|
|
// Nothing to do: already unpinned
|
|
|
|
if (this.props.onUnpinned) this.props.onUnpinned();
|
|
|
|
} else {
|
|
|
|
const pinned = pinnedEvents.getContent().pinned;
|
|
|
|
const index = pinned.indexOf(this.props.mxEvent.getId());
|
|
|
|
if (index !== -1) {
|
|
|
|
pinned.splice(index, 1);
|
2017-10-15 03:47:11 +03:00
|
|
|
MatrixClientPeg.get().sendStateEvent(this.props.mxRoom.roomId, 'm.room.pinned_events', {pinned}, '')
|
|
|
|
.then(() => {
|
2017-09-29 22:14:56 +03:00
|
|
|
if (this.props.onUnpinned) this.props.onUnpinned();
|
|
|
|
});
|
|
|
|
} else if (this.props.onUnpinned) this.props.onUnpinned();
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
_canUnpin() {
|
2017-09-29 22:14:56 +03:00
|
|
|
return this.props.mxRoom.currentState.mayClientSendStateEvent('m.room.pinned_events', MatrixClientPeg.get());
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-10-31 14:21:58 +03:00
|
|
|
const sender = this.props.mxEvent.getSender();
|
|
|
|
// Get the latest sender profile rather than historical
|
|
|
|
const senderProfile = this.props.mxRoom.getMember(sender);
|
2017-09-29 22:14:56 +03:00
|
|
|
const avatarSize = 40;
|
|
|
|
|
|
|
|
let unpinButton = null;
|
|
|
|
if (this._canUnpin()) {
|
2017-09-29 22:32:25 +03:00
|
|
|
unpinButton = (
|
|
|
|
<AccessibleButton onClick={this.onUnpinClicked} className="mx_PinnedEventTile_unpinButton">
|
2019-01-11 04:37:28 +03:00
|
|
|
<img src={require("../../../../res/img/cancel-red.svg")} width="8" height="8" alt={_t('Unpin Message')} title={_t('Unpin Message')} />
|
2017-09-29 22:32:25 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
);
|
2017-09-29 22:14:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mx_PinnedEventTile">
|
|
|
|
<div className="mx_PinnedEventTile_actions">
|
|
|
|
<AccessibleButton className="mx_PinnedEventTile_gotoButton mx_textButton" onClick={this.onTileClicked}>
|
|
|
|
{ _t("Jump to message") }
|
|
|
|
</AccessibleButton>
|
|
|
|
{ unpinButton }
|
|
|
|
</div>
|
|
|
|
|
2018-05-27 03:21:24 +03:00
|
|
|
<span className="mx_PinnedEventTile_senderAvatar">
|
2019-10-31 14:21:58 +03:00
|
|
|
<MemberAvatar member={senderProfile} width={avatarSize} height={avatarSize} fallbackUserId={sender} />
|
2018-05-27 03:21:24 +03:00
|
|
|
</span>
|
2017-09-29 22:14:56 +03:00
|
|
|
<span className="mx_PinnedEventTile_sender">
|
2019-10-31 14:21:58 +03:00
|
|
|
{ senderProfile ? senderProfile.name : sender }
|
2017-09-29 22:14:56 +03:00
|
|
|
</span>
|
2018-05-27 04:42:09 +03:00
|
|
|
<span className="mx_PinnedEventTile_timestamp">
|
|
|
|
{ formatFullDate(new Date(this.props.mxEvent.getTs())) }
|
|
|
|
</span>
|
2018-05-27 03:40:48 +03:00
|
|
|
<div className="mx_PinnedEventTile_message">
|
|
|
|
<MessageEvent mxEvent={this.props.mxEvent} className="mx_PinnedEventTile_body" maxImageHeight={150}
|
2019-03-06 14:27:16 +03:00
|
|
|
onHeightChanged={() => {}} // we need to give this, apparently
|
2018-05-27 03:40:48 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2017-09-29 22:14:56 +03:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|