import {UserIcon, UsersIcon} from '@heroicons/react/24/solid' import clsx from 'clsx' import {floor} from 'lodash' import Image from 'next/image' import {useRouter} from 'next/router' import {memo, MouseEvent, useEffect, useState} from 'react' export type AvatarSizeType = '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' export const Avatar = memo( (props: { username?: string avatarUrl?: string | null noLink?: boolean size?: AvatarSizeType className?: string preventDefault?: boolean }) => { const {username, noLink, size, className, preventDefault} = props const [avatarUrl, setAvatarUrl] = useState(props.avatarUrl) const router = useRouter() useEffect(() => setAvatarUrl(props.avatarUrl), [props.avatarUrl]) const s = size == '2xs' ? 4 : size == 'xs' ? 6 : size == 'sm' ? 8 : size == 'md' ? 10 : size == 'lg' ? 12 : size == 'xl' ? 24 : 10 const sizeInPx = s * 4 const onClick = (e: MouseEvent) => { if (!noLink && username) { if (preventDefault) { e.preventDefault() } e.stopPropagation() router.push(`/${username}`) } } const fallbackInitial = (username || 'U')[0] // first character, not encoded string const url: string = avatarUrl && avatarUrl.length > 0 ? avatarUrl : `https://ui-avatars.com/api/?name=${encodeURIComponent(fallbackInitial)}` // console.log(username, fallbackInitial, url) // there can be no avatar URL or username in the feed, we show a "submit comment" // item with a fake grey user circle guy even if you aren't signed in // return url ? ( return (