From ed16d8a4b2712b41c52d669158c1e8a05df5f6ab Mon Sep 17 00:00:00 2001 From: jubitjohn Date: Tue, 20 Jun 2023 20:52:27 +0530 Subject: [PATCH] Method moved AvatarComponent --- .../components/pages/gallery/AvatarIcon.tsx | 66 +++++++++++++++++-- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/apps/photos/src/components/pages/gallery/AvatarIcon.tsx b/apps/photos/src/components/pages/gallery/AvatarIcon.tsx index 878227ead..00dfc2dcd 100644 --- a/apps/photos/src/components/pages/gallery/AvatarIcon.tsx +++ b/apps/photos/src/components/pages/gallery/AvatarIcon.tsx @@ -1,16 +1,24 @@ -import React from 'react'; +import React, { useEffect, useState, useContext } from 'react'; +import { EnteFile } from 'types/file'; +import { userIdtoEmail } from 'services/collectionService'; +import { User } from 'types/user'; +import { GalleryContext } from 'pages/gallery'; +import { getData, LS_KEYS } from 'utils/storage/localStorage'; +import darkThemeColors from 'themes/colors/dark'; interface AvatarCircleProps { - email: string; - color: string; - size: number; + file: EnteFile; } -const AvatarCircle: React.FC = ({ email, color, size }) => { +const AvatarCircle: React.FC = ({ file }) => { + const [colorCode, setColorCode] = useState(''); + const [userLetter, setUserLetter] = useState(''); + const { idToMail } = useContext(GalleryContext); + const size = 20; const circleStyle = { width: `${size}px`, height: `${size}px`, - backgroundColor: `${color}80`, + backgroundColor: `${colorCode}80`, borderRadius: '50%', display: 'flex', justifyContent: 'center', @@ -21,8 +29,52 @@ const AvatarCircle: React.FC = ({ email, color, size }) => { fontWeight: 'bold', fontSize: `${Math.floor(size / 2)}px`, }; + const user: User = getData(LS_KEYS.USER); + useEffect(() => { + const avatarEnabledFiles = async (file: EnteFile) => { + let email: string; - return
{email}
; + // checking cache + if (idToMail.has(file.ownerID)) { + email = idToMail.get(file.ownerID); + } else { + const userIdEmail = userIdtoEmail(); + const idEmailMap = await userIdEmail; + email = idEmailMap.get(file.ownerID); + if (email) { + idToMail.set(file.ownerID, email); + } + } + + if (file.ownerID !== user.id && idToMail.has(file.ownerID)) { + setUserLetter(email?.charAt(0)?.toUpperCase()); + + const colorIndex = + file.ownerID % darkThemeColors.avatarColors.length; + const colorCode = darkThemeColors.avatarColors[colorIndex]; + setColorCode(colorCode); + } else if ( + file.ownerID === user.id && + file.pubMagicMetadata?.data?.uploaderName + ) { + const uploaderName = file.pubMagicMetadata?.data?.uploaderName; + setUserLetter(uploaderName?.charAt(0)?.toUpperCase()); + const colorCode = '#000000'; + setColorCode(colorCode); + } + }; + + if (file.ownerID !== user.id) { + avatarEnabledFiles(file); + } else if ( + file.ownerID === user.id && + file.pubMagicMetadata?.data?.uploaderName + ) { + avatarEnabledFiles(file); + } + }, []); + + return
{userLetter}
; }; export default AvatarCircle;