import { None, Option, Some } from "@sniptt/monads/build"; import { Component, linkEvent } from "inferno"; import { CreatePrivateMessageReport, DeletePrivateMessage, MarkPrivateMessageAsRead, PersonSafe, PrivateMessageView, toUndefined, } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { UserService, WebSocketService } from "../../services"; import { auth, mdToHtml, toast, wsClient } from "../../utils"; import { Icon } from "../common/icon"; import { MomentTime } from "../common/moment-time"; import { PersonListing } from "../person/person-listing"; import { PrivateMessageForm } from "./private-message-form"; interface PrivateMessageState { showReply: boolean; showEdit: boolean; collapsed: boolean; viewSource: boolean; showReportDialog: boolean; reportReason: Option; } interface PrivateMessageProps { private_message_view: PrivateMessageView; } export class PrivateMessage extends Component< PrivateMessageProps, PrivateMessageState > { private emptyState: PrivateMessageState = { showReply: false, showEdit: false, collapsed: false, viewSource: false, showReportDialog: false, reportReason: None, }; constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.handleReplyCancel = this.handleReplyCancel.bind(this); this.handlePrivateMessageCreate = this.handlePrivateMessageCreate.bind(this); this.handlePrivateMessageEdit = this.handlePrivateMessageEdit.bind(this); } get mine(): boolean { return UserService.Instance.myUserInfo .map( m => m.local_user_view.person.id == this.props.private_message_view.creator.id ) .unwrapOr(false); } render() { let message_view = this.props.private_message_view; let otherPerson: PersonSafe = this.mine ? message_view.recipient : message_view.creator; return (
    {/* TODO refactor this */}
  • {this.mine ? i18n.t("to") : i18n.t("from")}
  • {this.state.collapsed ? ( ) : ( )}
{this.state.showEdit && ( )} {!this.state.showEdit && !this.state.collapsed && (
{this.state.viewSource ? (
{this.messageUnlessRemoved}
) : (
)}
    {!this.mine && ( <>
  • {this.reportButton}
  • )} {this.mine && ( <>
  • )}
)}
{this.state.showReportDialog && (
)} {this.state.showReply && ( )} {/* A collapsed clearfix */} {this.state.collapsed &&
}
); } get reportButton() { return ( ); } get messageUnlessRemoved(): string { let message = this.props.private_message_view.private_message; return message.deleted ? `*${i18n.t("deleted")}*` : message.content; } handleReplyClick(i: PrivateMessage) { i.setState({ showReply: true }); } handleEditClick(i: PrivateMessage) { i.setState({ showEdit: true }); i.setState(i.state); } handleDeleteClick(i: PrivateMessage) { let form = new DeletePrivateMessage({ private_message_id: i.props.private_message_view.private_message.id, deleted: !i.props.private_message_view.private_message.deleted, auth: auth().unwrap(), }); WebSocketService.Instance.send(wsClient.deletePrivateMessage(form)); } handleReplyCancel() { this.setState({ showReply: false, showEdit: false }); } handleMarkRead(i: PrivateMessage) { let form = new MarkPrivateMessageAsRead({ private_message_id: i.props.private_message_view.private_message.id, read: !i.props.private_message_view.private_message.read, auth: auth().unwrap(), }); WebSocketService.Instance.send(wsClient.markPrivateMessageAsRead(form)); } handleMessageCollapse(i: PrivateMessage) { i.setState({ collapsed: !i.state.collapsed }); } handleViewSource(i: PrivateMessage) { i.setState({ viewSource: !i.state.viewSource }); } handleShowReportDialog(i: PrivateMessage) { i.setState({ showReportDialog: !i.state.showReportDialog }); } handleReportReasonChange(i: PrivateMessage, event: any) { i.setState({ reportReason: Some(event.target.value) }); } handleReportSubmit(i: PrivateMessage, event: any) { event.preventDefault(); let form = new CreatePrivateMessageReport({ private_message_id: i.props.private_message_view.private_message.id, reason: toUndefined(i.state.reportReason), auth: auth().unwrap(), }); WebSocketService.Instance.send(wsClient.createPrivateMessageReport(form)); i.setState({ showReportDialog: false }); } handlePrivateMessageEdit() { this.setState({ showEdit: false }); } handlePrivateMessageCreate(message: PrivateMessageView) { UserService.Instance.myUserInfo.match({ some: mui => { if (message.creator.id == mui.local_user_view.person.id) { this.setState({ showReply: false }); toast(i18n.t("message_sent")); } }, none: void 0, }); } }