Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/molecules/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react'
import { useState, useEffect } from 'react'
import { AvatarProps } from './Avatar.types'

/** Los mismos 10 colores que usaba la v8, determinados por el último dígito del userId */
/** Los mismos 10 colores que usaba la v8, determinados por un índice estable */
const AVATAR_COLORS = [
'#0076ba',
'#229f9c',
Expand All @@ -16,13 +16,14 @@ const AVATAR_COLORS = [
'#ff6363',
]

const getColorByUserId = (userId: number): string => {
if (userId >= 0) {
const digits = userId.toString().split('')
const position = parseInt(digits[digits.length - 1], 10)
return AVATAR_COLORS[position]
}
return AVATAR_COLORS[Math.floor(Math.random() * AVATAR_COLORS.length)]
const getColorIndex = (fullName?: string): number => {
const trimmedNameLength = fullName?.trim().length ?? 0
return trimmedNameLength % 10
}

const getAvatarColor = (fullName?: string): string => {
const position = getColorIndex(fullName)
return AVATAR_COLORS[position]
}

const getInitials = (fullName: string): string =>
Expand All @@ -35,11 +36,10 @@ const getInitials = (fullName: string): string =>

/**
* Muestra la foto de perfil del usuario o un placeholder circular con sus iniciales.
* El color de fondo es determinista según el `userId`.
* El color de fondo es determinista según el largo del `fullName`.
*/
export const Avatar = ({
fullName,
userId,
picture,
size = 50,
fontSize = 14,
Expand All @@ -51,8 +51,8 @@ export const Avatar = ({
const [imageValid, setImageValid] = useState<boolean>(false)

useEffect(() => {
setBgColor(getColorByUserId(userId))
}, [userId])
setBgColor(getAvatarColor(fullName))
}, [fullName])

const wrapperStyle: React.CSSProperties = {
alignItems: 'center',
Expand Down
4 changes: 2 additions & 2 deletions src/molecules/Avatar/Avatar.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface AvatarProps {
/** Nombre completo del usuario (se usan las primeras 2 iniciales como fallback) */
fullName: string
/** ID del usuario (determina el color de fondo de forma determinista) */
userId: number
/** ID del usuario (determina el color de fondo de forma determinista; si no existe, se usa el largo del nombre) */
userId?: number
/** URL de la foto de perfil (opcional) */
picture?: string
/** Diámetro en píxeles */
Expand Down
Loading