Skip to content
Merged
14 changes: 5 additions & 9 deletions src/components/Avatar/connected/AccountAvatar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import SingleAvatar from '@components/Avatar/layouts/SingleAvatar';
import {usePersonalDetails} from '@components/OnyxListItemProvider';

import useDefaultAvatars from '@hooks/useDefaultAvatars';
import useStyleUtils from '@hooks/useStyleUtils';

import {buildUserIcon} from '@libs/UserAvatarUtils';

import CONST from '@src/CONST';

import type {StyleProp, ViewStyle} from 'react-native';
import type {ValueOf} from 'type-fest';

import React from 'react';

import useAccountIcons from './useAccountIcons';

type AccountAvatarProps = {
/** Account ID of the user to display the avatar for */
accountID: number;
Expand All @@ -29,14 +27,12 @@ type AccountAvatarProps = {

/**
* Renders a single account's avatar, resolving the icon from the personal-details context (zero Onyx subscriptions).
* Use whenever exactly one account is rendered. Pass `Avatar/UserAvatar` a `source` instead when the avatar is already resolved.
* Use whenever exactly one account is rendered. Reach for `AccountAvatars` when several accounts share one slot,
* or pass `Avatar/UserAvatar` a `source` instead when the avatar is already resolved.
*/
function AccountAvatar({accountID, size = CONST.AVATAR_SIZE.DEFAULT, fallbackDisplayName, containerStyle}: AccountAvatarProps) {
const personalDetails = usePersonalDetails();
const defaultAvatars = useDefaultAvatars();
const StyleUtils = useStyleUtils();

const icon = buildUserIcon({accountID, personalDetails, defaultAvatars});
const [icon] = useAccountIcons([accountID]);

return (
<SingleAvatar
Expand Down
64 changes: 64 additions & 0 deletions src/components/Avatar/connected/AccountAvatarWithCardFeed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import SubscriptCardFeedAvatar from '@components/Avatar/layouts/SubscriptCardFeedAvatar';

import CONST from '@src/CONST';
import type {CardFeed} from '@src/types/onyx/CardFeeds';

import type {ColorValue, StyleProp, ViewStyle} from 'react-native';
import type {ValueOf} from 'type-fest';

import React from 'react';

import useAccountIcons from './useAccountIcons';

type AccountAvatarWithCardFeedProps = {
/** Account ID of the cardholder. `CONST.DEFAULT_NUMBER_ID` renders a placeholder avatar, so the card icon keeps its slot when the cardholder is unknown. */
accountID: number;

/** Card feed to display as the subscript */
cardFeed: CardFeed;

/** Size of the subscript card feed icon */
cardFeedIconSize?: {width: number; height: number};

/** Border color for the subscript card feed icon container */
borderColor?: ColorValue;

/** Size of the avatar */
size?: ValueOf<typeof CONST.AVATAR_SIZE>;

/** Display name used as a fallback for the avatar tooltip */
fallbackDisplayName?: string;

/** Container styles for the avatar */
containerStyle?: StyleProp<ViewStyle>;
};

/**
* Renders one account's avatar with a card feed icon as its subscript, resolving the icon from the personal-details
* context (zero Onyx subscriptions). Use for cardholder rows; reach for `AccountAvatar` when there is no card feed.
*/
function AccountAvatarWithCardFeed({
accountID,
cardFeed,
cardFeedIconSize,
borderColor,
size = CONST.AVATAR_SIZE.DEFAULT,
fallbackDisplayName,
containerStyle,
}: AccountAvatarWithCardFeedProps) {
const [icon] = useAccountIcons([accountID]);

return (
<SubscriptCardFeedAvatar
primaryAvatar={icon}
cardFeed={cardFeed}
cardFeedIconSize={cardFeedIconSize}
size={size}
containerStyle={containerStyle}
subscriptAvatarBorderColor={borderColor}
fallbackDisplayName={fallbackDisplayName}
/>
);
}

export default AccountAvatarWithCardFeed;
116 changes: 116 additions & 0 deletions src/components/Avatar/connected/MultiAccountAvatar.tsx
Comment thread
jmusial marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import HorizontalAvatars from '@components/Avatar/layouts/HorizontalAvatars';
import type {HorizontalStackingOptions} from '@components/Avatar/layouts/HorizontalAvatars';
import SingleAvatar from '@components/Avatar/layouts/SingleAvatar';

import useLocalize from '@hooks/useLocalize';

import {sortIconsByName} from '@libs/ReportUtils';

import CONST from '@src/CONST';
import type {InvitedEmailsToAccountIDs} from '@src/types/onyx';
import type {Icon} from '@src/types/onyx/OnyxCommon';

import type {ValueOf} from 'type-fest';

import lodashSortBy from 'lodash/sortBy';
import React from 'react';

import useAccountIcons from './useAccountIcons';

type SortByValue = ValueOf<typeof CONST.REPORT_ACTION_AVATARS.SORT_BY>;

type SortBy =
| readonly [typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.NAME]
| readonly [typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.ID]
| readonly [typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.REVERSE]
| readonly [typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.NAME, typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.REVERSE]
| readonly [typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.ID, typeof CONST.REPORT_ACTION_AVATARS.SORT_BY.REVERSE];

type MultiAccountAvatarProps = {
/** Account IDs to display avatars for. Entries equal to `CONST.DEFAULT_NUMBER_ID` are dropped; when none remain, a single placeholder avatar renders so the slot keeps its size. */
accountIDs: number[];

/** Options for the horizontal stack */
horizontalOptions?: HorizontalStackingOptions;

/** How to order the avatars before rendering them. Every avatar sits in an equivalent slot, so any order is renderable. Omit to leave them in the order `accountIDs` was passed */
sortBy?: SortBy;

/** Emails of invited, not-yet-registered accounts. Also seeds a deterministic fallback avatar for each invited account */
invitedEmailsToAccountIDs?: InvitedEmailsToAccountIDs;

/** Set the size of avatars */
size?: ValueOf<typeof CONST.AVATAR_SIZE>;

/** Display name used as a fallback for the avatar tooltip */
fallbackDisplayName?: string;

/** Whether the avatars are displayed within a report action */
isInReportAction?: boolean;
};

/** Applies the requested ordering. `undefined` leaves the icons in the order the account IDs were passed. */
function sortIcons(icons: Icon[], sortBy: SortBy | undefined, localeCompare: ReturnType<typeof useLocalize>['localeCompare']) {
if (!sortBy) {
return icons;
}

const sortKeys: readonly SortByValue[] = sortBy;

let sortedIcons = icons;
if (sortKeys.includes(CONST.REPORT_ACTION_AVATARS.SORT_BY.NAME)) {
// Icons built by `useAccountIcons` carry their own `displayName`, so no personal-details lookup is needed here
sortedIcons = sortIconsByName(icons, undefined, localeCompare);
} else if (sortKeys.includes(CONST.REPORT_ACTION_AVATARS.SORT_BY.ID)) {
sortedIcons = lodashSortBy(icons, (icon) => icon.id);
}

return sortKeys.includes(CONST.REPORT_ACTION_AVATARS.SORT_BY.REVERSE) ? [...sortedIcons].reverse() : sortedIcons;
}

/**
* Renders several known accounts as an overlapping row of avatars, resolving the icons from the personal-details context
* (zero Onyx subscriptions). Use `AccountAvatar` when there is exactly one account, and `ReportActionAvatars` when the
* actors still have to be resolved from a report, a report action or a policy.
*/
function MultiAccountAvatar({
accountIDs,
horizontalOptions,
sortBy,
invitedEmailsToAccountIDs,
size = CONST.AVATAR_SIZE.DEFAULT,
fallbackDisplayName,
isInReportAction = false,
}: MultiAccountAvatarProps) {
const {localeCompare} = useLocalize();

const filteredAccountIDs = accountIDs.filter((accountID) => accountID !== CONST.DEFAULT_NUMBER_ID);
const hasAccountsToRender = filteredAccountIDs.length > 0;
// When no account resolves, render one placeholder avatar instead of collapsing the slot,
// matching the fallback ReportActionAvatars renders for an empty account list.
const icons = useAccountIcons(hasAccountsToRender ? filteredAccountIDs : [CONST.DEFAULT_NUMBER_ID], invitedEmailsToAccountIDs);
Comment thread
roryabraham marked this conversation as resolved.
Resolved
const placeholderIcon = icons.at(0);

if (!hasAccountsToRender && placeholderIcon) {
return (
<SingleAvatar
avatar={placeholderIcon}
size={size}
containerStyles={[]}
fallbackDisplayName={fallbackDisplayName}
/>
);
}

return (
<HorizontalAvatars
{...horizontalOptions}
size={size}
icons={sortIcons(icons, sortBy, localeCompare)}
isInReportAction={isInReportAction}
fallbackDisplayName={fallbackDisplayName}
/>
);
}

export default MultiAccountAvatar;
30 changes: 30 additions & 0 deletions src/components/Avatar/connected/useAccountIcons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {usePersonalDetails} from '@components/OnyxListItemProvider';

import useDefaultAvatars from '@hooks/useDefaultAvatars';

import {buildUserIcon} from '@libs/UserAvatarUtils';

import type {InvitedEmailsToAccountIDs} from '@src/types/onyx';
import type {Icon} from '@src/types/onyx/OnyxCommon';

/**
* Resolves account IDs into avatar {@link Icon}s from the personal-details context and the default-avatar set.
*
* @param invitedEmailsToAccountIDs Emails of invited, not-yet-registered accounts. An account listed here gets a
* deterministic fallback avatar seeded from its email, so it looks the same before and after it registers.
*/
function useAccountIcons(accountIDs: number[], invitedEmailsToAccountIDs?: InvitedEmailsToAccountIDs): Icon[] {
const personalDetails = usePersonalDetails();
const defaultAvatars = useDefaultAvatars();

return accountIDs.map((accountID) =>
buildUserIcon({
accountID,
personalDetails,
defaultAvatars,
invitedEmail: invitedEmailsToAccountIDs ? Object.keys(invitedEmailsToAccountIDs).find((email) => invitedEmailsToAccountIDs[email] === accountID) : undefined,
}),
);
}

export default useAccountIcons;
75 changes: 44 additions & 31 deletions src/components/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ActivityIndicator from '@components/ActivityIndicator';
import Avatar from '@components/Avatar';
import AccountAvatar from '@components/Avatar/connected/AccountAvatar';
import WorkspaceAvatar from '@components/Avatar/WorkspaceAvatar';
import Badge from '@components/Badge';
import {useIsCompactMenu} from '@components/CompactMenuContext';
Expand Down Expand Up @@ -278,9 +279,6 @@ type MenuItemBaseProps = ForwardedFSClassProps &
/** Whether this item is selected */
isSelected?: boolean;

/** Prop to identify if we should load avatars vertically instead of diagonally */
shouldStackHorizontally?: boolean;

/** Prop to represent the size of the avatar images to be shown */
avatarSize?: ValueOf<typeof CONST.AVATAR_SIZE>;

Expand Down Expand Up @@ -563,7 +561,6 @@ function MenuItem({
isLabelHoverable = true,
rightLabel,
isSelected = false,
shouldStackHorizontally = false,
shouldShowDescriptionOnTop = false,
shouldShowRightComponent = false,
rightComponent,
Expand Down Expand Up @@ -838,6 +835,10 @@ function MenuItem({

const isIDPassed = !!iconReportID || !!iconAccountID || iconAccountID === CONST.DEFAULT_NUMBER_ID;

// A known account renders straight from the personal-details context. Only the report-derived avatars need `ReportActionAvatars`.
const rightIconAccountIDNumber = Number(rightIconAccountID);
const hasRightIconAccount = !!rightIconAccountID && rightIconAccountIDNumber > 0;

return (
<View
style={rootWrapperStyle}
Expand Down Expand Up @@ -923,20 +924,31 @@ function MenuItem({
]}
>
{!!leftComponent && <View style={[styles.mr3]}>{leftComponent}</View>}
{isIDPassed && (
<ReportActionAvatars
subscriptAvatarBorderColor={getSubscriptAvatarBackgroundColor(isHovered, pressed, theme.hoverComponentBG, theme.buttonHoveredBG)}
singleAvatarContainerStyle={[styles.actionAvatar, styles.mr3]}
size={avatarSize}
secondaryAvatarContainerStyle={[
StyleUtils.getBackgroundAndBorderStyle(theme.sidebar),
pressed && interactive ? StyleUtils.getBackgroundAndBorderStyle(theme.buttonPressedBG) : undefined,
isHovered && !pressed && interactive ? StyleUtils.getBackgroundAndBorderStyle(theme.border) : undefined,
]}
reportID={iconReportID}
accountIDs={iconAccountID ? [iconAccountID] : undefined}
/>
)}
{isIDPassed &&
(iconAccountID ? (
<AccountAvatar
accountID={iconAccountID}
size={avatarSize}
containerStyle={[styles.actionAvatar, styles.mr3]}
/>
) : (
<ReportActionAvatars
subscriptAvatarBorderColor={getSubscriptAvatarBackgroundColor(
isHovered,
pressed,
theme.hoverComponentBG,
theme.buttonHoveredBG,
)}
singleAvatarContainerStyle={[styles.actionAvatar, styles.mr3]}
size={avatarSize}
secondaryAvatarContainerStyle={[
StyleUtils.getBackgroundAndBorderStyle(theme.sidebar),
pressed && interactive ? StyleUtils.getBackgroundAndBorderStyle(theme.buttonPressedBG) : undefined,
isHovered && !pressed && interactive ? StyleUtils.getBackgroundAndBorderStyle(theme.border) : undefined,
]}
reportID={iconReportID}
/>
))}
{!icon && iconType !== CONST.ICON_TYPE_WORKSPACE && shouldPutLeftPaddingWhenNoIcon && (
<View
style={[
Expand Down Expand Up @@ -1134,19 +1146,20 @@ function MenuItem({
)}
{(!!rightIconAccountID || !!rightIconReportID) && (
<View style={[styles.alignItemsCenter, styles.justifyContentCenter, brickRoadIndicator ? styles.mr2 : styles.mrn2]}>
<ReportActionAvatars
subscriptAvatarBorderColor={isHovered ? theme.activeComponentBG : theme.componentBG}
singleAvatarContainerStyle={[styles.actionAvatar, styles.mr2]}
reportID={rightIconReportID}
size={CONST.AVATAR_SIZE.SMALL}
horizontalStacking={
shouldStackHorizontally && {
isHovered,
isPressed: pressed,
}
}
accountIDs={!!rightIconAccountID && Number(rightIconAccountID) > 0 ? [Number(rightIconAccountID)] : undefined}
/>
{hasRightIconAccount ? (
<AccountAvatar
accountID={rightIconAccountIDNumber}
containerStyle={[styles.actionAvatar, styles.mr2]}
size={CONST.AVATAR_SIZE.SMALL}
/>
) : (
<ReportActionAvatars
subscriptAvatarBorderColor={isHovered ? theme.activeComponentBG : theme.componentBG}
singleAvatarContainerStyle={[styles.actionAvatar, styles.mr2]}
reportID={rightIconReportID}
size={CONST.AVATAR_SIZE.SMALL}
/>
)}
</View>
)}
{!!brickRoadIndicator && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ function ReportFieldView(reportField: EnrichedPolicyReportField, report: OnyxEnt
shouldGreyOutWhenDisabled={false}
numberOfLinesTitle={0}
interactive={!reportField.isFieldDisabled}
shouldStackHorizontally={false}
onSecondaryInteraction={() => {}}
titleWithTooltips={[]}
brickRoadIndicator={reportField.violation ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined}
Expand Down
Loading
Loading