2021-06-20 17:09:18 -04:00
|
|
|
import Embed from "./embed/Embed";
|
2021-06-20 15:30:42 -04:00
|
|
|
import UserIcon from "../user/UserIcon";
|
|
|
|
import { Username } from "../user/UserShort";
|
2021-06-20 12:31:53 -04:00
|
|
|
import Markdown from "../../markdown/Markdown";
|
|
|
|
import { Children } from "../../../types/Preact";
|
2021-06-20 17:09:18 -04:00
|
|
|
import Attachment from "./attachments/Attachment";
|
2021-06-20 12:31:53 -04:00
|
|
|
import { attachContextMenu } from "preact-context-menu";
|
|
|
|
import { useUser } from "../../../context/revoltjs/hooks";
|
2021-06-22 05:59:06 -04:00
|
|
|
import { QueuedMessage } from "../../../redux/reducers/queue";
|
2021-06-20 12:31:53 -04:00
|
|
|
import { MessageObject } from "../../../context/revoltjs/util";
|
|
|
|
import MessageBase, { MessageContent, MessageDetail, MessageInfo } from "./MessageBase";
|
2021-06-22 05:59:06 -04:00
|
|
|
import Overline from "../../ui/Overline";
|
2021-06-22 08:28:03 -04:00
|
|
|
import { useContext } from "preact/hooks";
|
|
|
|
import { AppContext } from "../../../context/revoltjs/RevoltClient";
|
2021-06-23 11:14:46 -04:00
|
|
|
import { memo } from "preact/compat";
|
2021-06-20 12:31:53 -04:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
attachContext?: boolean
|
2021-06-22 05:59:06 -04:00
|
|
|
queued?: QueuedMessage
|
2021-06-20 12:31:53 -04:00
|
|
|
message: MessageObject
|
|
|
|
contrast?: boolean
|
|
|
|
content?: Children
|
|
|
|
head?: boolean
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:14:46 -04:00
|
|
|
function Message({ attachContext, message, contrast, content: replacement, head: preferHead, queued }: Props) {
|
2021-06-20 12:31:53 -04:00
|
|
|
// TODO: Can improve re-renders here by providing a list
|
|
|
|
// TODO: of dependencies. We only need to update on u/avatar.
|
2021-06-22 08:28:03 -04:00
|
|
|
const user = useUser(message.author);
|
|
|
|
const client = useContext(AppContext);
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-06-20 17:09:18 -04:00
|
|
|
const content = message.content as string;
|
2021-06-23 08:52:16 -04:00
|
|
|
const head = (message.replies && message.replies.length > 0) || preferHead;
|
2021-06-20 12:31:53 -04:00
|
|
|
return (
|
2021-06-21 16:11:53 -04:00
|
|
|
<MessageBase id={message._id}
|
2021-06-21 16:53:08 -04:00
|
|
|
head={head}
|
2021-06-22 05:59:06 -04:00
|
|
|
contrast={contrast}
|
|
|
|
sending={typeof queued !== 'undefined'}
|
2021-06-22 08:28:03 -04:00
|
|
|
mention={message.mentions?.includes(client.user!._id)}
|
2021-06-22 05:59:06 -04:00
|
|
|
failed={typeof queued?.error !== 'undefined'}
|
|
|
|
onContextMenu={attachContext ? attachContextMenu('Menu', { message, contextualChannel: message.channel, queued }) : undefined}>
|
2021-06-20 12:31:53 -04:00
|
|
|
<MessageInfo>
|
|
|
|
{ head ?
|
|
|
|
<UserIcon target={user} size={36} /> :
|
2021-06-21 16:11:53 -04:00
|
|
|
<MessageDetail message={message} position="left" /> }
|
2021-06-20 12:31:53 -04:00
|
|
|
</MessageInfo>
|
|
|
|
<MessageContent>
|
2021-06-21 16:11:53 -04:00
|
|
|
{ head && <span className="author">
|
|
|
|
<Username user={user} />
|
|
|
|
<MessageDetail message={message} position="top" />
|
|
|
|
</span> }
|
2021-06-21 08:28:26 -04:00
|
|
|
{ replacement ?? <Markdown content={content} /> }
|
2021-06-22 05:59:06 -04:00
|
|
|
{ queued?.error && <Overline type="error" error={queued.error} /> }
|
2021-06-20 17:09:18 -04:00
|
|
|
{ message.attachments?.map((attachment, index) =>
|
|
|
|
<Attachment key={index} attachment={attachment} hasContent={ index > 0 || content.length > 0 } />) }
|
|
|
|
{ message.embeds?.map((embed, index) =>
|
|
|
|
<Embed key={index} embed={embed} />) }
|
2021-06-20 12:31:53 -04:00
|
|
|
</MessageContent>
|
|
|
|
</MessageBase>
|
|
|
|
)
|
|
|
|
}
|
2021-06-23 11:14:46 -04:00
|
|
|
|
|
|
|
export default memo(Message);
|