import { useState, useCallback } from 'react'; import classNames from 'classnames'; import { Link } from 'react-router-dom'; import { useHovering } from 'mastodon/hooks/useHovering'; import { autoPlayGif } from 'mastodon/initial_state'; import type { Account } from 'mastodon/models/account'; import { useAccount } from '../hooks/useAccount'; interface Props { account: | Pick | undefined; // FIXME: remove `undefined` once we know for sure its always there alt?: string; size?: number; style?: React.CSSProperties; inline?: boolean; animate?: boolean; withLink?: boolean; counter?: number | string; counterBorderColor?: string; className?: string; } export const Avatar: React.FC = ({ account, alt = '', animate = autoPlayGif, size = 20, inline = false, withLink = false, style: styleFromParent, className, counter, counterBorderColor, }) => { const { hovering, handleMouseEnter, handleMouseLeave } = useHovering(animate); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const style = { ...styleFromParent, width: `${size}px`, height: `${size}px`, }; const src = hovering || animate ? account?.avatar : account?.avatar_static; const handleLoad = useCallback(() => { setLoading(false); }, [setLoading]); const handleError = useCallback(() => { setError(true); }, [setError]); const avatar = ( {src && !error && ( {alt} )} {counter && ( {counter} )} ); if (withLink) { return ( {avatar} ); } return avatar; }; export const AvatarById: React.FC< { accountId: string | undefined } & Omit > = ({ accountId, ...otherProps }) => { const account = useAccount(accountId); return ; };