Skip to content

Commit

Permalink
Merge pull request Expensify#6737 from Expensify/amal-policy-rooms-ui
Browse files Browse the repository at this point in the history
Make Policy Rooms available in Search and LHN
  • Loading branch information
yuwenmemon authored Jan 5, 2022
2 parents 90eeba8 + e5f4402 commit e8b7f12
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 71 deletions.
10 changes: 5 additions & 5 deletions src/components/Avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const propTypes = {
/** Set the size of Avatar */
size: PropTypes.oneOf(['default', 'small']),

/** Whether this avatar is for a default room */
isDefaultChatRoom: PropTypes.bool,
/** Whether this avatar is for a chat room */
isChatRoom: PropTypes.bool,

/** Whether this avatar is for an archived default room */
isArchivedRoom: PropTypes.bool,
Expand All @@ -30,13 +30,13 @@ const defaultProps = {
imageStyles: [],
containerStyles: [],
size: 'default',
isDefaultChatRoom: false,
isChatRoom: false,
isArchivedRoom: false,
};

class Avatar extends PureComponent {
render() {
if (!this.props.source && !this.props.isDefaultChatRoom) {
if (!this.props.source && !this.props.isChatRoom) {
return null;
}

Expand All @@ -46,7 +46,7 @@ class Avatar extends PureComponent {
];
return (
<View pointerEvents="none" style={this.props.containerStyles}>
{this.props.isDefaultChatRoom
{this.props.isChatRoom
? <RoomAvatar avatarStyle={imageStyle} isArchived={this.props.isArchivedRoom} />
: <Image source={{uri: this.props.source}} style={imageStyle} />}
</View>
Expand Down
8 changes: 4 additions & 4 deletions src/components/EmptyStateAvatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ const propTypes = {
avatarImageURLs: PropTypes.arrayOf(PropTypes.string),

/** Whether this avatar is for a custom/default room */
isDefaultChatRoom: PropTypes.bool,
isChatRoom: PropTypes.bool,
};

const defaultProps = {
avatarImageURLs: [],
isDefaultChatRoom: false,
isChatRoom: false,
};

const EmptyStateAvatars = (props) => {
if (!props.avatarImageURLs.length) {
return null;
}

if (props.avatarImageURLs.length === 1 || props.isDefaultChatRoom) {
if (props.avatarImageURLs.length === 1 || props.isChatRoom) {
return (
<Avatar
source={props.avatarImageURLs[0]}
imageStyles={[styles.avatarLarge]}
isDefaultChatRoom={props.isDefaultChatRoom}
isChatRoom={props.isChatRoom}
/>
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/MultipleAvatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const propTypes = {
// eslint-disable-next-line react/forbid-prop-types
secondAvatarStyle: PropTypes.arrayOf(PropTypes.object),

/** Whether this avatar is for a default room */
isDefaultChatRoom: PropTypes.bool,
/** Whether this avatar is for a chat room */
isChatRoom: PropTypes.bool,

/** Whether this avatar is for an archived room */
isArchivedRoom: PropTypes.bool,
Expand All @@ -27,7 +27,7 @@ const defaultProps = {
avatarImageURLs: [],
size: 'default',
secondAvatarStyle: [styles.secondAvatarHovered],
isDefaultChatRoom: false,
isChatRoom: false,
isArchivedRoom: false,
};

Expand All @@ -49,7 +49,7 @@ const MultipleAvatars = (props) => {
<Avatar
source={props.avatarImageURLs[0]}
size={props.size}
isDefaultChatRoom={props.isDefaultChatRoom}
isChatRoom={props.isChatRoom}
isArchivedRoom={props.isArchivedRoom}
/>
</View>
Expand Down
4 changes: 2 additions & 2 deletions src/components/optionPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export default PropTypes.shape({
// Whether the report is pinned or not
isPinned: PropTypes.bool,

// Whether the report corresponds to a default room
isDefaultChatRoom: PropTypes.bool,
// Whether the report corresponds to a chat room
isChatRoom: PropTypes.bool,
});
58 changes: 32 additions & 26 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ function getParticipantNames(personalDetailList) {
*
* @param {Object} report
* @param {Array} personalDetailList
* @param {Boolean} isDefaultChatRoom
* @param {Boolean} isChatRoom
* @return {String}
*/
function getSearchText(report, personalDetailList, isDefaultChatRoom) {
function getSearchText(report, personalDetailList, isChatRoom) {
const searchTerms = [];

if (!isDefaultChatRoom) {
if (!isChatRoom) {
_.each(personalDetailList, (personalDetail) => {
searchTerms.push(personalDetail.displayName);
searchTerms.push(personalDetail.login);
Expand All @@ -180,10 +180,10 @@ function getSearchText(report, personalDetailList, isDefaultChatRoom) {
searchTerms.push(...report.reportName);
searchTerms.push(..._.map(report.reportName.split(','), name => name.trim()));

if (isDefaultChatRoom) {
const defaultRoomSubtitle = ReportUtils.getDefaultRoomSubtitle(report, policies);
searchTerms.push(...defaultRoomSubtitle);
searchTerms.push(..._.map(defaultRoomSubtitle.split(','), name => name.trim()));
if (isChatRoom) {
const chatRoomSubtitle = ReportUtils.getChatRoomSubtitle(report, policies);
searchTerms.push(...chatRoomSubtitle);
searchTerms.push(..._.map(chatRoomSubtitle.split(','), name => name.trim()));
} else {
searchTerms.push(...report.participants);
}
Expand Down Expand Up @@ -216,8 +216,8 @@ function hasReportDraftComment(report) {
function createOption(personalDetailList, report, {
showChatPreviewLine = false, forcePolicyNamePreview = false,
}) {
const isDefaultChatRoom = ReportUtils.isDefaultRoom(report);
const hasMultipleParticipants = personalDetailList.length > 1 || isDefaultChatRoom;
const isChatRoom = ReportUtils.isChatRoom(report);
const hasMultipleParticipants = personalDetailList.length > 1 || isChatRoom;
const personalDetail = personalDetailList[0];
const hasDraftComment = hasReportDraftComment(report);
const hasOutstandingIOU = lodashGet(report, 'hasOutstandingIOU', false);
Expand All @@ -237,11 +237,16 @@ function createOption(personalDetailList, report, {

let text;
let alternateText;
if (isDefaultChatRoom) {
let icons;
if (isChatRoom) {
text = lodashGet(report, ['reportName'], '');
alternateText = (showChatPreviewLine && !forcePolicyNamePreview && lastMessageText)
? lastMessageText
: ReportUtils.getDefaultRoomSubtitle(report, policies);
: ReportUtils.getChatRoomSubtitle(report, policies);

// Chat rooms do not use icons from their users for the avatar so falling back on personalDetails
// doesn't make sense here
icons = lodashGet(report, 'icons', ['']);
} else {
text = hasMultipleParticipants
? _.map(personalDetailList, ({firstName, login}) => firstName || Str.removeSMSDomain(login))
Expand All @@ -250,11 +255,12 @@ function createOption(personalDetailList, report, {
alternateText = (showChatPreviewLine && lastMessageText)
? lastMessageText
: Str.removeSMSDomain(personalDetail.login);
icons = lodashGet(report, 'icons', [personalDetail.avatar]);
}
return {
text,
alternateText,
icons: lodashGet(report, 'icons', [personalDetail.avatar]),
icons,
tooltipText,
participantsList: personalDetailList,

Expand All @@ -267,13 +273,13 @@ function createOption(personalDetailList, report, {
isUnread: report ? report.unreadActionCount > 0 : null,
hasDraftComment,
keyForList: report ? String(report.reportID) : personalDetail.login,
searchText: getSearchText(report, personalDetailList, isDefaultChatRoom),
searchText: getSearchText(report, personalDetailList, isChatRoom),
isPinned: lodashGet(report, 'isPinned', false),
hasOutstandingIOU,
iouReportID: lodashGet(report, 'iouReportID'),
isIOUReportOwner: lodashGet(iouReport, 'ownerEmail', '') === currentUserLogin,
iouReportAmount: lodashGet(iouReport, 'total', 0),
isDefaultChatRoom,
isChatRoom,
isArchivedRoom: ReportUtils.isArchivedRoom(report),
};
}
Expand All @@ -284,10 +290,10 @@ function createOption(personalDetailList, report, {
* @param {String} searchValue
* @param {String} searchText
* @param {Set<String>} [participantNames]
* @param {Boolean} isDefaultChatRoom
* @param {Boolean} isChatRoom
* @returns {Boolean}
*/
function isSearchStringMatch(searchValue, searchText, participantNames = new Set(), isDefaultChatRoom = false) {
function isSearchStringMatch(searchValue, searchText, participantNames = new Set(), isChatRoom = false) {
const searchWords = _.map(
searchValue
.replace(/,/g, ' ')
Expand All @@ -297,7 +303,7 @@ function isSearchStringMatch(searchValue, searchText, participantNames = new Set
return _.every(searchWords, (word) => {
const matchRegex = new RegExp(Str.escapeForRegExp(word), 'i');
const valueToSearch = searchText && searchText.replace(new RegExp(/&nbsp;/g), '');
return matchRegex.test(valueToSearch) || (!isDefaultChatRoom && participantNames.has(word));
return matchRegex.test(valueToSearch) || (!isChatRoom && participantNames.has(word));
});
}

Expand Down Expand Up @@ -384,7 +390,7 @@ function getOptions(reports, personalDetails, activeReportID, {
const logins = lodashGet(report, ['participants'], []);

// Report data can sometimes be incomplete. If we have no logins or reportID then we will skip this entry.
if (!report || !report.reportID || _.isEmpty(logins)) {
if (!report || !report.reportID || (_.isEmpty(logins) && !ReportUtils.isChatRoom(report))) {
return;
}

Expand All @@ -405,7 +411,7 @@ function getOptions(reports, personalDetails, activeReportID, {
return;
}

if (ReportUtils.isDefaultRoom(report) && (!Permissions.canUseDefaultRooms(betas) || excludeDefaultRooms)) {
if (ReportUtils.isChatRoom(report) && (!Permissions.canUseDefaultRooms(betas) || excludeDefaultRooms)) {
return;
}

Expand Down Expand Up @@ -456,9 +462,9 @@ function getOptions(reports, personalDetails, activeReportID, {
}

// Finally check to see if this option is a match for the provided search string if we have one
const {searchText, participantsList, isDefaultChatRoom} = reportOption;
const {searchText, participantsList, isChatRoom} = reportOption;
const participantNames = getParticipantNames(participantsList);
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isDefaultChatRoom)) {
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom)) {
continue;
}

Expand Down Expand Up @@ -503,14 +509,14 @@ function getOptions(reports, personalDetails, activeReportID, {

// If we are prioritizing default rooms in search, do it only once we started something
if (prioritizeDefaultRoomsInSearch && searchValue !== '') {
const reportsSplitByDefaultChatRoom = _.partition(recentReportOptions, option => option.isDefaultChatRoom);
const reportsSplitByDefaultChatRoom = _.partition(recentReportOptions, option => option.isChatRoom);
recentReportOptions = reportsSplitByDefaultChatRoom[0].concat(reportsSplitByDefaultChatRoom[1]);
}

// If we are prioritizing 1:1 chats in search, do it only once we started searching
if (sortByReportTypeInSearch && searchValue !== '') {
recentReportOptions = lodashOrderBy(recentReportOptions, [(option) => {
if (option.isDefaultChatRoom || option.isArchivedRoom) {
if (option.isChatRoom || option.isArchivedRoom) {
return 3;
}
if (!option.login) {
Expand All @@ -528,9 +534,9 @@ function getOptions(reports, personalDetails, activeReportID, {
))) {
return;
}
const {searchText, participantsList, isDefaultChatRoom} = personalDetailOption;
const {searchText, participantsList, isChatRoom} = personalDetailOption;
const participantNames = getParticipantNames(participantsList);
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isDefaultChatRoom)) {
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom)) {
return;
}
personalDetailsOptions.push(personalDetailOption);
Expand Down Expand Up @@ -759,7 +765,7 @@ function getCurrencyListForSections(currencyOptions, searchValue) {
*/
function getReportIcons(report, personalDetails) {
// Default rooms have a specific avatar so we can return any non-empty array
if (ReportUtils.isDefaultRoom(report)) {
if (ReportUtils.isChatRoom(report)) {
return [''];
}
const sortedParticipants = _.map(report.participants, dmParticipant => ({
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ function getFromReportParticipants(reports) {
// skip over default rooms which aren't named by participants.
const reportsToUpdate = {};
_.each(reports, (report) => {
if (report.participants.length <= 0 && !ReportUtils.isDefaultRoom(report)) {
if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report)) {
return;
}

const avatars = OptionsListUtils.getReportIcons(report, details);
const reportName = ReportUtils.isDefaultRoom(report)
const reportName = ReportUtils.isChatRoom(report)
? report.reportName
: _.chain(report.participants)
.filter(participant => participant !== currentUserEmail)
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function getChatReportName(fullReport, chatType) {
}

// For a basic policy room, return its original name
if (ReportUtils.isPolicyRoom({chatType})) {
if (ReportUtils.isUserCreatedPolicyRoom({chatType})) {
return fullReport.reportName;
}

Expand Down Expand Up @@ -187,7 +187,7 @@ function getSimplifiedReportObject(report) {
? getChatReportName(report, chatType)
: report.reportName;
const lastActorEmail = lodashGet(report, 'lastActionActorEmail', '');
const notificationPreference = ReportUtils.isDefaultRoom({chatType})
const notificationPreference = ReportUtils.isChatRoom({chatType})
? lodashGet(report, ['reportNameValuePairs', 'notificationPreferences', currentUserAccountID], 'daily')
: '';

Expand Down
23 changes: 17 additions & 6 deletions src/libs/reportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,25 @@ function isDefaultRoom(report) {
}

/**
* Whether the provided report is a policy room
* Whether the provided report is a user created policy room
* @param {Object} report
* @param {String} report.chatType
* @returns {Boolean}
*/
function isPolicyRoom(report) {
function isUserCreatedPolicyRoom(report) {
return lodashGet(report, ['chatType'], '') === CONST.REPORT.CHAT_TYPE.POLICY_ROOM;
}

/**
* Whether the provided report is a chat room
* @param {Object} report
* @param {String} report.chatType
* @returns {Boolean}
*/
function isChatRoom(report) {
return isUserCreatedPolicyRoom(report) || isDefaultRoom(report);
}

/**
* Given a collection of reports returns the most recently accessed one
*
Expand Down Expand Up @@ -140,8 +150,8 @@ function isArchivedRoom(report) {
* @param {Object} policiesMap must have onyxkey prefix (i.e 'policy_') for keys
* @returns {String}
*/
function getDefaultRoomSubtitle(report, policiesMap) {
if (!isDefaultRoom(report)) {
function getChatRoomSubtitle(report, policiesMap) {
if (!isDefaultRoom(report) && !isUserCreatedPolicyRoom(report)) {
return '';
}
if (report.chatType === CONST.REPORT.CHAT_TYPE.DOMAIN_ALL) {
Expand Down Expand Up @@ -219,8 +229,9 @@ export {
canDeleteReportAction,
sortReportsByLastVisited,
isDefaultRoom,
isPolicyRoom,
getDefaultRoomSubtitle,
isUserCreatedPolicyRoom,
isChatRoom,
getChatRoomSubtitle,
isArchivedRoom,
isConciergeChatReport,
hasExpensifyEmails,
Expand Down
Loading

0 comments on commit e8b7f12

Please sign in to comment.