revite/src/components/common/messaging/Message.tsx

185 lines
6.7 KiB
TypeScript
Raw Normal View History

import { observer } from "mobx-react-lite";
import { Message as MessageObject } from "revolt.js/dist/maps/Messages";
import { attachContextMenu } from "preact-context-menu";
2021-07-05 06:23:23 -04:00
import { memo } from "preact/compat";
import { useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { internalEmit } from "../../../lib/eventEmitter";
import { QueuedMessage } from "../../../redux/reducers/queue";
2021-07-05 06:23:23 -04:00
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { useClient } from "../../../context/revoltjs/RevoltClient";
2021-07-05 06:23:23 -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";
import InviteList from "./embed/EmbedInvite";
2021-08-31 16:30:02 -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-08-09 12:34:25 -04:00
hideReply?: boolean;
}
const Message = observer(
({
highlight,
attachContext,
message,
contrast,
content: replacement,
head: preferHead,
queued,
2021-08-09 12:34:25 -04:00
hideReply,
}: Props) => {
const client = useClient();
const user = message.author;
const { openScreen } = useIntermediate();
const content = message.content as string;
const head =
preferHead || (message.reply_ids && message.reply_ids.length > 0);
// ! TODO: tell fatal to make this type generic
// bree: Fatal please...
const userContext = attachContext
? (attachContextMenu("Menu", {
user: message.author_id,
contextualChannel: message.channel_id,
// eslint-disable-next-line
}) as any)
: undefined;
const openProfile = () =>
openScreen({ id: "profile", user_id: message.author_id });
const handleUserClick = (e: MouseEvent) => {
if (e.shiftKey && user?._id) {
internalEmit(
"MessageBox",
"append",
`<@${user._id}>`,
"mention",
);
} else {
openProfile();
}
};
// ! FIXME(?): animate on hover
const [animate, setAnimate] = useState(false);
return (
<div id={message._id}>
2021-08-09 12:34:25 -04:00
{!hideReply &&
message.reply_ids?.map((message_id, index) => (
<MessageReply
key={message_id}
index={index}
id={message_id}
channel={message.channel!}
parent_mentions={message.mention_ids ?? []}
2021-08-09 12:34:25 -04:00
/>
))}
<MessageBase
highlight={highlight}
head={
2021-08-09 12:34:25 -04:00
hideReply
? false
: (head &&
!(
message.reply_ids &&
message.reply_ids.length > 0
)) ??
false
}
contrast={contrast}
sending={typeof queued !== "undefined"}
mention={message.mention_ids?.includes(client.user!._id)}
failed={typeof queued?.error !== "undefined"}
onContextMenu={
attachContext
? attachContextMenu("Menu", {
message,
contextualChannel: message.channel_id,
queued,
})
: undefined
}
onMouseEnter={() => setAnimate(true)}
onMouseLeave={() => setAnimate(false)}>
<MessageInfo>
{head ? (
<UserIcon
url={message.generateMasqAvatarURL()}
target={user}
size={36}
2021-07-05 06:25:20 -04:00
onContextMenu={userContext}
onClick={handleUserClick}
animate={animate}
showServerIdentity
2021-07-05 06:25:20 -04:00
/>
) : (
<MessageDetail message={message} position="left" />
)}
</MessageInfo>
<MessageContent>
{head && (
<span className="detail">
<Username
override={message.masquerade?.name}
className="author"
user={user}
onContextMenu={userContext}
onClick={handleUserClick}
showServerIdentity
/>
<MessageDetail
message={message}
position="top"
/>
</span>
)}
{replacement ?? <Markdown content={content} />}
{!queued && <InviteList message={message} />}
{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>
);
},
);
export default memo(Message);