Skip to content
Snippets Groups Projects
MessageReply.tsx 6.6 KiB
Newer Older
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";
insert's avatar
insert committed
import { SYSTEM_USER_ID } from "revolt.js";
import { Channel } from "revolt.js/dist/maps/Channels";
import { Message } from "revolt.js/dist/maps/Messages";
insert's avatar
insert committed
import styled, { css } from "styled-components";

import { Text } from "preact-i18n";
insert's avatar
insert committed
import { useLayoutEffect, useState } from "preact/hooks";
insert's avatar
insert committed
import { useRenderState } from "../../../../lib/renderer/Singleton";

insert's avatar
insert committed
import Markdown from "../../../markdown/Markdown";
import UserShort from "../../user/UserShort";
import { SystemMessage } from "../SystemMessage";
insert's avatar
insert committed
interface Props {
    channel: Channel;
    index: number;
    id: string;
insert's avatar
insert committed
}

insert's avatar
insert committed
export const ReplyBase = styled.div<{
    head?: boolean;
    fail?: boolean;
    preview?: boolean;
insert's avatar
insert committed
}>`
    gap: 4px;
insert's avatar
insert committed
    min-width: 0;
    display: flex;
nizune's avatar
nizune committed
    margin-inline-start: 30px;
    margin-inline-end: 12px;
nizune's avatar
nizune committed
    /*margin-bottom: 4px;*/
    font-size: 0.8em;
    user-select: none;
    align-items: center;
    color: var(--secondary-foreground);
insert's avatar
insert committed

nizune's avatar
nizune committed
    &::before {
        content: "";
        height: 10px;
        width: 28px;
        margin-inline-end: 2px;
        align-self: flex-end;
        display: flex;
        border-top: 2.2px solid var(--tertiary-foreground);
        border-inline-start: 2.2px solid var(--tertiary-foreground);
        border-start-start-radius: 6px;
    }

    * {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

nizune's avatar
nizune committed
    .user {
insert's avatar
insert committed
        display: flex;
nizune's avatar
nizune committed
        flex-shrink: 0;
        font-weight: 600;
insert's avatar
insert committed
        overflow: visible;
nizune's avatar
nizune committed
        align-items: center;
nizune's avatar
nizune committed
        padding: 2px 0;
nizune's avatar
nizune committed

nizune's avatar
nizune committed
        span {
            cursor: pointer;
            &:hover {
                text-decoration: underline;
            }
        }

nizune's avatar
nizune committed
        /*&::before {
            position:relative;
            width: 50px;
            height: 2px;
            background: red;
        }*/
    }

nizune's avatar
nizune committed
        padding: 2px 0;
        gap: 4px;
        display: flex;
        align-items: center;
        flex-direction: row;
nizune's avatar
nizune committed
        transition: filter 1s ease-in-out;
        transition: transform ease-in-out 0.1s;
nizune's avatar
nizune committed
        filter: brightness(1);

        &:hover {
            filter: brightness(2);
        }

        &:active {
            transform: translateY(1px);
        }
            display: flex;
    > svg:first-child {
        flex-shrink: 0;
        transform: scaleX(-1);
        color: var(--tertiary-foreground);
    }
insert's avatar
insert committed

    ${(props) =>
        props.fail &&
        css`
            color: var(--tertiary-foreground);
        `}
insert's avatar
insert committed

    ${(props) =>
        props.head &&
        css`
            margin-top: 12px;
        `}
insert's avatar
insert committed

insert's avatar
insert committed
    ${(props) =>
        props.preview &&
        css`
            margin-left: 0;
        `}
insert's avatar
insert committed
`;

export const MessageReply = observer(({ index, channel, id }: Props) => {
    const view = useRenderState(channel._id);
    if (view?.type !== "RENDER") return null;
insert's avatar
insert committed

    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);
insert's avatar
insert committed
    }, [id, channel, view.messages]);
    if (!message) {
        return (
            <ReplyBase head={index === 0} fail>
                <span>
                    <Text id="app.main.channel.misc.failed_load" />
                </span>
            </ReplyBase>
        );
    }
insert's avatar
insert committed

    const history = useHistory();
insert's avatar
insert committed

    return (
        <ReplyBase head={index === 0}>
            {message.author?.relationship === RelationshipStatus.Blocked ? (
nizune's avatar
nizune committed
                <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={() => {
                                    const channel = message.channel!;
                                    if (
                                        channel.channel_type === "TextChannel"
                                    ) {
                                        console.log(
                                            `/server/${channel.server_id}/channel/${channel._id}/${message._id}`,
                                        );
                                        history.push(
insert's avatar
insert committed
                                            `/server/${channel.server_id}/channel/${channel._id}/${message._id}`,
                                        );
                                    } else {
                                        history.push(
                                            `/channel/${channel._id}/${message._id}`,
nizune's avatar
nizune committed
                                {message.attachments && (
                                    <>
                                        <File size={16} />
insert's avatar
insert committed
                                        <em>
                                            {message.attachments.length > 1 ? (
                                                <Text id="app.main.channel.misc.sent_multiple_files" />
                                            ) : (
                                                <Text id="app.main.channel.misc.sent_file" />
                                            )}
nizune's avatar
nizune committed
                                        </em>
                                    </>
                                )}
                                <Markdown
                                    disallowBigEmoji
                                    content={(
                                        message.content as string
                                    ).replace(/\n/g, " ")}
                                />
                            </div>
                        </>
                    )}
        </ReplyBase>
    );
insert's avatar
insert committed
});