Skip to content
Snippets Groups Projects
ServerIcon.tsx 1.79 KiB
Newer Older
import { observer } from "mobx-react-lite";
insert's avatar
insert committed
import { Server } from "revolt.js/dist/maps/Servers";
insert's avatar
insert committed
import styled from "styled-components";
insert's avatar
insert committed

insert's avatar
insert committed
import { useContext } from "preact/hooks";
insert's avatar
insert committed

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

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

insert's avatar
insert committed
interface Props extends IconBaseProps<Server> {
    server_name?: string;
insert's avatar
insert committed
}

const ServerText = styled.div`
    display: grid;
    padding: 0.2em;
    overflow: hidden;
    border-radius: 50%;
    place-items: center;
    color: var(--foreground);
    background: var(--primary-background);
insert's avatar
insert committed
// const fallback = "/assets/group.png";
export default observer(
    (
insert's avatar
insert committed
        props: Props &
            Omit<
                JSX.HTMLAttributes<HTMLImageElement>,
                keyof Props | "children" | "as"
            >,
    ) => {
        const client = useContext(AppContext);

insert's avatar
insert committed
        const { target, attachment, size, animate, server_name, ...imgProps } =
            props;
        const iconURL = client.generateFileURL(
            target?.icon ?? attachment,
            { max_side: 256 },
            animate,
        );
        if (typeof iconURL === "undefined") {
            const name = target?.name ?? server_name ?? "";
            return (
                <ServerText style={{ width: size, height: size }}>
                    {name
                        .split(" ")
                        .map((x) => x[0])
                        .filter((x) => typeof x !== "undefined")}
                </ServerText>
            );
        }
        return (
            <ImageIconBase
                {...imgProps}
                width={size}
                height={size}
                src={iconURL}
                loading="lazy"
                aria-hidden="true"
            />