revite/src/components/common/ChannelIcon.tsx

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-06-27 06:17:59 -04:00
import { Hash, VolumeFull } from "@styled-icons/boxicons-regular";
2021-07-29 13:41:01 -04:00
import { observer } from "mobx-react-lite";
2021-07-30 17:40:49 -04:00
import { Channel } from "revolt.js/dist/maps/Channels";
2021-07-05 06:23:23 -04:00
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";
2021-07-29 13:41:01 -04:00
interface Props extends IconBaseProps<Channel> {
2021-07-05 06:25:20 -04:00
isServerChannel?: boolean;
2021-06-19 07:34:53 -04:00
}
2021-07-29 13:41:01 -04:00
export default observer(
(
props: Props & Omit<JSX.HTMLAttributes<HTMLImageElement>, keyof Props>,
) => {
const client = useContext(AppContext);
2021-07-05 06:23:23 -04:00
2021-07-29 13:41:01 -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-29 13:41:01 -04:00
if (typeof iconURL === "undefined") {
if (isServerChannel) {
if (target?.channel_type === "VoiceChannel") {
return <VolumeFull size={size} />;
}
return <Hash size={size} />;
2021-07-05 06:25:20 -04:00
}
}
2021-07-05 06:23:23 -04:00
2021-07-29 13:41:01 -04:00
return (
// ! TODO: replace fallback with <picture /> + <source />
2021-07-29 13:41:01 -04:00
<ImageIconBase
{...imgProps}
width={size}
height={size}
loading="lazy"
aria-hidden="true"
square={isServerChannel}
src={iconURL ?? fallback}
/>
);
},
);