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,
138 id: this.state.recipient.id,
139 local: this.state.recipient.local,
140 actor_id: this.state.recipient.actor_id,
147 <div class="form-group row">
148 <label class="col-sm-2 col-form-label">{i18n.t('message')}</label>
149 <div class="col-sm-10">
152 value={this.state.privateMessageForm.content}
153 onInput={linkEvent(this, this.handleContentChange)}
154 className={`form-control ${this.state.previewMode && 'd-none'}`}
158 {this.state.previewMode && (
161 dangerouslySetInnerHTML={mdToHtml(
162 this.state.privateMessageForm.content
167 <ul class="float-right list-inline mb-1 text-muted font-weight-bold">
168 <li class="list-inline-item">
170 onClick={linkEvent(this, this.handleShowDisclaimer)}
172 data-tippy-content={i18n.t('disclaimer')}
174 <svg class={`icon icon-inline`}>
175 <use xlinkHref="#icon-alert-triangle"></use>
179 <li class="list-inline-item">
181 href={markdownHelpUrl}
184 title={i18n.t('formatting_help')}
186 <svg class="icon icon-inline">
187 <use xlinkHref="#icon-help-circle"></use>
195 {this.state.showDisclaimer && (
196 <div class="form-group row">
197 <div class="col-sm-10">
198 <div class="alert alert-danger" role="alert">
199 <T i18nKey="private_message_disclaimer">
204 href="https://about.riot.im/"
213 <div class="form-group row">
214 <div class="col-sm-10">
215 <button type="submit" class="btn btn-secondary mr-2">
216 {this.state.loading ? (
217 <svg class="icon icon-spinner spin">
218 <use xlinkHref="#icon-spinner"></use>
220 ) : this.props.privateMessage ? (
221 capitalizeFirstLetter(i18n.t('save'))
223 capitalizeFirstLetter(i18n.t('send_message'))
226 {this.state.privateMessageForm.content && (
228 className={`btn btn-secondary mr-2 ${
229 this.state.previewMode && 'active'
231 onClick={linkEvent(this, this.handlePreviewToggle)}
236 {this.props.privateMessage && (
239 class="btn btn-secondary"
240 onClick={linkEvent(this, this.handleCancel)}
252 handlePrivateMessageSubmit(i: PrivateMessageForm, event: any) {
253 event.preventDefault();
254 if (i.props.privateMessage) {
255 let editForm: EditPrivateMessageForm = {
256 edit_id: i.props.privateMessage.id,
257 content: i.state.privateMessageForm.content,
259 WebSocketService.Instance.editPrivateMessage(editForm);
261 WebSocketService.Instance.createPrivateMessage(
262 i.state.privateMessageForm
265 i.state.loading = true;
269 handleRecipientChange(i: PrivateMessageForm, event: any) {
270 i.state.recipient = event.target.value;
274 handleContentChange(i: PrivateMessageForm, event: any) {
275 i.state.privateMessageForm.content = event.target.value;
279 handleCancel(i: PrivateMessageForm) {
283 handlePreviewToggle(i: PrivateMessageForm, event: any) {
284 event.preventDefault();
285 i.state.previewMode = !i.state.previewMode;
289 handleShowDisclaimer(i: PrivateMessageForm) {
290 i.state.showDisclaimer = !i.state.showDisclaimer;
294 parseMessage(msg: WebSocketJsonResponse) {
295 let res = wsJsonToRes(msg);
297 toast(i18n.t(msg.error), 'danger');
298 this.state.loading = false;
299 this.setState(this.state);
301 } else if (res.op == UserOperation.EditPrivateMessage) {
302 let data = res.data as PrivateMessageResponse;
303 this.state.loading = false;
304 this.props.onEdit(data.message);
305 } else if (res.op == UserOperation.GetUserDetails) {
306 let data = res.data as UserDetailsResponse;
307 this.state.recipient = data.user;
308 this.state.privateMessageForm.recipient_id = data.user.id;
309 this.setState(this.state);
310 } else if (res.op == UserOperation.CreatePrivateMessage) {
311 let data = res.data as PrivateMessageResponse;
312 this.state.loading = false;
313 this.props.onCreate(data.message);
314 this.setState(this.state);