From 94652db104411af377373b3a9dddbc4556d18831 Mon Sep 17 00:00:00 2001
From: Oliver Wilks <ollywilks@googlemail.com>
Date: Wed, 12 Oct 2022 11:31:31 +0100
Subject: [PATCH 1/4] Fix: Email of one user used in alternate Text

---
 src/components/LHNOptionsList/OptionRowLHN.js |  9 ++-------
 src/libs/SidebarUtils.js                      | 13 +++++++++++++
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/components/LHNOptionsList/OptionRowLHN.js b/src/components/LHNOptionsList/OptionRowLHN.js
index 096092187680..4091d88ee0d7 100644
--- a/src/components/LHNOptionsList/OptionRowLHN.js
+++ b/src/components/LHNOptionsList/OptionRowLHN.js
@@ -1,5 +1,4 @@
 import _ from 'underscore';
-import lodashGet from 'lodash/get';
 import React from 'react';
 import PropTypes from 'prop-types';
 import {
@@ -20,7 +19,6 @@ import withLocalize, {withLocalizePropTypes} from '../withLocalize';
 import Text from '../Text';
 import SubscriptAvatar from '../SubscriptAvatar';
 import CONST from '../../CONST';
-import * as ReportUtils from '../../libs/ReportUtils';
 import variables from '../../styles/variables';
 import themeColors from '../../styles/themes/default';
 import SidebarUtils from '../../libs/SidebarUtils';
@@ -93,11 +91,8 @@ const OptionRowLHN = (props) => {
         ? props.hoverStyle.backgroundColor
         : themeColors.sidebar;
     const focusedBackgroundColor = styles.sidebarLinkActive.backgroundColor;
-    const isMultipleParticipant = lodashGet(optionItem, 'participantsList.length', 0) > 1;
 
-    // We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade.
-    const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((optionItem.participantsList || []).slice(0, 10), isMultipleParticipant);
-    const avatarTooltips = !optionItem.isChatRoom && !optionItem.isArchivedRoom ? _.pluck(displayNamesWithTooltips, 'tooltip') : undefined;
+    const avatarTooltips = !optionItem.isChatRoom && !optionItem.isArchivedRoom ? _.pluck(optionItem.displayNamesWithTooltips, 'tooltip') : undefined;
 
     return (
         <Hoverable>
@@ -163,7 +158,7 @@ const OptionRowLHN = (props) => {
                                 <DisplayNames
                                     accessibilityLabel="Chat user display names"
                                     fullTitle={optionItem.text}
-                                    displayNamesWithTooltips={displayNamesWithTooltips}
+                                    displayNamesWithTooltips={optionItem.displayNamesWithTooltips}
                                     tooltipEnabled
                                     numberOfLines={1}
                                     textStyles={displayNameStyle}
diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js
index e9365750daf1..1ade931e8540 100644
--- a/src/libs/SidebarUtils.js
+++ b/src/libs/SidebarUtils.js
@@ -225,6 +225,7 @@ function getOptionData(reportID) {
 
     const hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat;
     const subtitle = ReportUtils.getChatRoomSubtitle(report, policies);
+    const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((personalDetailList || []).slice(0, 10), hasMultipleParticipants);
 
     let lastMessageTextFromReport = '';
     if (ReportUtils.isReportMessageAttachment({text: report.lastMessageText, html: report.lastMessageHtml})) {
@@ -251,6 +252,17 @@ function getOptionData(reportID) {
     if (result.isChatRoom || result.isPolicyExpenseChat) {
         result.alternateText = lastMessageText || subtitle;
     } else {
+        if (hasMultipleParticipants && !lastMessageText) {
+            lastMessageText = Localize.translate(preferredLocale, 'reportActionsView.beginningOfChatHistory')
+                + _.map(displayNamesWithTooltips, ({displayName, pronouns}, index) => {
+                    const formattedText = !_.isEmpty(pronouns) ? `${displayName} (${pronouns})` : displayName;
+
+                    if (index === displayNamesWithTooltips.length - 1) { return `${formattedText}.`; }
+                    if (index === displayNamesWithTooltips.length - 2) { return `${formattedText} ${Localize.translate(preferredLocale, 'common.and')}`; }
+                    if (index < displayNamesWithTooltips.length - 2) { return `${formattedText},`; }
+                }).join(' ');
+        }
+
         result.alternateText = lastMessageText || Str.removeSMSDomain(personalDetail.login);
     }
 
@@ -274,6 +286,7 @@ function getOptionData(reportID) {
     result.participantsList = personalDetailList;
     result.icons = ReportUtils.getIcons(report, personalDetails, policies, personalDetail.avatar);
     result.searchText = OptionsListUtils.getSearchText(report, reportName, personalDetailList, result.isChatRoom || result.isPolicyExpenseChat);
+    result.displayNamesWithTooltips = displayNamesWithTooltips;
 
     return result;
 }

From 33ffa349e2fce6b395ac17fbca961123094fc0b3 Mon Sep 17 00:00:00 2001
From: Oliver Wilks <ollywilks@googlemail.com>
Date: Wed, 12 Oct 2022 15:57:13 +0100
Subject: [PATCH 2/4] Swapped formattedText ternary

---
 src/libs/SidebarUtils.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js
index 1ade931e8540..4d9087526103 100644
--- a/src/libs/SidebarUtils.js
+++ b/src/libs/SidebarUtils.js
@@ -255,7 +255,7 @@ function getOptionData(reportID) {
         if (hasMultipleParticipants && !lastMessageText) {
             lastMessageText = Localize.translate(preferredLocale, 'reportActionsView.beginningOfChatHistory')
                 + _.map(displayNamesWithTooltips, ({displayName, pronouns}, index) => {
-                    const formattedText = !_.isEmpty(pronouns) ? `${displayName} (${pronouns})` : displayName;
+                    const formattedText = _.isEmpty(pronouns) ? displayName : `${displayName} (${pronouns})`;
 
                     if (index === displayNamesWithTooltips.length - 1) { return `${formattedText}.`; }
                     if (index === displayNamesWithTooltips.length - 2) { return `${formattedText} ${Localize.translate(preferredLocale, 'common.and')}`; }

From fba4869bc5a7189974f3d2e6d99f8e24854b5fe3 Mon Sep 17 00:00:00 2001
From: Oliver Wilks <ollywilks@googlemail.com>
Date: Wed, 12 Oct 2022 19:21:03 +0100
Subject: [PATCH 3/4] Added comment to displayNamesWithTooltips

---
 src/libs/SidebarUtils.js | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js
index 384c6c373014..61f6cb1407ea 100644
--- a/src/libs/SidebarUtils.js
+++ b/src/libs/SidebarUtils.js
@@ -233,6 +233,8 @@ function getOptionData(reportID) {
 
     const hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat;
     const subtitle = ReportUtils.getChatRoomSubtitle(report, policies);
+
+    // We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade.
     const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((personalDetailList || []).slice(0, 10), hasMultipleParticipants);
 
     let lastMessageTextFromReport = '';

From d9af57fc99978422e9b15415a7fcc83e5116c033 Mon Sep 17 00:00:00 2001
From: Oliver Wilks <ollywilks@googlemail.com>
Date: Fri, 14 Oct 2022 17:09:16 +0100
Subject: [PATCH 4/4] Added comment to lastMessageText

---
 src/libs/SidebarUtils.js | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/libs/SidebarUtils.js b/src/libs/SidebarUtils.js
index 61f6cb1407ea..e46d4d12f56f 100644
--- a/src/libs/SidebarUtils.js
+++ b/src/libs/SidebarUtils.js
@@ -263,6 +263,8 @@ function getOptionData(reportID) {
         result.alternateText = lastMessageText || subtitle;
     } else {
         if (hasMultipleParticipants && !lastMessageText) {
+            // Here we get the beginning of chat history message and append the display name for each user, adding pronouns if there are any.
+            // We also add a fullstop after the final name, the word "and" before the final name and commas between all previous names.
             lastMessageText = Localize.translate(preferredLocale, 'reportActionsView.beginningOfChatHistory')
                 + _.map(displayNamesWithTooltips, ({displayName, pronouns}, index) => {
                     const formattedText = _.isEmpty(pronouns) ? displayName : `${displayName} (${pronouns})`;
