hedgedoc/frontend/src/components/common/user-avatar/hooks/use-avatar-url.ts
Erik Michelson e411ddf099 refactor(frontend): switch to DTOs from @hedgedoc/commons
Co-authored-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
2025-03-29 22:09:01 +01:00

30 lines
894 B
TypeScript

/*
* SPDX-FileCopyrightText: 2025 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useMemo } from 'react'
import { createAvatar } from '@dicebear/core'
import * as identicon from '@dicebear/identicon'
import type { UserInfoDto } from '@hedgedoc/commons'
/**
* Returns the correct avatar url for a user.
* When the user has no photoUrl, a random avatar is generated from the display name.
*
* @param user The user for which to get the avatar URL
* @return The correct avatar url for the user
*/
export const useAvatarUrl = (user: UserInfoDto): string => {
const { photoUrl, displayName } = user
return useMemo(() => {
if (photoUrl && photoUrl.trim() !== '') {
return photoUrl
}
const avatar = createAvatar(identicon, {
seed: displayName
})
return avatar.toDataUri()
}, [photoUrl, displayName])
}