revite/src/components/common/ChannelIcon.tsx

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-06-27 06:17:59 -04:00
import { Hash, VolumeFull } from "@styled-icons/boxicons-regular";
2021-07-05 06:23:23 -04:00
import { Channels } from "revolt.js/dist/api/objects";
import { useContext } from "preact/hooks";
2021-06-19 07:34:53 -04:00
import { AppContext } from "../../context/revoltjs/RevoltClient";
2021-07-05 06:23:23 -04:00
import { ImageIconBase, IconBaseProps } from "./IconBase";
import fallback from "./assets/group.png";
interface Props
2021-07-05 06:25:20 -04:00
extends IconBaseProps<
Channels.GroupChannel | Channels.TextChannel | Channels.VoiceChannel
> {
isServerChannel?: boolean;
2021-06-19 07:34:53 -04:00
}
2021-07-05 06:23:23 -04:00
export default function ChannelIcon(
2021-07-05 06:25:20 -04:00
props: Props & Omit<JSX.HTMLAttributes<HTMLImageElement>, keyof Props>,
2021-07-05 06:23:23 -04:00
) {
2021-07-05 06:25:20 -04:00
const client = useContext(AppContext);
2021-07-05 06:23:23 -04:00
2021-07-05 06:25:20 -04:00
const {
size,
target,
attachment,
isServerChannel: server,
animate,
children,
as,
...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"));
2021-07-05 06:23:23 -04:00
2021-07-05 06:25:20 -04:00
if (typeof iconURL === "undefined") {
if (isServerChannel) {
if (target?.channel_type === "VoiceChannel") {
return <VolumeFull size={size} />;
} else {
return <Hash size={size} />;
}
}
}
2021-07-05 06:23:23 -04:00
2021-07-05 06:25:20 -04:00
return (
// ! fixme: replace fallback with <picture /> + <source />
<ImageIconBase
{...imgProps}
width={size}
height={size}
2021-07-10 10:21:35 -04:00
loading="lazy"
2021-07-05 06:25:20 -04:00
aria-hidden="true"
square={isServerChannel}
src={iconURL ?? fallback}
/>
);
2021-06-19 07:34:53 -04:00
}