revite/src/components/common/ChannelIcon.tsx

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-06-19 07:34:53 -04:00
import { useContext } from "preact/hooks";
import { Hash } from "@styled-icons/feather";
import { Channels } from "revolt.js/dist/api/objects";
2021-06-19 10:29:04 -04:00
import { ImageIconBase, IconBaseProps } from "./IconBase";
2021-06-19 07:34:53 -04:00
import { AppContext } from "../../context/revoltjs/RevoltClient";
interface Props extends IconBaseProps<Channels.GroupChannel | Channels.TextChannel> {
isServerChannel?: boolean;
}
const fallback = '/assets/group.png';
2021-06-19 10:29:04 -04:00
export default function ChannelIcon(props: Props & Omit<JSX.HTMLAttributes<HTMLImageElement>, keyof Props>) {
2021-06-19 07:34:53 -04:00
const { client } = useContext(AppContext);
2021-06-19 10:29:04 -04:00
const { size, target, attachment, isServerChannel: server, animate, children, as, ...imgProps } = props;
2021-06-19 07:34:53 -04:00
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 (
2021-06-19 10:29:04 -04:00
// ! fixme: replace fallback with <picture /> + <source />
<ImageIconBase {...imgProps}
2021-06-19 07:34:53 -04:00
width={size}
height={size}
aria-hidden="true"
2021-06-19 10:29:04 -04:00
square={isServerChannel}
src={iconURL ?? fallback}
onError={ e => {
let el = e.currentTarget;
if (el.src !== fallback) {
el.src = fallback
}
}} />
2021-06-19 07:34:53 -04:00
);
}