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

143 lines
5 KiB
TypeScript
Raw Normal View History

import { attachContextMenu } from "preact-context-menu";
2021-07-05 06:23:23 -04:00
import { memo } from "preact/compat";
import { useContext, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
import { QueuedMessage } from "../../../redux/reducers/queue";
2021-07-05 06:23:23 -04:00
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { AppContext } from "../../../context/revoltjs/RevoltClient";
import { useUser } from "../../../context/revoltjs/hooks";
import { MessageObject } from "../../../context/revoltjs/util";
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";
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-07-05 06:23:23 -04:00
function Message({
2021-07-09 04:58:38 -04:00
highlight,
2021-07-05 06:25:20 -04:00
attachContext,
message,
contrast,
content: replacement,
head: preferHead,
queued,
2021-07-05 06:23:23 -04:00
}: Props) {
2021-07-05 06:25:20 -04:00
// TODO: Can improve re-renders here by providing a list
// TODO: of dependencies. We only need to update on u/avatar.
const user = useUser(message.author);
const client = useContext(AppContext);
const { openScreen } = useIntermediate();
2021-07-05 06:25:20 -04:00
const content = message.content as string;
const head = preferHead || (message.replies && message.replies.length > 0);
2021-07-05 06:25:20 -04:00
// ! FIXME: tell fatal to make this type generic
// bree: Fatal please...
const userContext = attachContext
? (attachContextMenu("Menu", {
user: message.author,
contextualChannel: message.channel,
}) as any)
: undefined;
2021-07-05 06:25:20 -04:00
const openProfile = () =>
openScreen({ id: "profile", user_id: message.author });
// ! FIXME: animate on hover
const [animate, setAnimate] = useState(false);
2021-07-05 06:25:20 -04:00
return (
<div id={message._id}>
{message.replies?.map((message_id, index) => (
<MessageReply
index={index}
id={message_id}
channel={message.channel}
/>
))}
<MessageBase
2021-07-09 04:58:38 -04:00
highlight={highlight}
2021-07-05 06:25:20 -04:00
head={head && !(message.replies && message.replies.length > 0)}
contrast={contrast}
sending={typeof queued !== "undefined"}
mention={message.mentions?.includes(client.user!._id)}
failed={typeof queued?.error !== "undefined"}
onContextMenu={
attachContext
? attachContextMenu("Menu", {
message,
contextualChannel: message.channel,
queued,
})
: undefined
}
onMouseEnter={() => setAnimate(true)}
onMouseLeave={() => setAnimate(false)}>
2021-07-05 06:25:20 -04:00
<MessageInfo>
{head ? (
<UserIcon
target={user}
size={36}
onContextMenu={userContext}
onClick={openProfile}
animate={animate}
2021-07-05 06:25:20 -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>
);
}
export default memo(Message);