import { Component, linkEvent } from "inferno"; import { Prompt } from "inferno-router"; import { Subscription } from "rxjs"; import { CreatePrivateMessage, EditPrivateMessage, PrivateMessageView, PrivateMessageResponse, PersonSafe, UserOperation, } from "lemmy-js-client"; import { WebSocketService } from "../services"; import { capitalizeFirstLetter, wsJsonToRes, toast, setupTippy, wsSubscribe, isBrowser, wsUserOp, wsClient, authField, } from "../utils"; import { PersonListing } from "./person-listing"; import { MarkdownTextArea } from "./markdown-textarea"; import { Icon, Spinner } from "./icon"; import { i18n } from "../i18next"; import { T } from "inferno-i18next"; interface PrivateMessageFormProps { recipient: PersonSafe; privateMessage?: PrivateMessageView; // If a pm is given, that means this is an edit onCancel?(): any; onCreate?(message: PrivateMessageView): any; onEdit?(message: PrivateMessageView): any; } interface PrivateMessageFormState { privateMessageForm: CreatePrivateMessage; loading: boolean; previewMode: boolean; showDisclaimer: boolean; } export class PrivateMessageForm extends Component< PrivateMessageFormProps, PrivateMessageFormState > { private subscription: Subscription; private emptyState: PrivateMessageFormState = { privateMessageForm: { content: null, recipient_id: this.props.recipient.id, auth: authField(), }, loading: false, previewMode: false, showDisclaimer: false, }; constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.handleContentChange = this.handleContentChange.bind(this); this.parseMessage = this.parseMessage.bind(this); this.subscription = wsSubscribe(this.parseMessage); // Its an edit if (this.props.privateMessage) { this.state.privateMessageForm.content = this.props.privateMessage.private_message.content; } } componentDidMount() { setupTippy(); } componentDidUpdate() { if (!this.state.loading && this.state.privateMessageForm.content) { window.onbeforeunload = () => true; } else { window.onbeforeunload = undefined; } } componentWillUnmount() { if (isBrowser()) { this.subscription.unsubscribe(); window.onbeforeunload = null; } } render() { return (