Skip to content

Commit

Permalink
Merge pull request #6084 from marktoman/issue-6050
Browse files Browse the repository at this point in the history
LHN: Hide the pencil icon when message is sent or draft is deleted and show the icon when user started to type #6050
  • Loading branch information
mountiny authored Nov 3, 2021
2 parents 2281700 + dd4cd8a commit dc67437
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 121 deletions.
1 change: 1 addition & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export default {
REPORT_USER_IS_TYPING: 'reportUserIsTyping_',
REPORT_IOUS: 'reportIOUs_',
POLICY: 'policy_',
REPORTS_WITH_DRAFT: 'reportWithDraft_',
},

// Indicates which locale should be used
Expand Down
55 changes: 34 additions & 21 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Onyx.connect({
callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE,
});

const reportsWithDraft = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORTS_WITH_DRAFT,
callback: (hasDraft, key) => {
if (key) {
reportsWithDraft[key] = hasDraft;
}
},
});

const policies = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
Expand Down Expand Up @@ -178,26 +188,34 @@ function getSearchText(report, personalDetailList, isDefaultChatRoom) {
return _.unique(searchTerms).join(' ');
}

/**
* Determines whether a report has a draft comment.
*
* @param {Object} report
* @return {Boolean}
*/
function hasReportDraftComment(report) {
return report
&& reportsWithDraft
&& lodashGet(reportsWithDraft, `${ONYXKEYS.COLLECTION.REPORTS_WITH_DRAFT}${report.reportID}`, false);
}

/**
* Creates a report list option
*
* @param {Array<Object>} personalDetailList
* @param {Object} [report]
* @param {Object} draftComments
* @param {Boolean} showChatPreviewLine
* @param {Boolean} forcePolicyNamePreview
* @returns {Object}
*/
function createOption(personalDetailList, report, draftComments, {
function createOption(personalDetailList, report, {
showChatPreviewLine = false, forcePolicyNamePreview = false,
}) {
const isDefaultChatRoom = isDefaultRoom(report);
const hasMultipleParticipants = personalDetailList.length > 1 || isDefaultChatRoom;
const personalDetail = personalDetailList[0];
const reportDraftComment = report
&& draftComments
&& lodashGet(draftComments, `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, '');

const hasDraftComment = hasReportDraftComment(report);
const hasOutstandingIOU = lodashGet(report, 'hasOutstandingIOU', false);
const iouReport = hasOutstandingIOU
? lodashGet(iouReports, `${ONYXKEYS.COLLECTION.REPORT_IOUS}${report.iouReportID}`, {})
Expand Down Expand Up @@ -243,7 +261,7 @@ function createOption(personalDetailList, report, draftComments, {
phoneNumber: !hasMultipleParticipants ? personalDetail.phoneNumber : null,
payPalMeAddress: !hasMultipleParticipants ? personalDetail.payPalMeAddress : null,
isUnread: report ? report.unreadActionCount > 0 : null,
hasDraftComment: _.size(reportDraftComment) > 0,
hasDraftComment,
keyForList: report ? String(report.reportID) : personalDetail.login,
searchText: getSearchText(report, personalDetailList, isDefaultChatRoom),
isPinned: lodashGet(report, 'isPinned', false),
Expand Down Expand Up @@ -314,13 +332,12 @@ function isCurrentUser(userDetails) {
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {Object} draftComments
* @param {Number} activeReportID
* @param {Object} options
* @returns {Object}
* @private
*/
function getOptions(reports, personalDetails, draftComments, activeReportID, {
function getOptions(reports, personalDetails, activeReportID, {
betas = [],
selectedOptions = [],
maxRecentReportsToShow = 0,
Expand Down Expand Up @@ -367,17 +384,15 @@ function getOptions(reports, personalDetails, draftComments, activeReportID, {
return;
}

const reportDraftComment = report
&& draftComments
&& lodashGet(draftComments, `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, '');
const hasDraftComment = hasReportDraftComment(report);
const iouReportOwner = lodashGet(report, 'hasOutstandingIOU', false)
? lodashGet(iouReports, [`${ONYXKEYS.COLLECTION.REPORT_IOUS}${report.iouReportID}`, 'ownerEmail'], '')
: '';

const reportContainsIOUDebt = iouReportOwner && iouReportOwner !== currentUserLogin;
const shouldFilterReportIfEmpty = !showReportsWithNoComments && report.lastMessageTimestamp === 0;
const shouldFilterReportIfRead = hideReadReports && report.unreadActionCount === 0;
const shouldShowReportIfHasDraft = showReportsWithDrafts && reportDraftComment && reportDraftComment.length > 0;
const shouldShowReportIfHasDraft = showReportsWithDrafts && hasDraftComment;
const shouldFilterReport = shouldFilterReportIfEmpty || shouldFilterReportIfRead;
if (report.reportID !== activeReportID
&& !report.isPinned
Expand All @@ -398,14 +413,14 @@ function getOptions(reports, personalDetails, draftComments, activeReportID, {
if (logins.length <= 1) {
reportMapForLogins[logins[0]] = report;
}
allReportOptions.push(createOption(reportPersonalDetails, report, draftComments, {
allReportOptions.push(createOption(reportPersonalDetails, report, {
showChatPreviewLine,
forcePolicyNamePreview,
}));
});

const allPersonalDetailsOptions = _.map(personalDetails, personalDetail => (
createOption([personalDetail], reportMapForLogins[personalDetail.login], draftComments, {
createOption([personalDetail], reportMapForLogins[personalDetail.login], {
showChatPreviewLine,
forcePolicyNamePreview,
})
Expand Down Expand Up @@ -522,7 +537,7 @@ function getOptions(reports, personalDetails, draftComments, activeReportID, {
? `+${countryCodeByIP}${searchValue}`
: searchValue;
const userInvitePersonalDetails = getPersonalDetailsForLogins([login], personalDetails);
userToInvite = createOption(userInvitePersonalDetails, null, draftComments, {
userToInvite = createOption(userInvitePersonalDetails, null, {
showChatPreviewLine,
});
userToInvite.icons = [defaultAvatarForUserToInvite];
Expand Down Expand Up @@ -550,7 +565,7 @@ function getSearchOptions(
searchValue = '',
betas,
) {
return getOptions(reports, personalDetails, {}, 0, {
return getOptions(reports, personalDetails, 0, {
betas,
searchValue,
includeRecentReports: true,
Expand Down Expand Up @@ -618,7 +633,7 @@ function getNewChatOptions(
selectedOptions = [],
excludeLogins = [],
) {
return getOptions(reports, personalDetails, {}, 0, {
return getOptions(reports, personalDetails, 0, {
betas,
searchValue,
selectedOptions,
Expand All @@ -635,7 +650,6 @@ function getNewChatOptions(
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {Object} draftComments
* @param {Number} activeReportID
* @param {String} priorityMode
* @param {Array<String>} betas
Expand All @@ -644,7 +658,6 @@ function getNewChatOptions(
function getSidebarOptions(
reports,
personalDetails,
draftComments,
activeReportID,
priorityMode,
betas,
Expand All @@ -662,7 +675,7 @@ function getSidebarOptions(
};
}

return getOptions(reports, personalDetails, draftComments, activeReportID, {
return getOptions(reports, personalDetails, activeReportID, {
betas,
includeRecentReports: true,
includeMultipleParticipantReports: true,
Expand Down
12 changes: 12 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,17 @@ function saveReportComment(reportID, comment) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, comment);
}

/**
* Immediate indication whether the report has a draft comment.
*
* @param {String} reportID
* @param {Boolean} hasDraft
* @returns {Promise}
*/
function setReportWithDraft(reportID, hasDraft) {
return Onyx.merge(`${ONYXKEYS.COLLECTION.REPORTS_WITH_DRAFT}${reportID}`, hasDraft);
}

/**
* Broadcasts whether or not a user is typing on a report over the report's private pusher channel.
*
Expand Down Expand Up @@ -1428,5 +1439,6 @@ export {
syncChatAndIOUReports,
navigateToConciergeChat,
handleInaccessibleReport,
setReportWithDraft,
fetchActionsWithLoadingState,
};
12 changes: 12 additions & 0 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
saveReportComment,
saveReportActionDraft,
broadcastUserIsTyping,
setReportWithDraft,
} from '../../../libs/actions/Report';
import ReportTypingIndicator from './ReportTypingIndicator';
import AttachmentModal from '../../../components/AttachmentModal';
Expand Down Expand Up @@ -331,6 +332,17 @@ class ReportActionCompose extends React.Component {
this.setState({
isCommentEmpty: newComment.length === 0,
});

// Indicate that draft has been created.
if (this.comment.length === 0 && newComment.length !== 0) {
setReportWithDraft(this.props.reportID.toString(), true);
}

// The draft has been deleted.
if (newComment.length === 0) {
setReportWithDraft(this.props.reportID.toString(), false);
}

this.comment = newComment;
this.debouncedSaveReportComment(newComment);
if (newComment) {
Expand Down
Loading

0 comments on commit dc67437

Please sign in to comment.