1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
4 PrivateMessage as PrivateMessageI,
5 EditPrivateMessageForm,
6 } from '../interfaces';
7 import { WebSocketService, UserService } from '../services';
10 pictshareAvatarThumbnail,
14 import { MomentTime } from './moment-time';
15 import { PrivateMessageForm } from './private-message-form';
16 import { i18n } from '../i18next';
18 interface PrivateMessageState {
25 interface PrivateMessageProps {
26 privateMessage: PrivateMessageI;
29 export class PrivateMessage extends Component<
33 private emptyState: PrivateMessageState = {
40 constructor(props: any, context: any) {
41 super(props, context);
43 this.state = this.emptyState;
44 this.handleReplyCancel = this.handleReplyCancel.bind(this);
45 this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(
48 this.handlePrivateMessageEdit = this.handlePrivateMessageEdit.bind(this);
52 return UserService.Instance.user.id == this.props.privateMessage.creator_id;
56 let message = this.props.privateMessage;
60 <ul class="list-inline mb-0 text-muted small">
61 <li className="list-inline-item">
62 {this.mine ? i18n.t('to') : i18n.t('from')}
64 <li className="list-inline-item">
66 className="text-body font-weight-bold"
69 ? `/u/${message.recipient_name}`
70 : `/u/${message.creator_name}`
74 ? message.recipient_avatar
75 : message.creator_avatar) &&
80 src={pictshareAvatarThumbnail(
82 ? message.recipient_avatar
83 : message.creator_avatar
85 class="rounded-circle mr-1"
89 {this.mine ? message.recipient_name : message.creator_name}
93 <li className="list-inline-item">
95 <MomentTime data={message} />
98 <li className="list-inline-item">
100 className="pointer text-monospace"
101 onClick={linkEvent(this, this.handleMessageCollapse)}
103 {this.state.collapsed ? (
104 <svg class="icon icon-inline">
105 <use xlinkHref="#icon-plus-square"></use>
108 <svg class="icon icon-inline">
109 <use xlinkHref="#icon-minus-square"></use>
115 {this.state.showEdit && (
117 privateMessage={message}
118 onEdit={this.handlePrivateMessageEdit}
119 onCancel={this.handleReplyCancel}
122 {!this.state.showEdit && !this.state.collapsed && (
124 {this.state.viewSource ? (
125 <pre>{this.messageUnlessRemoved}</pre>
129 dangerouslySetInnerHTML={mdToHtml(this.messageUnlessRemoved)}
132 <ul class="list-inline mb-1 text-muted h5 font-weight-bold">
135 <li className="list-inline-item-action">
138 onClick={linkEvent(this, this.handleMarkRead)}
141 ? i18n.t('mark_as_unread')
142 : i18n.t('mark_as_read')
146 class={`icon icon-inline ${message.read &&
149 <use xlinkHref="#icon-check"></use>
153 <li className="list-inline-item-action">
156 onClick={linkEvent(this, this.handleReplyClick)}
157 data-tippy-content={i18n.t('reply')}
159 <svg class="icon icon-inline">
160 <use xlinkHref="#icon-reply1"></use>
168 <li className="list-inline-item-action">
171 onClick={linkEvent(this, this.handleEditClick)}
172 data-tippy-content={i18n.t('edit')}
174 <svg class="icon icon-inline">
175 <use xlinkHref="#icon-edit"></use>
179 <li className="list-inline-item-action">
182 onClick={linkEvent(this, this.handleDeleteClick)}
190 class={`icon icon-inline ${message.deleted &&
193 <use xlinkHref="#icon-trash"></use>
199 <li className="list-inline-item-action">
202 onClick={linkEvent(this, this.handleViewSource)}
203 data-tippy-content={i18n.t('view_source')}
206 class={`icon icon-inline ${this.state.viewSource &&
209 <use xlinkHref="#icon-file-text"></use>
217 {this.state.showReply && (
220 recipient_id: this.props.privateMessage.creator_id,
222 onCreate={this.handlePrivateMessageCreate}
225 {/* A collapsed clearfix */}
226 {this.state.collapsed && <div class="row col-12"></div>}
231 get messageUnlessRemoved(): string {
232 let message = this.props.privateMessage;
233 return message.deleted ? `*${i18n.t('deleted')}*` : message.content;
236 handleReplyClick(i: PrivateMessage) {
237 i.state.showReply = true;
241 handleEditClick(i: PrivateMessage) {
242 i.state.showEdit = true;
246 handleDeleteClick(i: PrivateMessage) {
247 let form: EditPrivateMessageForm = {
248 edit_id: i.props.privateMessage.id,
249 deleted: !i.props.privateMessage.deleted,
251 WebSocketService.Instance.editPrivateMessage(form);
254 handleReplyCancel() {
255 this.state.showReply = false;
256 this.state.showEdit = false;
257 this.setState(this.state);
260 handleMarkRead(i: PrivateMessage) {
261 let form: EditPrivateMessageForm = {
262 edit_id: i.props.privateMessage.id,
263 read: !i.props.privateMessage.read,
265 WebSocketService.Instance.editPrivateMessage(form);
268 handleMessageCollapse(i: PrivateMessage) {
269 i.state.collapsed = !i.state.collapsed;
273 handleViewSource(i: PrivateMessage) {
274 i.state.viewSource = !i.state.viewSource;
278 handlePrivateMessageEdit() {
279 this.state.showEdit = false;
280 this.setState(this.state);
283 handlePrivateMessageCreate() {
284 this.state.showReply = false;
285 this.setState(this.state);
286 toast(i18n.t('message_sent'));