2021-07-05 06:23:23 -04:00
|
|
|
import { Server } from "revolt.js/dist/api/objects";
|
2021-06-19 07:34:53 -04:00
|
|
|
import styled from "styled-components";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-19 07:34:53 -04:00
|
|
|
import { useContext } from "preact/hooks";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-19 07:34:53 -04:00
|
|
|
import { AppContext } from "../../context/revoltjs/RevoltClient";
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
import { IconBaseProps, ImageIconBase } from "./IconBase";
|
|
|
|
|
2021-06-19 07:34:53 -04:00
|
|
|
interface Props extends IconBaseProps<Server> {
|
2021-07-05 06:25:20 -04:00
|
|
|
server_name?: string;
|
2021-06-19 07:34:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const ServerText = styled.div`
|
2021-07-05 06:25:20 -04:00
|
|
|
display: grid;
|
|
|
|
padding: 0.2em;
|
|
|
|
overflow: hidden;
|
|
|
|
border-radius: 50%;
|
|
|
|
place-items: center;
|
|
|
|
color: var(--foreground);
|
|
|
|
background: var(--primary-background);
|
2021-06-19 07:34:53 -04:00
|
|
|
`;
|
|
|
|
|
2021-07-05 06:23:23 -04:00
|
|
|
const fallback = "/assets/group.png";
|
|
|
|
export default function ServerIcon(
|
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 {
|
|
|
|
target,
|
|
|
|
attachment,
|
|
|
|
size,
|
|
|
|
animate,
|
|
|
|
server_name,
|
|
|
|
children,
|
|
|
|
as,
|
|
|
|
...imgProps
|
|
|
|
} = props;
|
|
|
|
const iconURL = client.generateFileURL(
|
|
|
|
target?.icon ?? attachment,
|
|
|
|
{ max_side: 256 },
|
|
|
|
animate,
|
|
|
|
);
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
if (typeof iconURL === "undefined") {
|
|
|
|
const name = target?.name ?? server_name ?? "";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<ServerText style={{ width: size, height: size }}>
|
|
|
|
{name
|
|
|
|
.split(" ")
|
|
|
|
.map((x) => x[0])
|
|
|
|
.filter((x) => typeof x !== "undefined")}
|
|
|
|
</ServerText>
|
|
|
|
);
|
|
|
|
}
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<ImageIconBase
|
|
|
|
{...imgProps}
|
|
|
|
width={size}
|
|
|
|
height={size}
|
|
|
|
aria-hidden="true"
|
|
|
|
src={iconURL}
|
|
|
|
/>
|
|
|
|
);
|
2021-06-19 07:34:53 -04:00
|
|
|
}
|