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

[Clean-up] Unify the logic for getting alternateText for getOptions and getOptionData #38220

Closed
mountiny opened this issue Mar 13, 2024 · 4 comments
Assignees
Labels

Comments

@mountiny
Copy link
Contributor

Stemming from improvements to the LHN previews #34411

The getOptions and getOptionData methods get the alternateText differently as highlighted in the PR review here #33966 (comment)

To improve the DX it would be great to unify this logic and ideally simplify it if possible.

@koko57

@mountiny
Copy link
Contributor Author

@koko57 are there any next steps for this one?

@koko57
Copy link
Contributor

koko57 commented Mar 28, 2024

First I would identify how much of the code from here

const lastMessageTextFromReport = getLastMessageTextForReport(report);
const lastActorDetails = personalDetailMap[report.lastActorAccountID] || null;
const lastActorDisplayName =
hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID !== currentUserAccountID ? lastActorDetails.firstName || lastActorDetails.displayName : '';
let lastMessageText = lastActorDisplayName ? `${lastActorDisplayName}: ${lastMessageTextFromReport}` : lastMessageTextFromReport;
if (result.isArchivedRoom) {
const archiveReason =
(lastReportActions[report.reportID] && lastReportActions[report.reportID].originalMessage && lastReportActions[report.reportID].originalMessage.reason) ||
CONST.REPORT.ARCHIVE_REASON.DEFAULT;
lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.${archiveReason}`, {
displayName: archiveReason.displayName || PersonalDetailsUtils.getDisplayNameOrDefault(lodashGet(lastActorDetails, 'displayName')),
policyName: ReportUtils.getPolicyName(report),
});
}
if (result.isThread || result.isMoneyRequestReport) {
result.alternateText = lastMessageTextFromReport.length > 0 ? lastMessageText : Localize.translate(preferredLocale, 'report.noActivityYet');
} else if (result.isChatRoom || result.isPolicyExpenseChat) {
result.alternateText = showChatPreviewLine && !forcePolicyNamePreview && lastMessageText ? lastMessageText : subtitle;
} else if (result.isTaskReport) {
result.alternateText = showChatPreviewLine && lastMessageText ? lastMessageTextFromReport : Localize.translate(preferredLocale, 'report.noActivityYet');
} else {
result.alternateText = showChatPreviewLine && lastMessageText ? lastMessageText : LocalePhoneNumber.formatPhoneNumber(personalDetail.login);
}
reportName = ReportUtils.getReportName(report);
} else {
reportName = ReportUtils.getDisplayNameForParticipant(accountIDs[0]) || LocalePhoneNumber.formatPhoneNumber(personalDetail.login);
result.keyForList = String(accountIDs[0]);
result.alternateText = LocalePhoneNumber.formatPhoneNumber(lodashGet(personalDetails, [accountIDs[0], 'login'], ''));
}
(it's in createOption not getOption now) and here
const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report);
// If the last actor's details are not currently saved in Onyx Collection,
// then try to get that from the last report action if that action is valid
// to get data from.
let lastActorDetails: ActorDetails | null = report.lastActorAccountID && personalDetails?.[report.lastActorAccountID] ? personalDetails[report.lastActorAccountID] : null;
if (!lastActorDetails && visibleReportActionItems[report.reportID]) {
const lastActorDisplayName = visibleReportActionItems[report.reportID]?.person?.[0]?.text;
lastActorDetails = lastActorDisplayName
? {
displayName: lastActorDisplayName,
accountID: report.lastActorAccountID,
}
: null;
}
const shouldShowDisplayName = hasMultipleParticipants && lastActorDetails?.accountID && Number(lastActorDetails.accountID) !== currentUserAccountID;
const lastActorName = lastActorDetails?.firstName ?? lastActorDetails?.displayName;
const lastActorDisplayName = shouldShowDisplayName ? lastActorName : '';
let lastMessageText = lastMessageTextFromReport;
const reportAction = lastReportActions?.[report.reportID];
if (result.isArchivedRoom) {
const archiveReason = (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED && reportAction?.originalMessage?.reason) || CONST.REPORT.ARCHIVE_REASON.DEFAULT;
switch (archiveReason) {
case CONST.REPORT.ARCHIVE_REASON.ACCOUNT_CLOSED:
case CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY:
case CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED: {
lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.${archiveReason}`, {
policyName: ReportUtils.getPolicyName(report, false, policy),
displayName: PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails),
});
break;
}
default: {
lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.default`);
}
}
}
const isThreadMessage =
ReportUtils.isThread(report) && reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && reportAction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
if ((result.isChatRoom || result.isPolicyExpenseChat || result.isThread || result.isTaskReport || isThreadMessage) && !result.isArchivedRoom) {
const lastAction = visibleReportActionItems[report.reportID];
if (lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.RENAMED) {
const newName = lastAction?.originalMessage?.newName ?? '';
result.alternateText = Localize.translate(preferredLocale, 'newRoomPage.roomRenamedTo', {newName});
} else if (ReportActionsUtils.isTaskAction(lastAction)) {
result.alternateText = TaskUtils.getTaskReportActionMessage(lastAction.actionName);
} else if (
lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ROOMCHANGELOG.INVITE_TO_ROOM ||
lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ROOMCHANGELOG.REMOVE_FROM_ROOM ||
lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG.INVITE_TO_ROOM ||
lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG.REMOVE_FROM_ROOM
) {
const targetAccountIDs = lastAction?.originalMessage?.targetAccountIDs ?? [];
const verb =
lastAction.actionName === CONST.REPORT.ACTIONS.TYPE.ROOMCHANGELOG.INVITE_TO_ROOM || lastAction.actionName === CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG.INVITE_TO_ROOM
? Localize.translate(preferredLocale, 'workspace.invite.invited')
: Localize.translate(preferredLocale, 'workspace.invite.removed');
const users = Localize.translate(preferredLocale, targetAccountIDs.length > 1 ? 'workspace.invite.users' : 'workspace.invite.user');
result.alternateText = `${verb} ${targetAccountIDs.length} ${users}`;
const roomName = lastAction?.originalMessage?.roomName ?? '';
if (roomName) {
const preposition =
lastAction.actionName === CONST.REPORT.ACTIONS.TYPE.ROOMCHANGELOG.INVITE_TO_ROOM || lastAction.actionName === CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG.INVITE_TO_ROOM
? ` ${Localize.translate(preferredLocale, 'workspace.invite.to')}`
: ` ${Localize.translate(preferredLocale, 'workspace.invite.from')}`;
result.alternateText += `${preposition} ${roomName}`;
}
} else if (lastAction?.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && lastActorDisplayName && lastMessageTextFromReport) {
result.alternateText = `${lastActorDisplayName}: ${lastMessageText}`;
} else {
result.alternateText = lastMessageTextFromReport.length > 0 ? lastMessageText : Localize.translate(preferredLocale, 'report.noActivityYet');
}
} else {
if (!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') +
displayNamesWithTooltips
.map(({displayName, pronouns}, index) => {
const formattedText = !pronouns ? displayName : `${displayName} (${pronouns})`;
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},`;
}
return '';
})
.join(' ');
}
result.alternateText = lastMessageText || formattedLogin;
}
can be shared and moved out to shared util function. I started back then with moving the logic for getting last actor displayName to getLastActorDisplayName function. I'm also still thinking that maybe it would be good to create a doc with alternateText examples for each type of the chat for both Sidebar and Search Page. But it's something that I'd definitely leave after all higher-priority issues are done 🙂

@mountiny
Copy link
Contributor Author

Alright, I do feel like this is a minor change we dont really need to focus on right now. Thoughts on me closing this issue for the time being until we will have clearer problem to solve?

@koko57
Copy link
Contributor

koko57 commented Mar 28, 2024

ok, I agree! I still remember and will remember of this one. We can come back to this one later 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants