Skip to content

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

Closed

Description

@mountiny

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

Activity

  1. self-assigned this
    on Mar 13, 2024
  2. mountiny commented on Mar 28, 2024

    @mountiny
    ContributorAuthor

    @koko57 are there any next steps for this one?

  3. koko57 commented on Mar 28, 2024

    @koko57
    Contributor

    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 🙂

  4. mountiny commented on Mar 28, 2024

    @mountiny
    ContributorAuthor

    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?

  5. koko57 commented on Mar 28, 2024

    @koko57
    Contributor

    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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions