Skip to content

Commit

Permalink
Merge pull request Expensify#36431 from Expensify/rodrigo-fix-avatar-…
Browse files Browse the repository at this point in the history
…mismatch

Making avatar url be only accountID based
  • Loading branch information
srikarparsi authored Feb 15, 2024
2 parents c4cff43 + f13db5d commit b0bdf6b
Showing 1 changed file with 2 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import Str from 'expensify-common/lib/str';
import _ from 'lodash';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import * as defaultAvatars from '@components/Icon/DefaultAvatars';
import {ConciergeAvatar, FallbackAvatar} from '@components/Icon/Expensicons';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList} from '@src/types/onyx';
import type Login from '@src/types/onyx/Login';
import type IconAsset from '@src/types/utils/IconAsset';
import hashCode from './hashCode';
Expand All @@ -18,12 +14,6 @@ type AvatarSource = IconAsset | string;

type LoginListIndicator = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | '';

let allPersonalDetails: OnyxEntry<PersonalDetailsList>;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (allPersonalDetails = _.isEmpty(val) ? {} : val),
});

/**
* Searches through given loginList for any contact method / login with an error.
*
Expand Down Expand Up @@ -116,19 +106,15 @@ function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset {
}

/**
* Helper method to return default avatar URL associated with login
* Helper method to return default avatar URL associated with the accountID
*/
function getDefaultAvatarURL(accountID: string | number = ''): string {
if (Number(accountID) === CONST.ACCOUNT_ID.CONCIERGE) {
return CONST.CONCIERGE_ICON_URL;
}
// To ensure that the avatar remains unchanged and matches the one returned by the backend,
// utilize an optimistic ID generated from the email instead of directly using the user ID.
const email = allPersonalDetails?.[accountID]?.login;
const originalOptimisticAccountID = email ? generateAccountID(email) : accountID;

// Note that Avatar count starts at 1 which is why 1 has to be added to the result (or else 0 would result in a broken avatar link)
const accountIDHashBucket = (Number(originalOptimisticAccountID) % CONST.DEFAULT_AVATAR_COUNT) + 1;
const accountIDHashBucket = (Number(accountID) % CONST.DEFAULT_AVATAR_COUNT) + 1;
const avatarPrefix = `default-avatar`;

return `${CONST.CLOUDFRONT_URL}/images/avatars/${avatarPrefix}_${accountIDHashBucket}.png`;
Expand Down

0 comments on commit b0bdf6b

Please sign in to comment.