mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 09:04:44 -04:00
feature: add identicon generation to users without photo
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
parent
55398e2428
commit
a8b3b117dc
17 changed files with 210 additions and 23 deletions
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { useMemo } from 'react'
|
||||
import { createAvatar } from '@dicebear/core'
|
||||
import identicon from '@dicebear/identicon'
|
||||
|
||||
/**
|
||||
* Returns the correct avatar url for a user.
|
||||
* When an empty or no photoUrl is given, a random avatar is generated from the displayName.
|
||||
*
|
||||
* @param photoUrl The photo url of the user to use. Maybe empty or not set.
|
||||
* @param displayName The display name of the user to use as input to the random avatar.
|
||||
* @return The correct avatar url for the user.
|
||||
*/
|
||||
export const useAvatarUrl = (photoUrl: string | undefined, displayName: string): string => {
|
||||
return useMemo(() => {
|
||||
if (photoUrl && photoUrl.trim() !== '') {
|
||||
return photoUrl
|
||||
}
|
||||
const avatar = createAvatar(identicon, {
|
||||
seed: displayName
|
||||
})
|
||||
return avatar.toDataUriSync()
|
||||
}, [photoUrl, displayName])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue