Skip to content

merge getReportIcons and getAvatarSources functions #8499

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

Merged
merged 33 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4dd6ae4
avoid storing report icons into Onyx
marcochavezf Apr 5, 2022
383c9d1
get avatar source directly from report.icons
marcochavezf Apr 5, 2022
29e1c2e
remove getReportIcons from PersonalDetails
marcochavezf Apr 5, 2022
e80e04a
use getReportIcons in getAvatarSources
marcochavezf Apr 5, 2022
2ecf2e2
get personalDetails from Onyx to get avatar urls
marcochavezf Apr 5, 2022
afcbf42
merge getReportIcons and getAvatarSources into one function
marcochavezf Apr 5, 2022
c118096
fix single avatars by returning an array
marcochavezf Apr 5, 2022
7ec2c8f
fix default icon component if avatar is an empty string
marcochavezf Apr 5, 2022
08e93ef
add comment for group chats
marcochavezf Apr 5, 2022
84858e7
return empty string for firstName in sortedParticipants
marcochavezf Apr 5, 2022
6e3ef16
change fn name getAvatarSources to getReportIcons
marcochavezf Apr 5, 2022
3aac326
add defaultIcon param to getReportIcons
marcochavezf Apr 5, 2022
caf72a5
remove personalDetails subscription and add it as param in getReportI…
marcochavezf Apr 7, 2022
e7404de
pass personalDetails for getReportIcons for ReportActionItemCreated
marcochavezf Apr 7, 2022
cf6dfda
add personalDetails param to getReportIcons usages
marcochavezf Apr 7, 2022
93ff338
move personalDetails to createOption to pass it to getReportIcons
marcochavezf Apr 7, 2022
1981e43
move getReportIcons to reportUtils
marcochavezf Apr 7, 2022
0212cc4
fix errors in getReportIcons
marcochavezf Apr 7, 2022
773fe51
move getDefaultAvatar to reportUtils
marcochavezf Apr 7, 2022
2a42503
fix usages of getDefaultAvatar
marcochavezf Apr 7, 2022
d044382
fix getDefaultAvatar in ProfilePage
marcochavezf Apr 7, 2022
785675c
fix usages of getReportIcons
marcochavezf Apr 7, 2022
68d07a4
remove unnecessary jsdoc params
marcochavezf Apr 7, 2022
36555a2
add policies as param to getReportIcons
marcochavezf Apr 7, 2022
0cea30b
include policies prop via withOnyx to ReportActionItemCreated
marcochavezf Apr 7, 2022
8ba167f
pass policies in when getReportIcons is called
marcochavezf Apr 7, 2022
bba318f
change const name avatarIcons to reportIcons
marcochavezf Apr 7, 2022
6c63d9b
fix issue when personalDetail is not defined
marcochavezf Apr 7, 2022
e16e0ef
Merge branch 'main' into marco-mergeAvatarIconFunctions
marcochavezf Apr 7, 2022
0967f47
rename avatarIcons to icons in MultipleAvatars
marcochavezf Apr 11, 2022
652c131
change avatarIcons to icons in RoomHeaderAvatars
marcochavezf Apr 11, 2022
e5b823d
update usages of RoomHeaderAvatars
marcochavezf Apr 11, 2022
1d3f3b6
change getReportIcons to getIcons
marcochavezf Apr 11, 2022
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
117 changes: 50 additions & 67 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Onyx.connect({
},
});

let formattedPersonalDetails;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS,
callback: val => formattedPersonalDetails = val,
});

/**
* Helper method to return a default avatar
*
Expand Down Expand Up @@ -206,33 +212,53 @@ function hasReportDraftComment(report) {
}

/**
* Get the Avatar urls or return the icon according to the chat type
* Returns the appropriate icons for the given chat report using the stored personalDetails.
* The Avatar sources can be URLs or Icon components according to the chat type.
*
* @param {Object} report
* @param {*} [defaultIcon]
* @returns {Array<*>}
*/
function getAvatarSources(report) {
return _.map(lodashGet(report, 'icons', ['']), (source) => {
if (source) {
return source;
}
if (ReportUtils.isArchivedRoom(report)) {
return Expensicons.DeletedRoomAvatar;
}
if (ReportUtils.isAdminRoom(report)) {
return Expensicons.AdminRoomAvatar;
}
if (ReportUtils.isAnnounceRoom(report)) {
return Expensicons.AnnounceRoomAvatar;
}
if (ReportUtils.isChatRoom(report)) {
return Expensicons.ActiveRoomAvatar;
}
if (ReportUtils.isPolicyExpenseChat(report)) {
return Expensicons.Workspace;
function getReportIcons(report, defaultIcon = null) {
if (!report) {
return [defaultIcon || getDefaultAvatar()];
}
if (ReportUtils.isArchivedRoom(report)) {
return [Expensicons.DeletedRoomAvatar];
}
if (ReportUtils.isAdminRoom(report)) {
return [Expensicons.AdminRoomAvatar];
}
if (ReportUtils.isAnnounceRoom(report)) {
return [Expensicons.AnnounceRoomAvatar];
}
if (ReportUtils.isChatRoom(report)) {
return [Expensicons.ActiveRoomAvatar];
}
if (ReportUtils.isPolicyExpenseChat(report)) {
const policyExpenseChatAvatarSource = lodashGet(policies, [
`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'avatarURL',
]) || Expensicons.Workspace;

// Return the workspace avatar if the user is the owner of the policy expense chat
if (report.isOwnPolicyExpenseChat) {
return [policyExpenseChatAvatarSource];
}
return Expensicons.Profile;
});

// If the user is an admin, return avatar source of the other participant of the report
// (their workspace chat) and the avatar source of the workspace
return [
lodashGet(formattedPersonalDetails, [report.ownerEmail, 'avatar']) || getDefaultAvatar(report.ownerEmail),
policyExpenseChatAvatarSource,
];
}

// Return avatar sources for Group chats
const sortedParticipants = _.map(report.participants, dmParticipant => ({
firstName: lodashGet(formattedPersonalDetails, [dmParticipant, 'firstName'], ''),
avatar: lodashGet(formattedPersonalDetails, [dmParticipant, 'avatar']) || getDefaultAvatar(dmParticipant),
})).sort((first, second) => first.firstName - second.firstName);
return _.map(sortedParticipants, item => item.avatar);
}

/**
Expand Down Expand Up @@ -269,7 +295,6 @@ function createOption(personalDetailList, report, {

const tooltipText = ReportUtils.getReportParticipantsTitle(lodashGet(report, ['participants'], []));
const subtitle = ReportUtils.getChatRoomSubtitle(report, policies);
let icons = getAvatarSources(report);
let text;
let alternateText;
if (isChatRoom || isPolicyExpenseChat) {
Expand All @@ -285,15 +310,11 @@ function createOption(personalDetailList, report, {
alternateText = (showChatPreviewLine && lastMessageText)
? lastMessageText
: Str.removeSMSDomain(personalDetail.login);
if (!report) {
// If the report doesn't exist then we're creating a list of users to invite (using the personalDetailList)
icons = [personalDetail.avatar];
}
}
return {
text,
alternateText,
icons,
icons: getReportIcons(report, personalDetail.avatar),
tooltipText,
ownerEmail: lodashGet(report, ['ownerEmail']),
subtitle,
Expand Down Expand Up @@ -814,47 +835,10 @@ function getCurrencyListForSections(currencyOptions, searchValue) {
};
}

/**
* Returns the appropriate icons for the given chat report using personalDetails if applicable
*
* @param {Object} report
* @param {Object} personalDetails
* @returns {Array<String>}
*/
function getReportIcons(report, personalDetails) {
// Default rooms have a specific avatar so we can return any non-empty array
if (ReportUtils.isChatRoom(report)) {
return [''];
}

if (ReportUtils.isPolicyExpenseChat(report)) {
const policyExpenseChatAvatarURL = lodashGet(policies, [
`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'avatarURL',
]);

// Return the workspace avatar if the user is the owner of the policy expense chat
if (report.isOwnPolicyExpenseChat) {
return [policyExpenseChatAvatarURL];
}

// If the user is an admin, return avatar url of the other participant of the report
// (their workspace chat) and the avatar url of the workspace
return [lodashGet(personalDetails, [report.ownerEmail, 'avatarThumbnail']), policyExpenseChatAvatarURL];
}

const sortedParticipants = _.map(report.participants, dmParticipant => ({
firstName: lodashGet(personalDetails, [dmParticipant, 'firstName'], ''),
avatar: lodashGet(personalDetails, [dmParticipant, 'avatarThumbnail'], '')
|| getDefaultAvatar(dmParticipant),
}))
.sort((first, second) => first.firstName - second.firstName);
return _.map(sortedParticipants, item => item.avatar);
}

export {
addSMSDomainIfPhoneNumber,
isCurrentUser,
getAvatarSources,
getReportIcons,
getSearchOptions,
getNewChatOptions,
getSidebarOptions,
Expand All @@ -864,5 +848,4 @@ export {
getIOUConfirmationOptionsFromMyPersonalDetail,
getIOUConfirmationOptionsFromParticipants,
getDefaultAvatar,
getReportIcons,
};
3 changes: 1 addition & 2 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ function getFromReportParticipants(reports) {
return;
}

const avatars = OptionsListUtils.getReportIcons(report, details);
const reportName = (ReportUtils.isChatRoom(report) || ReportUtils.isPolicyExpenseChat(report))
? report.reportName
: _.chain(report.participants)
Expand All @@ -239,7 +238,7 @@ function getFromReportParticipants(reports) {
.value()
.join(', ');

reportsToUpdate[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = {icons: avatars, reportName};
reportsToUpdate[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = {reportName};
});

// We use mergeCollection such that it updates ONYXKEYS.COLLECTION.REPORT in one go.
Expand Down
6 changes: 0 additions & 6 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import CONST from '../../CONST';
import Log from '../Log';
import * as LoginUtils from '../LoginUtils';
import * as ReportUtils from '../reportUtils';
import * as OptionsListUtils from '../OptionsListUtils';
import Timers from '../Timers';
import * as ReportActions from './ReportActions';
import Growl from '../Growl';
Expand Down Expand Up @@ -1506,11 +1505,6 @@ function createPolicyRoom(policyID, reportName, visibility) {
Log.error('Unable to grab policy room after creation', reportID);
return;
}

// Make sure the report has its icons set
const report = allReports[reportID];
const icons = OptionsListUtils.getReportIcons(report, {});
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {icons});
Navigation.navigate(ROUTES.getReportRoute(reportID));
})
.catch(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ReportDetailsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class ReportDetailsPage extends Component {
>
<View style={styles.mb4}>
<RoomHeaderAvatars
avatarIcons={OptionsListUtils.getAvatarSources(this.props.report)}
avatarIcons={OptionsListUtils.getReportIcons(this.props.report)}
shouldShowLargeAvatars={isPolicyExpenseChat}
/>
</View>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/HeaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const HeaderView = (props) => {
const shouldShowCallButton = isConcierge || !isAutomatedExpensifyAccount;
const avatarTooltip = isChatRoom ? undefined : _.pluck(displayNamesWithTooltips, 'tooltip');
const shouldShowSubscript = isPolicyExpenseChat && !props.report.isOwnPolicyExpenseChat;
const avatarIcons = OptionsListUtils.getAvatarSources(props.report);
const avatarIcons = OptionsListUtils.getReportIcons(props.report);
return (
<View style={[styles.appContentHeader]} nativeID="drag-area">
<View style={[styles.appContentHeaderTitle, !props.isSmallScreenWidth && styles.pl5]}>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItemCreated.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const defaultProps = {

const ReportActionItemCreated = (props) => {
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(props.report);
const avatarIcons = OptionsListUtils.getAvatarSources(props.report);
const avatarIcons = OptionsListUtils.getReportIcons(props.report);
return (
<View style={[
styles.chatContent,
Expand Down