revite/src/components/common/user/UserIcon.tsx

138 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-06-27 06:17:59 -04:00
import { MicrophoneOff } from "@styled-icons/boxicons-regular";
2021-07-29 10:11:21 -04:00
import { observer } from "mobx-react-lite";
import { useParams } from "react-router-dom";
2021-07-30 17:40:49 -04:00
import { Presence } from "revolt-api/types/Users";
import { User } from "revolt.js/dist/maps/Users";
2021-07-05 06:23:23 -04:00
import styled, { css } from "styled-components";
import { useContext } from "preact/hooks";
2021-06-20 15:30:42 -04:00
import { ThemeContext } from "../../../context/Theme";
import { AppContext, useClient } from "../../../context/revoltjs/RevoltClient";
2021-06-19 07:34:53 -04:00
2021-07-05 06:23:23 -04:00
import IconBase, { IconBaseProps } from "../IconBase";
import fallback from "../assets/user.png";
2021-06-19 07:34:53 -04:00
type VoiceStatus = "muted";
interface Props extends IconBaseProps<User> {
2021-07-05 06:25:20 -04:00
mask?: string;
status?: boolean;
voice?: VoiceStatus;
showServerIdentity?: boolean;
2021-06-19 07:34:53 -04:00
}
export function useStatusColour(user?: User) {
2021-07-05 06:25:20 -04:00
const theme = useContext(ThemeContext);
2021-06-19 07:34:53 -04:00
2021-07-30 17:40:49 -04:00
return user?.online && user?.status?.presence !== Presence.Invisible
? user?.status?.presence === Presence.Idle
2021-07-05 06:25:20 -04:00
? theme["status-away"]
2021-07-30 17:40:49 -04:00
: user?.status?.presence === Presence.Busy
2021-07-05 06:25:20 -04:00
? theme["status-busy"]
: theme["status-online"]
: theme["status-invisible"];
2021-06-19 07:34:53 -04:00
}
const VoiceIndicator = styled.div<{ status: VoiceStatus }>`
2021-07-05 06:25:20 -04:00
width: 10px;
height: 10px;
border-radius: 50%;
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
display: flex;
align-items: center;
justify-content: center;
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
svg {
stroke: white;
}
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
${(props) =>
props.status === "muted" &&
css`
background: var(--error);
`}
2021-06-19 07:34:53 -04:00
`;
2021-07-29 10:11:21 -04:00
export default observer(
2021-08-05 09:47:00 -04:00
(
props: Props &
Omit<
JSX.SVGAttributes<SVGSVGElement>,
keyof Props | "children" | "as"
>,
) => {
const client = useClient();
2021-06-19 07:34:53 -04:00
2021-07-29 10:11:21 -04:00
const {
target,
attachment,
size,
status,
2021-07-05 06:25:20 -04:00
animate,
2021-07-29 10:11:21 -04:00
mask,
hover,
showServerIdentity,
2021-07-29 10:11:21 -04:00
...svgProps
} = props;
let override;
if (target && showServerIdentity) {
const { server } = useParams<{ server?: string }>();
if (server) {
const member = client.members.getKey({
server,
user: target._id,
});
if (member?.avatar) {
override = member?.avatar;
}
}
}
2021-07-29 10:11:21 -04:00
const iconURL =
client.generateFileURL(
override ?? target?.avatar ?? attachment,
2021-07-29 10:11:21 -04:00
{ max_side: 256 },
animate,
2021-07-30 17:40:49 -04:00
) ?? (target ? target.defaultAvatarURL : fallback);
2021-06-19 07:34:53 -04:00
2021-07-29 10:11:21 -04:00
return (
<IconBase
{...svgProps}
width={size}
height={size}
hover={hover}
2021-07-29 10:11:21 -04:00
aria-hidden="true"
viewBox="0 0 32 32">
<foreignObject
x="0"
y="0"
width="32"
height="32"
class="icon"
2021-07-29 10:11:21 -04:00
mask={mask ?? (status ? "url(#user)" : undefined)}>
{<img src={iconURL} draggable={false} loading="lazy" />}
2021-07-05 06:25:20 -04:00
</foreignObject>
2021-07-29 10:11:21 -04:00
{props.status && (
<circle
cx="27"
cy="27"
r="5"
fill={useStatusColour(target)}
/>
)}
{props.voice && (
<foreignObject x="22" y="22" width="10" height="10">
<VoiceIndicator status={props.voice}>
{props.voice === "muted" && (
<MicrophoneOff size={6} />
)}
</VoiceIndicator>
</foreignObject>
)}
</IconBase>
);
},
);