Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Fallback Avatar to Support Themes #38674

Merged
merged 20 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
11 changes: 10 additions & 1 deletion assets/images/avatars/fallback-avatar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 15 additions & 13 deletions src/components/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,28 @@ function Avatar({
setImageError(false);
}, [source]);

if (!source) {
return null;
}

const isWorkspace = type === CONST.ICON_TYPE_WORKSPACE;
const iconSize = StyleUtils.getAvatarSize(size);

const imageStyle: StyleProp<ImageStyle> = [StyleUtils.getAvatarStyle(size), imageStyles, styles.noBorderRadius];
const iconStyle = imageStyles ? [StyleUtils.getAvatarStyle(size), styles.bgTransparent, imageStyles] : undefined;

const iconFillColor = isWorkspace ? StyleUtils.getDefaultWorkspaceAvatarColor(name).fill : fill;
// We pass the color styles down to the SVG for the workspace and fallback avatar.
const useFallBackAvatar = imageError || source === Expensicons.FallbackAvatar;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const useFallBackAvatar = imageError || source === Expensicons.FallbackAvatar;
const useFallBackAvatar = imageError || !source;
const avatarSource = useFallBackAvatar ? fallbackAvatar : source;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this idea, but I'm worried there's a few places in app that are setting the fallback avatar as the avatar. For example here. Maybe we should just return nothing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing as this gets a little hazy, what if I remove this change from the PR (leaving only the theme support) and we handle all the fallback issues in #38743

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling this separately sounds good to me 👍


let iconColors;
if (isWorkspace) {
iconColors = StyleUtils.getDefaultWorkspaceAvatarColor(name);
} else if (useFallBackAvatar) {
iconColors = StyleUtils.getBackgroundColorAndFill(theme.border, theme.icon);
} else {
iconColors = null;
}

const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(name) : fallbackIcon || Expensicons.FallbackAvatar;
const fallbackAvatarTestID = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatarTestID(name) : fallbackIconTestID || 'SvgFallbackAvatar Icon';

const avatarSource = imageError ? fallbackAvatar : source;
const avatarSource = imageError || !source ? fallbackAvatar : source;

return (
<View style={[containerStyles, styles.pointerEventsNone]}>
Expand All @@ -107,13 +114,8 @@ function Avatar({
src={avatarSource}
height={iconSize}
width={iconSize}
fill={imageError ? theme.offline : iconFillColor}
additionalStyles={[
StyleUtils.getAvatarBorderStyle(size, type),
isWorkspace && StyleUtils.getDefaultWorkspaceAvatarColor(name),
imageError && StyleUtils.getBackgroundColorStyle(theme.fallbackIconColor),
iconAdditionalStyles,
]}
fill={imageError ? iconColors?.fill ?? theme.offline : fill}
additionalStyles={[StyleUtils.getAvatarBorderStyle(size, type), iconColors, iconAdditionalStyles]}
/>
</View>
)}
Expand Down
5 changes: 3 additions & 2 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import lodashSet from 'lodash/set';
import lodashSortBy from 'lodash/sortBy';
import Onyx from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {FallbackAvatar} from '@components/Icon/Expensicons';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -1682,10 +1683,10 @@ function getOptions(
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
userToInvite.alternateText = userToInvite.alternateText || searchValue;

// If user doesn't exist, use a default avatar
// If user doesn't exist, always use the fallback avatar
userToInvite.icons = [
{
source: UserUtils.getAvatar('', optimisticAccountID),
source: FallbackAvatar,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source: FallbackAvatar,

Not specifying any source should by design fallback to the fallback avatart

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also found setting the source here causes imageError in Avatar in being true

name: searchValue,
type: CONST.ICON_TYPE_AVATAR,
},
Expand Down
2 changes: 1 addition & 1 deletion src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function generateAccountID(searchValue: string): number {
* @returns
*/
function getDefaultAvatar(accountID = -1, avatarURL?: string): IconAsset {
if (accountID <= 0) {
if (!accountID || accountID <= 0) {
return FallbackAvatar;
}
if (Number(accountID) === CONST.ACCOUNT_ID.CONCIERGE) {
Expand Down
12 changes: 10 additions & 2 deletions src/styles/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ import type {
EReceiptColorName,
EreceiptColorStyle,
ParsableStyle,
SVGAvatarColorStyle,
TextColorStyle,
WorkspaceColorStyle,
} from './types';

const workspaceColorOptions: WorkspaceColorStyle[] = [
const workspaceColorOptions: SVGAvatarColorStyle[] = [
{backgroundColor: colors.blue200, fill: colors.blue700},
{backgroundColor: colors.blue400, fill: colors.blue800},
{backgroundColor: colors.blue700, fill: colors.blue200},
Expand Down Expand Up @@ -276,6 +276,13 @@ function getDefaultWorkspaceAvatarColor(workspaceName: string): ViewStyle {
return workspaceColorOptions[colorHash];
}

/**
* Helper method to return formatted backgroundColor and fill styles
*/
function getBackgroundColorAndFill(backgroundColor: string, fill: string): SVGAvatarColorStyle {
return {backgroundColor, fill};
}

/**
* Helper method to return eReceipt color code
*/
Expand Down Expand Up @@ -1112,6 +1119,7 @@ const staticStyleUtils = {
getComposeTextAreaPadding,
getColorStyle,
getDefaultWorkspaceAvatarColor,
getBackgroundColorAndFill,
getDirectionStyle,
getDropDownButtonHeight,
getEmojiPickerListHeight,
Expand Down
4 changes: 2 additions & 2 deletions src/styles/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ButtonSizeValue = ValueOf<typeof CONST.DROPDOWN_BUTTON_SIZE>;
type ButtonStateName = ValueOf<typeof CONST.BUTTON_STATES>;
type AvatarSize = {width: number};

type WorkspaceColorStyle = {backgroundColor: ColorValue; fill: ColorValue};
type SVGAvatarColorStyle = {backgroundColor: ColorValue; fill: ColorValue};
type EreceiptColorStyle = {backgroundColor: ColorValue; color: ColorValue};
type TextColorStyle = {color: string};

Expand All @@ -55,7 +55,7 @@ export type {
ButtonSizeValue,
ButtonStateName,
AvatarSize,
WorkspaceColorStyle,
SVGAvatarColorStyle,
EreceiptColorStyle,
TextColorStyle,
};
Loading