Skip to content
Snippets Groups Projects
ChannelIcon.tsx 1.47 KiB
Newer Older
insert's avatar
insert committed
import { useContext } from "preact/hooks";
import { Hash } from "@styled-icons/feather";
import { Channels } from "revolt.js/dist/api/objects";
insert's avatar
insert committed
import { ImageIconBase, IconBaseProps } from "./IconBase";
insert's avatar
insert committed
import { AppContext } from "../../context/revoltjs/RevoltClient";

interface Props extends IconBaseProps<Channels.GroupChannel | Channels.TextChannel> {
    isServerChannel?: boolean;
}

import fallback from './assets/group.png';

insert's avatar
insert committed
export default function ChannelIcon(props: Props & Omit<JSX.HTMLAttributes<HTMLImageElement>, keyof Props>) {
insert's avatar
insert committed
    const client = useContext(AppContext);
insert's avatar
insert committed
    const { size, target, attachment, isServerChannel: server, animate, children, as, ...imgProps } = props;
insert's avatar
insert committed
    const iconURL = client.generateFileURL(target?.icon ?? attachment, { max_side: 256 }, animate);
    const isServerChannel = server || target?.channel_type === 'TextChannel';

    if (typeof iconURL === 'undefined') {
        if (isServerChannel) {
            return (
                <Hash size={size} />
            )
        }
    }

    return (
insert's avatar
insert committed
        // ! fixme: replace fallback with <picture /> + <source />
        <ImageIconBase {...imgProps}
insert's avatar
insert committed
            width={size}
            height={size}
            aria-hidden="true"
insert's avatar
insert committed
            square={isServerChannel}
            src={iconURL ?? fallback}
            onError={ e => {
                let el = e.currentTarget;
                if (el.src !== fallback) {
                    el.src = fallback
                }
            }} />
insert's avatar
insert committed
    );
}