import Router from 'next/router' import clsx from 'clsx' import { MouseEvent, useEffect, useState } from 'react' import { UserCircleIcon, UserIcon, UsersIcon } from '@heroicons/react/solid' import Image from 'next/future/image' export function Avatar(props: { username?: string avatarUrl?: string noLink?: boolean size?: number | 'xxs' | 'xs' | 'sm' className?: string }) { const { username, noLink, size, className } = props const [avatarUrl, setAvatarUrl] = useState(props.avatarUrl) useEffect(() => setAvatarUrl(props.avatarUrl), [props.avatarUrl]) const s = size == 'xxs' ? 4 : size == 'xs' ? 6 : size === 'sm' ? 8 : size || 10 const sizeInPx = s * 4 const onClick = noLink && username ? undefined : (e: MouseEvent) => { e.stopPropagation() Router.push(`/${username}`) } // 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 avatarUrl ? ( {`${username { // If the image doesn't load, clear the avatarUrl to show the default // Mostly for localhost, when getting a 403 from googleusercontent setAvatarUrl('') }} /> ) : (