-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathOptionsListUtils.js
857 lines (773 loc) · 30.3 KB
/
OptionsListUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/* eslint-disable no-continue */
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import lodashOrderBy from 'lodash/orderBy';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
import * as ReportUtils from './reportUtils';
import * as Localize from './Localize';
import Permissions from './Permissions';
import md5 from './md5';
import * as Expensicons from '../components/Icon/Expensicons';
/**
* OptionsListUtils is used to build a list options passed to the OptionsList component. Several different UI views can
* be configured to display different results based on the options passed to the private getOptions() method. Public
* methods should be named for the views they build options for and then exported for use in a component.
*/
let currentUserLogin;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => currentUserLogin = val && val.email,
});
let currentUser;
Onyx.connect({
key: ONYXKEYS.USER,
callback: val => currentUser = val,
});
let countryCodeByIP;
Onyx.connect({
key: ONYXKEYS.COUNTRY_CODE,
callback: val => countryCodeByIP = val || 1,
});
let preferredLocale;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: val => preferredLocale = val || CONST.DEFAULT_LOCALE,
});
const reportsWithDraft = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORTS_WITH_DRAFT,
callback: (hasDraft, key) => {
if (!key) {
return;
}
reportsWithDraft[key] = hasDraft;
},
});
const policies = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (policy, key) => {
if (!policy || !key || !policy.name) {
return;
}
policies[key] = policy;
},
});
const iouReports = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_IOUS,
callback: (iouReport, key) => {
if (!iouReport || !key || !iouReport.ownerEmail) {
return;
}
iouReports[key] = iouReport;
},
});
/**
* Helper method to return a default avatar
*
* @param {String} [login]
* @returns {String}
*/
function getDefaultAvatar(login = '') {
// There are 8 possible default avatars, so we choose which one this user has based
// on a simple hash of their login (which is converted from HEX to INT)
const loginHashBucket = (parseInt(md5(login).substring(0, 4), 16) % 8) + 1;
return `${CONST.CLOUDFRONT_URL}/images/avatars/avatar_${loginHashBucket}.png`;
}
// We are initializing a default avatar here so that we use the same default color for each user we are inviting. This
// will update when the OptionsListUtils re-loads. But will stay the same color for the life of the JS session.
const defaultAvatarForUserToInvite = getDefaultAvatar();
/**
* Adds expensify SMS domain (@expensify.sms) if login is a phone number and if it's not included yet
*
* @param {String} login
* @return {String}
*/
function addSMSDomainIfPhoneNumber(login) {
if (Str.isValidPhone(login) && !Str.isValidEmail(login)) {
return login + CONST.SMS.DOMAIN;
}
return login;
}
/**
* Returns the personal details for an array of logins
*
* @param {Array} logins
* @param {Object} personalDetails
* @returns {Array}
*/
function getPersonalDetailsForLogins(logins, personalDetails) {
return _.map(logins, (login) => {
let personalDetail = personalDetails[login];
if (!personalDetail) {
personalDetail = {
login,
displayName: login,
avatar: getDefaultAvatar(login),
};
}
return personalDetail;
});
}
/**
* Constructs a Set with all possible names (displayName, firstName, lastName, email) for all participants in a report,
* to be used in isSearchStringMatch.
*
* @param {Array<Object>} personalDetailList
* @return {Set<String>}
*/
function getParticipantNames(personalDetailList) {
// We use a Set because `Set.has(value)` on a Set of with n entries is up to n (or log(n)) times faster than
// `_.contains(Array, value)` for an Array with n members.
const participantNames = new Set();
_.each(personalDetailList, (participant) => {
if (participant.login) {
participantNames.add(participant.login.toLowerCase());
}
if (participant.firstName) {
participantNames.add(participant.firstName.toLowerCase());
}
if (participant.lastName) {
participantNames.add(participant.lastName.toLowerCase());
}
if (participant.displayName) {
participantNames.add(participant.displayName.toLowerCase());
}
});
return participantNames;
}
/**
* Returns a string with all relevant search terms.
* Default should be serachable by policy/domain name but not by participants.
*
* @param {Object} report
* @param {Array} personalDetailList
* @param {Boolean} isChatRoomOrPolicyExpenseChat
* @return {String}
*/
function getSearchText(report, personalDetailList, isChatRoomOrPolicyExpenseChat) {
const searchTerms = [];
if (!isChatRoomOrPolicyExpenseChat) {
_.each(personalDetailList, (personalDetail) => {
searchTerms.push(personalDetail.displayName);
searchTerms.push(personalDetail.login);
});
}
if (report) {
searchTerms.push(...report.reportName);
searchTerms.push(..._.map(report.reportName.split(','), name => name.trim()));
if (isChatRoomOrPolicyExpenseChat) {
const chatRoomSubtitle = ReportUtils.getChatRoomSubtitle(report, policies);
searchTerms.push(...chatRoomSubtitle);
searchTerms.push(..._.map(chatRoomSubtitle.split(','), name => name.trim()));
} else {
searchTerms.push(...report.participants);
}
}
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);
}
/**
* Get the Avatar urls or return the icon according to the chat type
*
* @param {Object} report
* @returns {Array<*>}
*/
function getAvatarSources(report) {
return _.map(lodashGet(report, 'icons', ['']), (source) => {
if (source) {
return source;
}
if (ReportUtils.isArchivedRoom(report)) {
return Expensicons.DeletedRoomAvatar;
}
if (ReportUtils.isChatRoom(report)) {
return Expensicons.ActiveRoomAvatar;
}
if (ReportUtils.isPolicyExpenseChat(report)) {
return Expensicons.Workspace;
}
return Expensicons.Profile;
});
}
/**
* Creates a report list option
*
* @param {Array<Object>} personalDetailList
* @param {Object} report
* @param {Object} options
* @param {Boolean} [options.showChatPreviewLine]
* @param {Boolean} [options.forcePolicyNamePreview]
* @returns {Object}
*/
function createOption(personalDetailList, report, {
showChatPreviewLine = false, forcePolicyNamePreview = false,
}) {
const isChatRoom = ReportUtils.isChatRoom(report);
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(report);
const hasMultipleParticipants = personalDetailList.length > 1 || isChatRoom || isPolicyExpenseChat;
const personalDetail = personalDetailList[0];
const hasDraftComment = hasReportDraftComment(report);
const hasOutstandingIOU = lodashGet(report, 'hasOutstandingIOU', false);
const iouReport = hasOutstandingIOU
? lodashGet(iouReports, `${ONYXKEYS.COLLECTION.REPORT_IOUS}${report.iouReportID}`, {})
: {};
const lastActorDetails = report ? _.find(personalDetailList, {login: report.lastActorEmail}) : null;
const lastMessageTextFromReport = ReportUtils.isReportMessageAttachment(lodashGet(report, 'lastMessageText', ''))
? `[${Localize.translateLocal('common.attachment')}]`
: Str.htmlDecode(lodashGet(report, 'lastMessageText', ''));
let lastMessageText = report && hasMultipleParticipants && lastActorDetails
? `${lastActorDetails.displayName}: `
: '';
lastMessageText += report ? lastMessageTextFromReport : '';
const tooltipText = ReportUtils.getReportParticipantsTitle(lodashGet(report, ['participants'], []));
const subtitle = ReportUtils.getChatRoomSubtitle(report, policies);
let text;
let alternateText;
if (isChatRoom || isPolicyExpenseChat) {
text = lodashGet(report, ['reportName'], '');
alternateText = (showChatPreviewLine && !forcePolicyNamePreview && lastMessageText)
? lastMessageText
: subtitle;
} else {
text = hasMultipleParticipants
? _.map(personalDetailList, ({firstName, login}) => firstName || Str.removeSMSDomain(login))
.join(', ')
: lodashGet(report, ['reportName'], personalDetail.displayName);
alternateText = (showChatPreviewLine && lastMessageText)
? lastMessageText
: Str.removeSMSDomain(personalDetail.login);
}
return {
text,
alternateText,
icons: getAvatarSources(report),
tooltipText,
ownerEmail: lodashGet(report, ['ownerEmail']),
subtitle,
participantsList: personalDetailList,
// It doesn't make sense to provide a login in the case of a report with multiple participants since
// there isn't any one single login to refer to for a report.
login: !hasMultipleParticipants ? personalDetail.login : null,
reportID: report ? report.reportID : null,
phoneNumber: !hasMultipleParticipants ? personalDetail.phoneNumber : null,
payPalMeAddress: !hasMultipleParticipants ? personalDetail.payPalMeAddress : null,
isUnread: report ? report.unreadActionCount > 0 : null,
hasDraftComment,
keyForList: report ? String(report.reportID) : personalDetail.login,
searchText: getSearchText(report, personalDetailList, isChatRoom || isPolicyExpenseChat),
isPinned: lodashGet(report, 'isPinned', false),
hasOutstandingIOU,
iouReportID: lodashGet(report, 'iouReportID'),
isIOUReportOwner: lodashGet(iouReport, 'ownerEmail', '') === currentUserLogin,
iouReportAmount: lodashGet(iouReport, 'total', 0),
isChatRoom,
isArchivedRoom: ReportUtils.isArchivedRoom(report),
shouldShowSubscript: isPolicyExpenseChat && !report.isOwnPolicyExpenseChat,
isPolicyExpenseChat,
};
}
/**
* Searches for a match when provided with a value
*
* @param {String} searchValue
* @param {String} searchText
* @param {Set<String>} [participantNames]
* @param {Boolean} isChatRoom
* @returns {Boolean}
*/
function isSearchStringMatch(searchValue, searchText, participantNames = new Set(), isChatRoom = false) {
const searchWords = _.map(
searchValue
.replace(/,/g, ' ')
.split(' '),
word => word.trim(),
);
return _.every(searchWords, (word) => {
const matchRegex = new RegExp(Str.escapeForRegExp(word), 'i');
const valueToSearch = searchText && searchText.replace(new RegExp(/ /g), '');
return matchRegex.test(valueToSearch) || (!isChatRoom && participantNames.has(word));
});
}
/**
* Returns the given userDetails is currentUser or not.
* @param {Object} userDetails
* @returns {Boolean}
*/
function isCurrentUser(userDetails) {
if (!userDetails) {
// If userDetails is null or undefined
return false;
}
// If user login is mobile number, append sms domain if not appended already.
const userDetailsLogin = addSMSDomainIfPhoneNumber(userDetails.login);
// Initial check with currentUserLogin
let result = currentUserLogin.toLowerCase() === userDetailsLogin.toLowerCase();
const loginList = _.isEmpty(currentUser) || _.isEmpty(currentUser.loginList) ? [] : currentUser.loginList;
let index = 0;
// Checking userDetailsLogin against to current user login options.
while (index < loginList.length && !result) {
if (loginList[index].partnerUserID.toLowerCase() === userDetailsLogin.toLowerCase()) {
result = true;
}
index++;
}
return result;
}
/**
* Build the options
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {Number} activeReportID
* @param {Object} options
* @returns {Object}
* @private
*/
function getOptions(reports, personalDetails, activeReportID, {
betas = [],
selectedOptions = [],
maxRecentReportsToShow = 0,
excludeLogins = [],
excludeDefaultRooms = false,
includeMultipleParticipantReports = false,
includePersonalDetails = false,
includeRecentReports = false,
prioritizePinnedReports = false,
prioritizeDefaultRoomsInSearch = false,
// When sortByReportTypeInSearch flag is true, recentReports will include the personalDetails options as well.
sortByReportTypeInSearch = false,
sortByLastMessageTimestamp = false,
searchValue = '',
showChatPreviewLine = false,
showReportsWithNoComments = false,
hideReadReports = false,
sortByAlphaAsc = false,
forcePolicyNamePreview = false,
prioritizeIOUDebts = false,
prioritizeReportsWithDraftComments = false,
}) {
let recentReportOptions = [];
const pinnedReportOptions = [];
let personalDetailsOptions = [];
const iouDebtReportOptions = [];
const draftReportOptions = [];
const reportMapForLogins = {};
let sortProperty = sortByLastMessageTimestamp
? ['lastMessageTimestamp']
: ['lastVisitedTimestamp'];
if (sortByAlphaAsc) {
sortProperty = ['reportName'];
}
const sortDirection = [sortByAlphaAsc ? 'asc' : 'desc'];
let orderedReports = lodashOrderBy(reports, sortProperty, sortDirection);
// Move the archived Rooms to the last
orderedReports = _.sortBy(orderedReports, report => ReportUtils.isArchivedRoom(report));
const allReportOptions = [];
_.each(orderedReports, (report) => {
const isChatRoom = ReportUtils.isChatRoom(report);
const isDefaultRoom = ReportUtils.isDefaultRoom(report);
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(report);
const logins = lodashGet(report, ['participants'], []);
// Report data can sometimes be incomplete. If we have no logins or reportID then we will skip this entry.
const shouldFilterNoParticipants = _.isEmpty(logins) && !isChatRoom && !isDefaultRoom && !isPolicyExpenseChat;
if (!report || !report.reportID || shouldFilterNoParticipants) {
return;
}
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 && !isDefaultRoom;
const shouldFilterReportIfRead = hideReadReports && report.unreadActionCount === 0;
const shouldFilterReport = shouldFilterReportIfEmpty || shouldFilterReportIfRead;
if (report.reportID !== activeReportID
&& !report.isPinned
&& !hasDraftComment
&& shouldFilterReport
&& !reportContainsIOUDebt) {
return;
}
if (isChatRoom && (!Permissions.canUseDefaultRooms(betas) || excludeDefaultRooms)) {
return;
}
if (isPolicyExpenseChat && !Permissions.canUsePolicyExpenseChat(betas)) {
return;
}
if (ReportUtils.isUserCreatedPolicyRoom(report) && !Permissions.canUsePolicyRooms(betas)) {
return;
}
const reportPersonalDetails = getPersonalDetailsForLogins(logins, personalDetails);
// Save the report in the map if this is a single participant so we can associate the reportID with the
// personal detail option later.
if (logins.length <= 1) {
reportMapForLogins[logins[0]] = report;
}
const isSearchingSomeonesPolicyExpenseChat = !report.isOwnPolicyExpenseChat && searchValue !== '';
allReportOptions.push(createOption(reportPersonalDetails, report, {
showChatPreviewLine,
forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat : forcePolicyNamePreview,
}));
});
const allPersonalDetailsOptions = _.map(personalDetails, personalDetail => (
createOption([personalDetail], reportMapForLogins[personalDetail.login], {
showChatPreviewLine,
forcePolicyNamePreview,
})
));
// Always exclude already selected options and the currently logged in user
const loginOptionsToExclude = [...selectedOptions, {login: currentUserLogin}];
_.each(excludeLogins, (login) => {
loginOptionsToExclude.push({login});
});
if (includeRecentReports) {
for (let i = 0; i < allReportOptions.length; i++) {
// Stop adding options to the recentReports array when we reach the maxRecentReportsToShow value
if (recentReportOptions.length > 0 && recentReportOptions.length === maxRecentReportsToShow) {
break;
}
const reportOption = allReportOptions[i];
// Skip if we aren't including multiple participant reports and this report has multiple participants
if (!includeMultipleParticipantReports && !reportOption.login) {
continue;
}
// Check the report to see if it has a single participant and if the participant is already selected
if (reportOption.login && _.some(loginOptionsToExclude, option => option.login === reportOption.login)) {
continue;
}
// Finally check to see if this option is a match for the provided search string if we have one
const {searchText, participantsList, isChatRoom} = reportOption;
const participantNames = getParticipantNames(participantsList);
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom)) {
continue;
}
// If the report is pinned and we are using the option to display pinned reports on top then we need to
// collect the pinned reports so we can sort them alphabetically once they are collected
if (prioritizePinnedReports && reportOption.isPinned) {
pinnedReportOptions.push(reportOption);
} else if (prioritizeIOUDebts && reportOption.hasOutstandingIOU && !reportOption.isIOUReportOwner) {
iouDebtReportOptions.push(reportOption);
} else if (prioritizeReportsWithDraftComments && reportOption.hasDraftComment) {
draftReportOptions.push(reportOption);
} else {
recentReportOptions.push(reportOption);
}
// Add this login to the exclude list so it won't appear when we process the personal details
if (reportOption.login) {
loginOptionsToExclude.push({login: reportOption.login});
}
}
}
// If we are prioritizing reports with draft comments, add them before the normal recent report options
// and sort them by report name.
if (prioritizeReportsWithDraftComments) {
const sortedDraftReports = lodashOrderBy(draftReportOptions, ['text'], ['asc']);
recentReportOptions = sortedDraftReports.concat(recentReportOptions);
}
// If we are prioritizing IOUs the user owes, add them before the normal recent report options and reports
// with draft comments.
if (prioritizeIOUDebts) {
const sortedIOUReports = lodashOrderBy(iouDebtReportOptions, ['iouReportAmount'], ['desc']);
recentReportOptions = sortedIOUReports.concat(recentReportOptions);
}
// If we are prioritizing our pinned reports then shift them to the front and sort them by report name
if (prioritizePinnedReports) {
const sortedPinnedReports = lodashOrderBy(pinnedReportOptions, ['text'], ['asc']);
recentReportOptions = sortedPinnedReports.concat(recentReportOptions);
}
// If we are prioritizing default rooms in search, do it only once we started something
if (prioritizeDefaultRoomsInSearch && searchValue !== '') {
const reportsSplitByDefaultChatRoom = _.partition(recentReportOptions, option => option.isChatRoom);
recentReportOptions = reportsSplitByDefaultChatRoom[0].concat(reportsSplitByDefaultChatRoom[1]);
}
if (includePersonalDetails) {
// Next loop over all personal details removing any that are selectedUsers or recentChats
_.each(allPersonalDetailsOptions, (personalDetailOption) => {
if (_.some(loginOptionsToExclude, loginOptionToExclude => (
loginOptionToExclude.login === personalDetailOption.login
))) {
return;
}
const {searchText, participantsList, isChatRoom} = personalDetailOption;
const participantNames = getParticipantNames(participantsList);
if (searchValue && !isSearchStringMatch(searchValue, searchText, participantNames, isChatRoom)) {
return;
}
personalDetailsOptions.push(personalDetailOption);
});
}
let userToInvite = null;
if (searchValue
&& recentReportOptions.length === 0
&& personalDetailsOptions.length === 0
&& !isCurrentUser({login: searchValue})
&& _.every(selectedOptions, option => option.login !== searchValue)
&& ((Str.isValidEmail(searchValue) && !Str.isDomainEmail(searchValue)) || Str.isValidPhone(searchValue))
&& (!_.find(loginOptionsToExclude, loginOptionToExclude => loginOptionToExclude.login === searchValue.toLowerCase()))
&& (searchValue !== CONST.EMAIL.CHRONOS || Permissions.canUseChronos(betas))
) {
// If the phone number doesn't have an international code then let's prefix it with the
// current user's international code based on their IP address.
const login = (Str.isValidPhone(searchValue) && !searchValue.includes('+'))
? `+${countryCodeByIP}${searchValue}`
: searchValue;
const userInvitePersonalDetails = getPersonalDetailsForLogins([login], personalDetails);
userToInvite = createOption(userInvitePersonalDetails, null, {
showChatPreviewLine,
});
userToInvite.icons = [defaultAvatarForUserToInvite];
}
// If we are prioritizing 1:1 chats in search, do it only once we started searching
if (sortByReportTypeInSearch && searchValue !== '') {
// When sortByReportTypeInSearch is true, recentReports will be returned with all the reports including personalDetailsOptions in the correct Order.
recentReportOptions.push(...personalDetailsOptions);
personalDetailsOptions = [];
recentReportOptions = lodashOrderBy(recentReportOptions, [(option) => {
if (option.isChatRoom || option.isArchivedRoom) {
return 3;
}
if (!option.login) {
return 2;
}
return 1;
}], ['asc']);
}
return {
personalDetails: personalDetailsOptions,
recentReports: recentReportOptions,
userToInvite,
};
}
/**
* Build the options for the Search view
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {String} searchValue
* @param {Array<String>} betas
* @returns {Object}
*/
function getSearchOptions(
reports,
personalDetails,
searchValue = '',
betas,
) {
return getOptions(reports, personalDetails, 0, {
betas,
searchValue: searchValue.trim(),
includeRecentReports: true,
includeMultipleParticipantReports: true,
maxRecentReportsToShow: 0, // Unlimited
prioritizePinnedReports: false,
prioritizeDefaultRoomsInSearch: false,
sortByReportTypeInSearch: true,
showChatPreviewLine: true,
showReportsWithNoComments: true,
includePersonalDetails: true,
sortByLastMessageTimestamp: false,
forcePolicyNamePreview: true,
prioritizeIOUDebts: false,
});
}
/**
* Build the IOUConfirmation options for showing MyPersonalDetail
*
* @param {Object} myPersonalDetail
* @param {String} amountText
* @returns {Object}
*/
function getIOUConfirmationOptionsFromMyPersonalDetail(myPersonalDetail, amountText) {
return {
text: myPersonalDetail.displayName,
alternateText: myPersonalDetail.login,
icons: [myPersonalDetail.avatar],
descriptiveText: amountText,
login: myPersonalDetail.login,
};
}
/**
* Build the IOUConfirmationOptions for showing participants
*
* @param {Array} participants
* @param {String} amountText
* @returns {Array}
*/
function getIOUConfirmationOptionsFromParticipants(
participants, amountText,
) {
return _.map(participants, participant => ({
...participant, descriptiveText: amountText,
}));
}
/**
* Build the options for the New Group view
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {Array<String>} betas
* @param {String} searchValue
* @param {Array} selectedOptions
* @param {Array} excludeLogins
* @returns {Object}
*/
function getNewChatOptions(
reports,
personalDetails,
betas = [],
searchValue = '',
selectedOptions = [],
excludeLogins = [],
) {
return getOptions(reports, personalDetails, 0, {
betas,
searchValue: searchValue.trim(),
selectedOptions,
excludeDefaultRooms: true,
includeRecentReports: true,
includePersonalDetails: true,
maxRecentReportsToShow: 5,
excludeLogins,
});
}
/**
* Build the options for the Sidebar a.k.a. LHN
*
* @param {Object} reports
* @param {Object} personalDetails
* @param {Number} activeReportID
* @param {String} priorityMode
* @param {Array<String>} betas
* @returns {Object}
*/
function getSidebarOptions(
reports,
personalDetails,
activeReportID,
priorityMode,
betas,
) {
let sideBarOptions = {
prioritizeIOUDebts: true,
prioritizeReportsWithDraftComments: true,
};
if (priorityMode === CONST.PRIORITY_MODE.GSD) {
sideBarOptions = {
hideReadReports: true,
sortByAlphaAsc: true,
};
}
return getOptions(reports, personalDetails, activeReportID, {
betas,
includeRecentReports: true,
includeMultipleParticipantReports: true,
maxRecentReportsToShow: 0, // Unlimited
sortByLastMessageTimestamp: true,
showChatPreviewLine: true,
prioritizePinnedReports: true,
...sideBarOptions,
});
}
/**
* Helper method that returns the text to be used for the header's message and title (if any)
*
* @param {Boolean} hasSelectableOptions
* @param {Boolean} hasUserToInvite
* @param {String} searchValue
* @param {Boolean} [maxParticipantsReached]
* @return {String}
*/
function getHeaderMessage(hasSelectableOptions, hasUserToInvite, searchValue, maxParticipantsReached = false) {
if (maxParticipantsReached) {
return Localize.translate(preferredLocale, 'messages.maxParticipantsReached');
}
if (searchValue && CONST.REGEX.DIGITS_AND_PLUS.test(searchValue) && !Str.isValidPhone(searchValue)) {
return Localize.translate(preferredLocale, 'messages.errorMessageInvalidPhone');
}
// Without a search value, it would be very confusing to see a search validation message.
// Therefore, this skips the validation when there is no search value.
if (searchValue && !hasSelectableOptions && !hasUserToInvite) {
if (/^\d+$/.test(searchValue)) {
return Localize.translate(preferredLocale, 'messages.errorMessageInvalidPhone');
}
return Localize.translate(preferredLocale, 'common.noResultsFound');
}
return '';
}
/**
* Returns the currency list for sections display
*
* @param {Object} currencyOptions
* @param {String} searchValue
* @returns {Array}
*/
function getCurrencyListForSections(currencyOptions, searchValue) {
const filteredOptions = _.filter(currencyOptions, currencyOption => (
isSearchStringMatch(searchValue, currencyOption.searchText)));
return {
// returns filtered options i.e. options with string match if search text is entered
currencyOptions: filteredOptions,
};
}
/**
* Returns the appropriate icons for the given chat report using personalDetails if applicable
*
* @param {Object} report
* @param {Object} personalDetails
* @returns {Array<String>}
*/
function getReportIcons(report, personalDetails) {
// Default rooms have a specific avatar so we can return any non-empty array
if (ReportUtils.isChatRoom(report)) {
return [''];
}
if (ReportUtils.isPolicyExpenseChat(report)) {
const policyExpenseChatAvatarURL = lodashGet(policies, [
`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`, 'avatarURL',
]);
// Return the workspace avatar if the user is the owner of the policy expense chat
if (report.isOwnPolicyExpenseChat) {
return [policyExpenseChatAvatarURL];
}
// If the user is an admin, return avatar url of the other participant of the report
// (their workspace chat) and the avatar url of the workspace
return [lodashGet(personalDetails, [report.ownerEmail, 'avatarThumbnail']), policyExpenseChatAvatarURL];
}
const sortedParticipants = _.map(report.participants, dmParticipant => ({
firstName: lodashGet(personalDetails, [dmParticipant, 'firstName'], ''),
avatar: lodashGet(personalDetails, [dmParticipant, 'avatarThumbnail'], '')
|| getDefaultAvatar(dmParticipant),
}))
.sort((first, second) => first.firstName - second.firstName);
return _.map(sortedParticipants, item => item.avatar);
}
export {
addSMSDomainIfPhoneNumber,
isCurrentUser,
getAvatarSources,
getSearchOptions,
getNewChatOptions,
getSidebarOptions,
getHeaderMessage,
getPersonalDetailsForLogins,
getCurrencyListForSections,
getIOUConfirmationOptionsFromMyPersonalDetail,
getIOUConfirmationOptionsFromParticipants,
getDefaultAvatar,
getReportIcons,
};