Skip to content

Commit

Permalink
fix(ui): display default user avatar in chat (#2251)
Browse files Browse the repository at this point in the history
* fix(ui): display default user avatar in chat

* update

* [autofix.ci] apply automated fixes

* update

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
liangfung and autofix-ci[bot] authored May 27, 2024
1 parent 55ce2ee commit d32dfd6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
11 changes: 9 additions & 2 deletions ee/tabby-ui/components/chat/question-answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
AccordionTrigger
} from '../ui/accordion'
import { Button } from '../ui/button'
import { IconFile, IconRefresh, IconTrash } from '../ui/icons'
import { IconFile, IconRefresh, IconTrash, IconUser } from '../ui/icons'
import { Separator } from '../ui/separator'
import { Skeleton } from '../ui/skeleton'
import { UserAvatar } from '../user-avatar'
Expand Down Expand Up @@ -108,7 +108,14 @@ function UserMessageCard(props: { message: UserMessage }) {
{...props}
>
<div className="shrink-0 select-none rounded-full border bg-background shadow">
<UserAvatar className="h-8 w-8" />
<UserAvatar
className="h-8 w-8"
fallback={
<div className="flex h-8 w-8 items-center justify-center">
<IconUser className="h-5 w-5" />
</div>
}
/>
</div>
<div className="ml-4 flex-1 space-y-2 overflow-hidden px-1">
<MessageMarkdown message={message.message} />
Expand Down
30 changes: 23 additions & 7 deletions ee/tabby-ui/components/user-avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client'

import React from 'react'
import NiceAvatar, { genConfig } from 'react-nice-avatar'
import { mutate } from 'swr'
import useSWRImmutable from 'swr/immutable'
Expand All @@ -13,11 +14,18 @@ import {
AvatarImage
} from '@/components/ui/avatar'
import { Skeleton } from '@/components/ui/skeleton'
import { ReactNode } from 'react'

const NOT_FOUND_ERROR = 'not_found'
let shouldFetchAvatar = true

export function UserAvatar({ className }: { className?: string }) {
export function UserAvatar({
className,
fallback
}: {
className?: string
fallback?: string | ReactNode
}) {
const [{ data }] = useMe()
const userId = data?.me.id

Expand All @@ -41,7 +49,10 @@ export function UserAvatar({ className }: { className?: string }) {
})
})

if (!userId) return null
const avatarConfigFromEmail = React.useMemo(() => {
if (!data?.me?.email) return undefined
return genConfig(data.me.email)
}, [data?.me?.email])

if (isLoading) {
return <Skeleton className={cn('h-16 w-16 rounded-full', className)} />
Expand All @@ -51,19 +62,24 @@ export function UserAvatar({ className }: { className?: string }) {
shouldFetchAvatar = false
}

if (!avatarImageSrc) {
const config = genConfig(data.me.email)
return <NiceAvatar className={cn('h-16 w-16', className)} {...config} />
if (!avatarImageSrc && !avatarConfigFromEmail && fallback) return fallback
if (!avatarImageSrc && avatarConfigFromEmail) {
return (
<NiceAvatar
className={cn('h-16 w-16', className)}
{...avatarConfigFromEmail}
/>
)
}

return (
<AvatarComponent className={cn('h-16 w-16', className)}>
<AvatarImage
src={avatarImageSrc}
alt={data.me.email}
alt={data?.me?.email}
className="object-cover"
/>
<AvatarFallback>{data.me?.email.substring(0, 2)}</AvatarFallback>
<AvatarFallback>{data?.me?.email?.substring(0, 2)}</AvatarFallback>
</AvatarComponent>
)
}
Expand Down

0 comments on commit d32dfd6

Please sign in to comment.