import { myAuthRequired } from "@utils/app"; import { Component, InfernoNode, linkEvent } from "inferno"; import { CreatePrivateMessage, CreatePrivateMessageReport, DeletePrivateMessage, EditPrivateMessage, MarkPrivateMessageAsRead, Person, PrivateMessageView, } from "lemmy-js-client"; import { i18n } from "../../i18next"; import { mdToHtml } from "../../markdown"; import { UserService } from "../../services"; import { Icon, Spinner } 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?: string; deleteLoading: boolean; readLoading: boolean; reportLoading: boolean; } interface PrivateMessageProps { private_message_view: PrivateMessageView; onDelete(form: DeletePrivateMessage): void; onMarkRead(form: MarkPrivateMessageAsRead): void; onReport(form: CreatePrivateMessageReport): void; onCreate(form: CreatePrivateMessage): void; onEdit(form: EditPrivateMessage): void; } export class PrivateMessage extends Component< PrivateMessageProps, PrivateMessageState > { state: PrivateMessageState = { showReply: false, showEdit: false, collapsed: false, viewSource: false, showReportDialog: false, deleteLoading: false, readLoading: false, reportLoading: false, }; constructor(props: any, context: any) { super(props, context); this.handleReplyCancel = this.handleReplyCancel.bind(this); } get mine(): boolean { return ( UserService.Instance.myUserInfo?.local_user_view.person.id == this.props.private_message_view.creator.id ); } componentWillReceiveProps( nextProps: Readonly<{ children?: InfernoNode } & PrivateMessageProps> ): void { if (this.props != nextProps) { this.setState({ showReply: false, showEdit: false, collapsed: false, viewSource: false, showReportDialog: false, deleteLoading: false, readLoading: false, reportLoading: false, }); } } render() { const message_view = this.props.private_message_view; const otherPerson: Person = this.mine ? message_view.recipient : message_view.creator; return (
{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 { const 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) { i.setState({ deleteLoading: true }); i.props.onDelete({ private_message_id: i.props.private_message_view.private_message.id, deleted: !i.props.private_message_view.private_message.deleted, auth: myAuthRequired(), }); } handleReplyCancel() { this.setState({ showReply: false, showEdit: false }); } handleMarkRead(i: PrivateMessage) { i.setState({ readLoading: true }); i.props.onMarkRead({ private_message_id: i.props.private_message_view.private_message.id, read: !i.props.private_message_view.private_message.read, auth: myAuthRequired(), }); } 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: event.target.value }); } handleReportSubmit(i: PrivateMessage, event: any) { event.preventDefault(); i.setState({ reportLoading: true }); i.props.onReport({ private_message_id: i.props.private_message_view.private_message.id, reason: i.state.reportReason ?? "", auth: myAuthRequired(), }); } }