2021-07-29 10:51:19 -04:00
|
|
|
import { observer } from "mobx-react-lite";
|
2021-07-30 15:24:53 -04:00
|
|
|
import { Message as MessageObject } from "revolt.js/dist/maps/Messages";
|
2021-07-29 10:51:19 -04:00
|
|
|
|
2021-06-20 12:31:53 -04:00
|
|
|
import { attachContextMenu } from "preact-context-menu";
|
2021-07-05 06:23:23 -04:00
|
|
|
import { memo } from "preact/compat";
|
2021-07-30 15:24:53 -04:00
|
|
|
import { useState } from "preact/hooks";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-22 05:59:06 -04:00
|
|
|
import { QueuedMessage } from "../../../redux/reducers/queue";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { useIntermediate } from "../../../context/intermediate/Intermediate";
|
2021-07-30 15:24:53 -04:00
|
|
|
import { useClient } from "../../../context/revoltjs/RevoltClient";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-22 05:59:06 -04:00
|
|
|
import Overline from "../../ui/Overline";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { Children } from "../../../types/Preact";
|
|
|
|
import Markdown from "../../markdown/Markdown";
|
|
|
|
import UserIcon from "../user/UserIcon";
|
|
|
|
import { Username } from "../user/UserShort";
|
|
|
|
import MessageBase, {
|
2021-07-05 06:25:20 -04:00
|
|
|
MessageContent,
|
|
|
|
MessageDetail,
|
|
|
|
MessageInfo,
|
2021-07-05 06:23:23 -04:00
|
|
|
} from "./MessageBase";
|
|
|
|
import Attachment from "./attachments/Attachment";
|
2021-06-23 13:26:41 -04:00
|
|
|
import { MessageReply } from "./attachments/MessageReply";
|
2021-07-05 06:23:23 -04:00
|
|
|
import Embed from "./embed/Embed";
|
2021-06-20 12:31:53 -04:00
|
|
|
|
|
|
|
interface Props {
|
2021-07-05 06:25:20 -04:00
|
|
|
attachContext?: boolean;
|
|
|
|
queued?: QueuedMessage;
|
|
|
|
message: MessageObject;
|
2021-07-09 04:58:38 -04:00
|
|
|
highlight?: boolean;
|
2021-07-05 06:25:20 -04:00
|
|
|
contrast?: boolean;
|
|
|
|
content?: Children;
|
|
|
|
head?: boolean;
|
2021-06-20 12:31:53 -04:00
|
|
|
}
|
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
const Message = observer(
|
|
|
|
({
|
|
|
|
highlight,
|
|
|
|
attachContext,
|
|
|
|
message,
|
|
|
|
contrast,
|
|
|
|
content: replacement,
|
|
|
|
head: preferHead,
|
|
|
|
queued,
|
|
|
|
}: Props) => {
|
2021-07-30 15:24:53 -04:00
|
|
|
const client = useClient();
|
|
|
|
const user = message.author;
|
2021-07-29 10:51:19 -04:00
|
|
|
|
|
|
|
const { openScreen } = useIntermediate();
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
const content = message.content as string;
|
|
|
|
const head =
|
2021-07-30 15:24:53 -04:00
|
|
|
preferHead || (message.reply_ids && message.reply_ids.length > 0);
|
2021-07-02 08:35:50 -04:00
|
|
|
|
2021-07-31 05:31:16 -04:00
|
|
|
// ! TODO: tell fatal to make this type generic
|
2021-07-29 10:51:19 -04:00
|
|
|
// bree: Fatal please...
|
|
|
|
const userContext = attachContext
|
|
|
|
? (attachContextMenu("Menu", {
|
2021-07-31 05:31:16 -04:00
|
|
|
user: message.author_id,
|
|
|
|
contextualChannel: message.channel_id,
|
2021-07-29 10:51:19 -04:00
|
|
|
}) as any)
|
|
|
|
: undefined;
|
2021-07-04 07:09:39 -04:00
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
const openProfile = () =>
|
2021-07-30 15:24:53 -04:00
|
|
|
openScreen({ id: "profile", user_id: message.author_id });
|
2021-06-24 11:43:37 -04:00
|
|
|
|
2021-07-31 05:31:16 -04:00
|
|
|
// ! FIXME(?): animate on hover
|
2021-07-29 10:51:19 -04:00
|
|
|
const [animate, setAnimate] = useState(false);
|
2021-07-14 08:25:40 -04:00
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
return (
|
|
|
|
<div id={message._id}>
|
2021-07-30 15:24:53 -04:00
|
|
|
{message.reply_ids?.map((message_id, index) => (
|
2021-07-29 10:51:19 -04:00
|
|
|
<MessageReply
|
|
|
|
index={index}
|
|
|
|
id={message_id}
|
2021-07-30 15:24:53 -04:00
|
|
|
channel={message.channel!}
|
2021-07-29 10:51:19 -04:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
<MessageBase
|
|
|
|
highlight={highlight}
|
|
|
|
head={
|
2021-07-30 15:24:53 -04:00
|
|
|
(head &&
|
|
|
|
!(
|
|
|
|
message.reply_ids &&
|
|
|
|
message.reply_ids.length > 0
|
|
|
|
)) ??
|
|
|
|
false
|
2021-07-29 10:51:19 -04:00
|
|
|
}
|
|
|
|
contrast={contrast}
|
|
|
|
sending={typeof queued !== "undefined"}
|
2021-07-30 15:24:53 -04:00
|
|
|
mention={message.mention_ids?.includes(client.user!._id)}
|
2021-07-29 10:51:19 -04:00
|
|
|
failed={typeof queued?.error !== "undefined"}
|
|
|
|
onContextMenu={
|
|
|
|
attachContext
|
|
|
|
? attachContextMenu("Menu", {
|
|
|
|
message,
|
2021-07-31 05:31:16 -04:00
|
|
|
contextualChannel: message.channel_id,
|
2021-07-29 10:51:19 -04:00
|
|
|
queued,
|
|
|
|
})
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
onMouseEnter={() => setAnimate(true)}
|
|
|
|
onMouseLeave={() => setAnimate(false)}>
|
|
|
|
<MessageInfo>
|
|
|
|
{head ? (
|
|
|
|
<UserIcon
|
|
|
|
target={user}
|
|
|
|
size={36}
|
2021-07-05 06:25:20 -04:00
|
|
|
onContextMenu={userContext}
|
|
|
|
onClick={openProfile}
|
2021-07-29 10:51:19 -04:00
|
|
|
animate={animate}
|
2021-07-05 06:25:20 -04:00
|
|
|
/>
|
2021-07-29 10:51:19 -04:00
|
|
|
) : (
|
|
|
|
<MessageDetail message={message} position="left" />
|
|
|
|
)}
|
|
|
|
</MessageInfo>
|
|
|
|
<MessageContent>
|
|
|
|
{head && (
|
|
|
|
<span className="detail">
|
|
|
|
<Username
|
|
|
|
className="author"
|
|
|
|
user={user}
|
|
|
|
onContextMenu={userContext}
|
|
|
|
onClick={openProfile}
|
|
|
|
/>
|
|
|
|
<MessageDetail
|
|
|
|
message={message}
|
|
|
|
position="top"
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
{replacement ?? <Markdown content={content} />}
|
|
|
|
{queued?.error && (
|
|
|
|
<Overline type="error" error={queued.error} />
|
|
|
|
)}
|
|
|
|
{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} />
|
|
|
|
))}
|
|
|
|
</MessageContent>
|
|
|
|
</MessageBase>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2021-06-23 11:14:46 -04:00
|
|
|
|
|
|
|
export default memo(Message);
|