diff --git a/web/components/widgets/avatar.tsx b/web/components/widgets/avatar.tsx index 3e25a759..06ec18ec 100644 --- a/web/components/widgets/avatar.tsx +++ b/web/components/widgets/avatar.tsx @@ -1,37 +1,37 @@ import Router from 'next/router' import clsx from 'clsx' -import { memo, MouseEvent, useEffect, useState } from 'react' -import { UserCircleIcon, UserIcon, UsersIcon } from '@heroicons/react/solid' +import {memo, MouseEvent, useEffect, useState} from 'react' +import {UserIcon, UsersIcon} from '@heroicons/react/solid' import Image from 'next/image' -import { floor } from 'lodash' +import {floor} from 'lodash' export type AvatarSizeType = '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' export const Avatar = memo( (props: { username?: string - avatarUrl?: string + avatarUrl?: string | null noLink?: boolean size?: AvatarSizeType className?: string preventDefault?: boolean }) => { - const { username, noLink, size, className, preventDefault } = props + const {username, noLink, size, className, preventDefault} = props const [avatarUrl, setAvatarUrl] = useState(props.avatarUrl) 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 + ? 6 + : size == 'sm' + ? 8 + : size == 'md' + ? 10 + : size == 'lg' + ? 12 + : size == 'xl' + ? 24 + : 10 const sizeInPx = s * 4 const onClick = (e: MouseEvent) => { @@ -44,9 +44,18 @@ export const Avatar = memo( } } + 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(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 avatarUrl ? ( + // return url ? ( + return
{`${username { - // If the image doesn't load, clear the avatarUrl to show the default - // Mostly for localhost, when getting a 403 from googleusercontent - setAvatarUrl('') - }} + onError={() => setAvatarUrl('')} /> - ) : ( -
+ + // ) : ( + //