1 import { Component, linkEvent } from 'inferno';
2 import { Prompt } from 'inferno-router';
3 import { Link } from 'inferno-router';
4 import { Subscription } from 'rxjs';
5 import { retryWhen, delay, take } from 'rxjs/operators';
7 PrivateMessageForm as PrivateMessageFormI,
8 EditPrivateMessageForm,
9 PrivateMessageFormParams,
11 PrivateMessageResponse,
17 WebSocketJsonResponse,
18 } from '../interfaces';
19 import { WebSocketService } from '../services';
21 capitalizeFirstLetter,
30 import { UserListing } from './user-listing';
31 import Tribute from 'tributejs/src/Tribute.js';
32 import autosize from 'autosize';
33 import { i18n } from '../i18next';
34 import { T } from 'inferno-i18next';
36 interface PrivateMessageFormProps {
37 privateMessage?: PrivateMessage; // If a pm is given, that means this is an edit
38 params?: PrivateMessageFormParams;
40 onCreate?(message: PrivateMessage): any;
41 onEdit?(message: PrivateMessage): any;
44 interface PrivateMessageFormState {
45 privateMessageForm: PrivateMessageFormI;
49 showDisclaimer: boolean;
52 export class PrivateMessageForm extends Component<
53 PrivateMessageFormProps,
54 PrivateMessageFormState
56 private id = `message-form-${randomStr()}`;
57 private tribute: Tribute;
58 private subscription: Subscription;
59 private emptyState: PrivateMessageFormState = {
67 showDisclaimer: false,
70 constructor(props: any, context: any) {
71 super(props, context);
73 this.tribute = setupTribute();
74 this.state = this.emptyState;
76 if (this.props.privateMessage) {
77 this.state.privateMessageForm = {
78 content: this.props.privateMessage.content,
79 recipient_id: this.props.privateMessage.recipient_id,
83 if (this.props.params) {
84 this.state.privateMessageForm.recipient_id = this.props.params.recipient_id;
85 let form: GetUserDetailsForm = {
86 user_id: this.state.privateMessageForm.recipient_id,
87 sort: SortType[SortType.New],
90 WebSocketService.Instance.getUserDetails(form);
93 this.subscription = WebSocketService.Instance.subject
94 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
96 msg => this.parseMessage(msg),
97 err => console.error(err),
98 () => console.log('complete')
102 componentDidMount() {
103 var textarea: any = document.getElementById(this.id);
105 this.tribute.attach(textarea);
106 textarea.addEventListener('tribute-replaced', () => {
107 this.state.privateMessageForm.content = textarea.value;
108 this.setState(this.state);
109 autosize.update(textarea);
114 componentWillUnmount() {
115 this.subscription.unsubscribe();
122 when={!this.state.loading && this.state.privateMessageForm.content}
123 message={i18n.t('block_leaving')}
125 <form onSubmit={linkEvent(this, this.handlePrivateMessageSubmit)}>
126 {!this.props.privateMessage && (
127 <div class="form-group row">
128 <label class="col-sm-2 col-form-label">
129 {capitalizeFirstLetter(i18n.t('to'))}
132 {this.state.recipient && (
133 <div class="col-sm-10 form-control-plaintext">
136 name: this.state.recipient.name,
137 avatar: this.state.recipient.avatar,
144 <div class="form-group row">
145 <label class="col-sm-2 col-form-label">{i18n.t('message')}</label>
146 <div class="col-sm-10">
149 value={this.state.privateMessageForm.content}
150 onInput={linkEvent(this, this.handleContentChange)}
151 className={`form-control ${this.state.previewMode && 'd-none'}`}
155 {this.state.previewMode && (
157 className="card card-body md-div"
158 dangerouslySetInnerHTML={mdToHtml(
159 this.state.privateMessageForm.content
166 {this.state.showDisclaimer && (
167 <div class="form-group row">
168 <div class="offset-sm-2 col-sm-10">
169 <div class="alert alert-danger" role="alert">
170 <T i18nKey="private_message_disclaimer">
175 href="https://about.riot.im/"
184 <div class="form-group row">
185 <div class="offset-sm-2 col-sm-10">
186 <button type="submit" class="btn btn-secondary mr-2">
187 {this.state.loading ? (
188 <svg class="icon icon-spinner spin">
189 <use xlinkHref="#icon-spinner"></use>
191 ) : this.props.privateMessage ? (
192 capitalizeFirstLetter(i18n.t('save'))
194 capitalizeFirstLetter(i18n.t('send_message'))
197 {this.state.privateMessageForm.content && (
199 className={`btn btn-secondary mr-2 ${
200 this.state.previewMode && 'active'
202 onClick={linkEvent(this, this.handlePreviewToggle)}
207 {this.props.privateMessage && (
210 class="btn btn-secondary"
211 onClick={linkEvent(this, this.handleCancel)}
216 <ul class="d-inline-block float-right list-inline mb-1 text-muted font-weight-bold">
217 <li class="list-inline-item">
219 onClick={linkEvent(this, this.handleShowDisclaimer)}
221 data-tippy-content={i18n.t('disclaimer')}
223 <svg class={`icon icon-inline`}>
224 <use xlinkHref="#icon-alert-triangle"></use>
228 <li class="list-inline-item">
230 href={markdownHelpUrl}
233 title={i18n.t('formatting_help')}
235 <svg class="icon icon-inline">
236 <use xlinkHref="#icon-help-circle"></use>
248 handlePrivateMessageSubmit(i: PrivateMessageForm, event: any) {
249 event.preventDefault();
250 if (i.props.privateMessage) {
251 let editForm: EditPrivateMessageForm = {
252 edit_id: i.props.privateMessage.id,
253 content: i.state.privateMessageForm.content,
255 WebSocketService.Instance.editPrivateMessage(editForm);
257 WebSocketService.Instance.createPrivateMessage(
258 i.state.privateMessageForm
261 i.state.loading = true;
265 handleRecipientChange(i: PrivateMessageForm, event: any) {
266 i.state.recipient = event.target.value;
270 handleContentChange(i: PrivateMessageForm, event: any) {
271 i.state.privateMessageForm.content = event.target.value;
275 handleCancel(i: PrivateMessageForm) {
279 handlePreviewToggle(i: PrivateMessageForm, event: any) {
280 event.preventDefault();
281 i.state.previewMode = !i.state.previewMode;
285 handleShowDisclaimer(i: PrivateMessageForm) {
286 i.state.showDisclaimer = !i.state.showDisclaimer;
290 parseMessage(msg: WebSocketJsonResponse) {
291 let res = wsJsonToRes(msg);
293 toast(i18n.t(msg.error), 'danger');
294 this.state.loading = false;
295 this.setState(this.state);
297 } else if (res.op == UserOperation.EditPrivateMessage) {
298 let data = res.data as PrivateMessageResponse;
299 this.state.loading = false;
300 this.props.onEdit(data.message);
301 } else if (res.op == UserOperation.GetUserDetails) {
302 let data = res.data as UserDetailsResponse;
303 this.state.recipient = data.user;
304 this.state.privateMessageForm.recipient_id = data.user.id;
305 this.setState(this.state);
306 } else if (res.op == UserOperation.CreatePrivateMessage) {
307 let data = res.data as PrivateMessageResponse;
308 this.state.loading = false;
309 this.props.onCreate(data.message);
310 this.setState(this.state);