revite/src/components/common/messaging/attachments/MessageReply.tsx

204 lines
5.7 KiB
TypeScript
Raw Normal View History

import { Reply } from "@styled-icons/boxicons-regular";
import { File } from "@styled-icons/boxicons-solid";
import { observer } from "mobx-react-lite";
import { useHistory } from "react-router-dom";
import { RelationshipStatus } from "revolt-api/types/Users";
2021-07-06 14:29:27 -04:00
import { SYSTEM_USER_ID } from "revolt.js";
import { Channel } from "revolt.js/dist/maps/Channels";
import { Message } from "revolt.js/dist/maps/Messages";
2021-07-05 06:23:23 -04:00
import styled, { css } from "styled-components";
import { Text } from "preact-i18n";
import { useLayoutEffect, useState } from "preact/hooks";
2021-07-05 06:23:23 -04:00
2021-06-23 13:26:41 -04:00
import { useRenderState } from "../../../../lib/renderer/Singleton";
import { useClient } from "../../../../context/revoltjs/RevoltClient";
2021-07-05 06:23:23 -04:00
import Markdown from "../../../markdown/Markdown";
import UserShort from "../../user/UserShort";
import { SystemMessage } from "../SystemMessage";
2021-07-05 06:23:23 -04:00
2021-06-23 13:26:41 -04:00
interface Props {
channel: Channel;
2021-07-05 06:25:20 -04:00
index: number;
id: string;
2021-06-23 13:26:41 -04:00
}
2021-07-05 06:23:23 -04:00
export const ReplyBase = styled.div<{
2021-07-05 06:25:20 -04:00
head?: boolean;
fail?: boolean;
preview?: boolean;
2021-07-05 06:23:23 -04:00
}>`
2021-07-05 06:25:20 -04:00
gap: 4px;
min-width: 0;
2021-07-05 06:25:20 -04:00
display: flex;
2021-07-09 15:46:35 -04:00
margin-inline-start: 30px;
margin-inline-end: 12px;
margin-bottom: 4px;
2021-07-05 06:25:20 -04:00
font-size: 0.8em;
user-select: none;
align-items: center;
color: var(--secondary-foreground);
2021-06-23 13:26:41 -04:00
* {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
2021-07-09 15:46:35 -04:00
.user {
gap: 4px;
display: flex;
2021-07-09 15:46:35 -04:00
flex-shrink: 0;
font-weight: 600;
overflow: visible;
2021-07-09 15:46:35 -04:00
align-items: center;
2021-07-09 15:56:09 -04:00
span {
cursor: pointer;
&:hover {
text-decoration: underline;
}
}
2021-07-09 15:46:35 -04:00
/*&::before {
position:relative;
width: 50px;
height: 2px;
background: red;
}*/
}
.content {
gap: 4px;
display: flex;
cursor: pointer;
align-items: center;
flex-direction: row;
2021-07-09 15:56:09 -04:00
transition: filter 1s ease-in-out;
transition: transform ease-in-out 0.1s;
2021-07-09 15:56:09 -04:00
filter: brightness(1);
&:hover {
filter: brightness(2);
}
&:active {
transform: translateY(1px);
}
> * {
pointer-events: none;
}
> span {
display: flex;
}
}
> svg:first-child {
2021-07-05 06:25:20 -04:00
flex-shrink: 0;
transform: scaleX(-1);
color: var(--tertiary-foreground);
}
2021-06-23 13:26:41 -04:00
2021-07-05 06:25:20 -04:00
${(props) =>
props.fail &&
css`
color: var(--tertiary-foreground);
`}
2021-06-23 13:26:41 -04:00
2021-07-05 06:25:20 -04:00
${(props) =>
props.head &&
css`
margin-top: 12px;
`}
2021-06-23 13:26:41 -04:00
2021-07-05 06:23:23 -04:00
${(props) =>
2021-07-05 06:25:20 -04:00
props.preview &&
css`
margin-left: 0;
`}
2021-06-23 13:26:41 -04:00
`;
export const MessageReply = observer(({ index, channel, id }: Props) => {
const view = useRenderState(channel._id);
2021-07-05 06:25:20 -04:00
if (view?.type !== "RENDER") return null;
2021-06-23 13:26:41 -04:00
const [message, setMessage] = useState<Message | undefined>(undefined);
useLayoutEffect(() => {
// ! FIXME: We should do this through the message renderer, so it can fetch it from cache if applicable.
const m = view.messages.find((x) => x._id === id);
if (m) {
setMessage(m);
} else {
channel.fetchMessage(id).then(setMessage);
}
}, [view.messages]);
2021-07-05 06:25:20 -04:00
if (!message) {
return (
<ReplyBase head={index === 0} fail>
<Reply size={16} />
<span>
<Text id="app.main.channel.misc.failed_load" />
</span>
</ReplyBase>
);
}
2021-06-23 13:26:41 -04:00
const history = useHistory();
2021-06-23 13:26:41 -04:00
2021-07-05 06:25:20 -04:00
return (
<ReplyBase head={index === 0}>
<Reply size={16} />
{message.author?.relationship === RelationshipStatus.Blocked ? (
<>
<Text id="app.main.channel.misc.blocked_user" />
</>
) : (
<>
{message.author_id === SYSTEM_USER_ID ? (
<SystemMessage message={message} hideInfo />
) : (
<>
<div className="user">
<UserShort user={message.author} size={16} />
</div>
<div
className="content"
onClick={() => {
if (
channel.channel_type === "TextChannel"
) {
history.push(
`/server/${channel.server}/channel/${channel._id}/${message._id}`,
);
} else {
history.push(
`/channel/${channel}/${message._id}`,
);
}
}}>
{message.attachments &&
message.attachments.length > 0 && (
<File size={16} />
)}
<Markdown
disallowBigEmoji
content={(
message.content as string
).replace(/\n/g, " ")}
/>
</div>
</>
)}
</>
)}
2021-07-05 06:25:20 -04:00
</ReplyBase>
);
});