Skip to content
Snippets Groups Projects
ChannelIcon.tsx 1.87 KiB
Newer Older
import { Hash, VolumeFull } from "@styled-icons/boxicons-regular";
insert's avatar
insert committed
import { observer } from "mobx-react-lite";
insert's avatar
insert committed
import { Channel } from "revolt.js/dist/maps/Channels";
insert's avatar
insert committed

import { useContext } from "preact/hooks";

insert's avatar
insert committed
import { AppContext } from "../../context/revoltjs/RevoltClient";

insert's avatar
insert committed
import { ImageIconBase, IconBaseProps } from "./IconBase";
import fallback from "./assets/group.png";

insert's avatar
insert committed
interface Props extends IconBaseProps<Channel> {
    isServerChannel?: boolean;
insert's avatar
insert committed
export default observer(
    (
insert's avatar
insert committed
        props: Props &
            Omit<
                JSX.HTMLAttributes<HTMLImageElement>,
                keyof Props | "children" | "as"
            >,
insert's avatar
insert committed
    ) => {
        const client = useContext(AppContext);
insert's avatar
insert committed
        const {
            size,
            target,
            attachment,
            isServerChannel: server,
            animate,
            ...imgProps
        } = props;
        const iconURL = client.generateFileURL(
            target?.icon ?? attachment,
            { max_side: 256 },
            animate,
        );
        const isServerChannel =
            server ||
            (target &&
                (target.channel_type === "TextChannel" ||
                    target.channel_type === "VoiceChannel"));
insert's avatar
insert committed
        if (typeof iconURL === "undefined") {
            if (isServerChannel) {
                if (target?.channel_type === "VoiceChannel") {
                    return <VolumeFull size={size} />;
                }
                return <Hash size={size} />;
insert's avatar
insert committed
        return (
insert's avatar
insert committed
            // ! TODO: replace fallback with <picture /> + <source />
insert's avatar
insert committed
            <ImageIconBase
                {...imgProps}
                width={size}
                height={size}
                loading="lazy"
                aria-hidden="true"
                square={isServerChannel}
                src={iconURL ?? fallback}
            />
        );
    },
);