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,
25 pictshareAvatarThumbnail,
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);
113 componentWillUnmount() {
114 this.subscription.unsubscribe();
121 when={!this.state.loading && this.state.privateMessageForm.content}
122 message={i18n.t('block_leaving')}
124 <form onSubmit={linkEvent(this, this.handlePrivateMessageSubmit)}>
125 {!this.props.privateMessage && (
126 <div class="form-group row">
127 <label class="col-sm-2 col-form-label">
128 {capitalizeFirstLetter(i18n.t('to'))}
131 {this.state.recipient && (
132 <div class="col-sm-10 form-control-plaintext">
134 className="text-body font-weight-bold"
135 to={`/u/${this.state.recipient.name}`}
137 {this.state.recipient.avatar && showAvatars() && (
141 src={pictshareAvatarThumbnail(
142 this.state.recipient.avatar
144 class="rounded-circle mr-1"
147 <span>{this.state.recipient.name}</span>
153 <div class="form-group row">
154 <label class="col-sm-2 col-form-label">{i18n.t('message')}</label>
155 <div class="col-sm-10">
158 value={this.state.privateMessageForm.content}
159 onInput={linkEvent(this, this.handleContentChange)}
160 className={`form-control ${this.state.previewMode && 'd-none'}`}
164 {this.state.previewMode && (
167 dangerouslySetInnerHTML={mdToHtml(
168 this.state.privateMessageForm.content
173 <ul class="float-right list-inline mb-1 text-muted small font-weight-bold">
174 <li class="list-inline-item">
176 onClick={linkEvent(this, this.handleShowDisclaimer)}
179 {i18n.t('disclaimer')}
182 <li class="list-inline-item">
183 <a href={markdownHelpUrl} target="_blank" class="text-muted">
184 {i18n.t('formatting_help')}
191 {this.state.showDisclaimer && (
192 <div class="form-group row">
193 <div class="col-sm-10">
194 <div class="alert alert-danger" role="alert">
195 <T i18nKey="private_message_disclaimer">
200 href="https://about.riot.im/"
209 <div class="form-group row">
210 <div class="col-sm-10">
211 <button type="submit" class="btn btn-secondary mr-2">
212 {this.state.loading ? (
213 <svg class="icon icon-spinner spin">
214 <use xlinkHref="#icon-spinner"></use>
216 ) : this.props.privateMessage ? (
217 capitalizeFirstLetter(i18n.t('save'))
219 capitalizeFirstLetter(i18n.t('send_message'))
222 {this.state.privateMessageForm.content && (
224 className={`btn btn-secondary mr-2 ${this.state.previewMode &&
226 onClick={linkEvent(this, this.handlePreviewToggle)}
231 {this.props.privateMessage && (
234 class="btn btn-secondary"
235 onClick={linkEvent(this, this.handleCancel)}
247 handlePrivateMessageSubmit(i: PrivateMessageForm, event: any) {
248 event.preventDefault();
249 if (i.props.privateMessage) {
250 let editForm: EditPrivateMessageForm = {
251 edit_id: i.props.privateMessage.id,
252 content: i.state.privateMessageForm.content,
254 WebSocketService.Instance.editPrivateMessage(editForm);
256 WebSocketService.Instance.createPrivateMessage(
257 i.state.privateMessageForm
260 i.state.loading = true;
264 handleRecipientChange(i: PrivateMessageForm, event: any) {
265 i.state.recipient = event.target.value;
269 handleContentChange(i: PrivateMessageForm, event: any) {
270 i.state.privateMessageForm.content = event.target.value;
274 handleCancel(i: PrivateMessageForm) {
278 handlePreviewToggle(i: PrivateMessageForm, event: any) {
279 event.preventDefault();
280 i.state.previewMode = !i.state.previewMode;
284 handleShowDisclaimer(i: PrivateMessageForm) {
285 i.state.showDisclaimer = !i.state.showDisclaimer;
289 parseMessage(msg: WebSocketJsonResponse) {
290 let res = wsJsonToRes(msg);
292 toast(i18n.t(msg.error), 'danger');
293 this.state.loading = false;
294 this.setState(this.state);
296 } else if (res.op == UserOperation.EditPrivateMessage) {
297 let data = res.data as PrivateMessageResponse;
298 this.state.loading = false;
299 this.props.onEdit(data.message);
300 } else if (res.op == UserOperation.GetUserDetails) {
301 let data = res.data as UserDetailsResponse;
302 this.state.recipient = data.user;
303 this.state.privateMessageForm.recipient_id = data.user.id;
304 this.setState(this.state);
305 } else if (res.op == UserOperation.CreatePrivateMessage) {
306 let data = res.data as PrivateMessageResponse;
307 this.state.loading = false;
308 this.props.onCreate(data.message);
309 this.setState(this.state);