import { Component, linkEvent } from "inferno";
import {
PrivateMessageView,
DeletePrivateMessage,
MarkPrivateMessageAsRead,
PersonSafe,
} from "lemmy-js-client";
import { WebSocketService, UserService } from "../services";
import { authField, mdToHtml, toast, wsClient } from "../utils";
import { MomentTime } from "./moment-time";
import { PrivateMessageForm } from "./private-message-form";
import { PersonListing } from "./person-listing";
import { Icon } from "./icon";
import { i18n } from "../i18next";
interface PrivateMessageState {
showReply: boolean;
showEdit: boolean;
collapsed: boolean;
viewSource: boolean;
}
interface PrivateMessageProps {
private_message_view: PrivateMessageView;
}
export class PrivateMessage extends Component<
PrivateMessageProps,
PrivateMessageState
> {
private emptyState: PrivateMessageState = {
showReply: false,
showEdit: false,
collapsed: false,
viewSource: false,
};
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.localUserView &&
UserService.Instance.localUserView.person.id ==
this.props.private_message_view.creator.id
);
}
render() {
let message_view = this.props.private_message_view;
// TODO check this again
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.mine && (
<>
-
-
>
)}
-
)}
{this.state.showReply && (
)}
{/* A collapsed clearfix */}
{this.state.collapsed &&
}
);
}
get messageUnlessRemoved(): string {
let message = this.props.private_message_view.private_message;
return message.deleted ? `*${i18n.t("deleted")}*` : message.content;
}
handleReplyClick(i: PrivateMessage) {
i.state.showReply = true;
i.setState(i.state);
}
handleEditClick(i: PrivateMessage) {
i.state.showEdit = true;
i.setState(i.state);
}
handleDeleteClick(i: PrivateMessage) {
let form: DeletePrivateMessage = {
private_message_id: i.props.private_message_view.private_message.id,
deleted: !i.props.private_message_view.private_message.deleted,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.deletePrivateMessage(form));
}
handleReplyCancel() {
this.state.showReply = false;
this.state.showEdit = false;
this.setState(this.state);
}
handleMarkRead(i: PrivateMessage) {
let form: MarkPrivateMessageAsRead = {
private_message_id: i.props.private_message_view.private_message.id,
read: !i.props.private_message_view.private_message.read,
auth: authField(),
};
WebSocketService.Instance.send(wsClient.markPrivateMessageAsRead(form));
}
handleMessageCollapse(i: PrivateMessage) {
i.state.collapsed = !i.state.collapsed;
i.setState(i.state);
}
handleViewSource(i: PrivateMessage) {
i.state.viewSource = !i.state.viewSource;
i.setState(i.state);
}
handlePrivateMessageEdit() {
this.state.showEdit = false;
this.setState(this.state);
}
handlePrivateMessageCreate(message: PrivateMessageView) {
if (
UserService.Instance.localUserView &&
message.creator.id == UserService.Instance.localUserView.person.id
) {
this.state.showReply = false;
this.setState(this.state);
toast(i18n.t("message_sent"));
}
}
}