-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SidebarUtils.js
390 lines (341 loc) · 16.8 KB
/
SidebarUtils.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
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import lodashOrderBy from 'lodash/orderBy';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../ONYXKEYS';
import * as ReportUtils from './ReportUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import * as Localize from './Localize';
import CONST from '../CONST';
import * as OptionsListUtils from './OptionsListUtils';
import * as CollectionUtils from './CollectionUtils';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as UserUtils from './UserUtils';
// Note: It is very important that the keys subscribed to here are the same
// keys that are connected to SidebarLinks withOnyx(). If there was a key missing from SidebarLinks and it's data was updated
// for that key, then there would be no re-render and the options wouldn't reflect the new data because SidebarUtils.getOrderedReportIDs() wouldn't be triggered.
// There are a couple of keys here which are OK to have stale data. Having redundant subscriptions causes more re-renders which should be avoided.
// Session also can remain stale because the only way for the current user to change is to sign out and sign in, which would clear out all the Onyx
// data anyway and cause SidebarLinks to rerender.
let allReports;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (val) => (allReports = val),
});
let personalDetails;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (personalDetails = val),
});
let priorityMode;
Onyx.connect({
key: ONYXKEYS.NVP_PRIORITY_MODE,
callback: (val) => (priorityMode = val),
});
let betas;
Onyx.connect({
key: ONYXKEYS.BETAS,
callback: (val) => (betas = val),
});
const visibleReportActionItems = {};
const lastReportActions = {};
const reportActions = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
return;
}
const reportID = CollectionUtils.extractCollectionItemID(key);
const actionsArray = ReportActionsUtils.getSortedReportActions(_.toArray(actions));
lastReportActions[reportID] = _.last(actionsArray);
// The report is only visible if it is the last action not deleted that
// does not match a closed or created state.
const reportActionsForDisplay = _.filter(
actionsArray,
(reportAction, actionKey) => ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED,
);
visibleReportActionItems[reportID] = _.last(reportActionsForDisplay);
reportActions[key] = actions;
},
});
let policies;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (val) => (policies = val),
});
let currentUserAccountID;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
if (!val) {
return;
}
currentUserAccountID = val.accountID;
},
});
let preferredLocale;
Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (val) => (preferredLocale = val || CONST.LOCALES.DEFAULT),
});
let resolveSidebarIsReadyPromise;
let sidebarIsReadyPromise = new Promise((resolve) => {
resolveSidebarIsReadyPromise = resolve;
});
function resetIsSidebarLoadedReadyPromise() {
sidebarIsReadyPromise = new Promise((resolve) => {
resolveSidebarIsReadyPromise = resolve;
});
}
function isSidebarLoadedReady() {
return sidebarIsReadyPromise;
}
function setIsSidebarLoadedReady() {
resolveSidebarIsReadyPromise();
}
/**
* @param {String} currentReportId
* @returns {String[]} An array of reportIDs sorted in the proper order
*/
function getOrderedReportIDs(currentReportId) {
const isInGSDMode = priorityMode === CONST.PRIORITY_MODE.GSD;
const isInDefaultMode = !isInGSDMode;
// Filter out all the reports that shouldn't be displayed
const reportsToDisplay = _.filter(allReports, (report) => ReportUtils.shouldReportBeInOptionList(report, currentReportId, isInGSDMode, allReports, betas, policies));
if (_.isEmpty(reportsToDisplay)) {
// Display Concierge chat report when there is no report to be displayed
const conciergeChatReport = _.find(allReports, ReportUtils.isConciergeChatReport);
if (conciergeChatReport) {
reportsToDisplay.push(conciergeChatReport);
}
}
// There are a few properties that need to be calculated for the report which are used when sorting reports.
_.each(reportsToDisplay, (report) => {
// Normally, the spread operator would be used here to clone the report and prevent the need to reassign the params.
// However, this code needs to be very performant to handle thousands of reports, so in the interest of speed, we're just going to disable this lint rule and add
// the reportDisplayName property to the report object directly.
// eslint-disable-next-line no-param-reassign
report.displayName = ReportUtils.getReportName(report);
// eslint-disable-next-line no-param-reassign
report.iouReportAmount = ReportUtils.getMoneyRequestTotal(report, allReports);
});
// The LHN is split into five distinct groups, and each group is sorted a little differently. The groups will ALWAYS be in this order:
// 1. Pinned - Always sorted by reportDisplayName
// 2. Outstanding IOUs - Always sorted by iouReportAmount with the largest amounts at the top of the group
// 3. Drafts - Always sorted by reportDisplayName
// 4. Non-archived reports
// - Sorted by lastVisibleActionCreated in default (most recent) view mode
// - Sorted by reportDisplayName in GSD (focus) view mode
// 5. Archived reports
// - Sorted by lastVisibleActionCreated in default (most recent) view mode
// - Sorted by reportDisplayName in GSD (focus) view mode
let pinnedReports = [];
let outstandingIOUReports = [];
let draftReports = [];
let nonArchivedReports = [];
let archivedReports = [];
_.each(reportsToDisplay, (report) => {
if (report.isPinned) {
pinnedReports.push(report);
return;
}
if (report.hasOutstandingIOU && !ReportUtils.isIOUOwnedByCurrentUser(report, allReports)) {
outstandingIOUReports.push(report);
return;
}
if (report.hasDraft) {
draftReports.push(report);
return;
}
if (ReportUtils.isArchivedRoom(report)) {
archivedReports.push(report);
return;
}
if (ReportUtils.isTaskReport(report) && ReportUtils.isTaskCompleted(report)) {
archivedReports.push(report);
return;
}
nonArchivedReports.push(report);
});
// Sort each group of reports accordingly
pinnedReports = _.sortBy(pinnedReports, (report) => report.displayName.toLowerCase());
outstandingIOUReports = lodashOrderBy(outstandingIOUReports, ['iouReportAmount', (report) => report.displayName.toLowerCase()], ['desc', 'asc']);
draftReports = _.sortBy(draftReports, (report) => report.displayName.toLowerCase());
nonArchivedReports = isInDefaultMode
? lodashOrderBy(nonArchivedReports, ['lastVisibleActionCreated', (report) => report.displayName.toLowerCase()], ['desc', 'asc'])
: lodashOrderBy(nonArchivedReports, [(report) => report.displayName.toLowerCase()], ['asc']);
archivedReports = _.sortBy(archivedReports, (report) => (isInDefaultMode ? report.lastVisibleActionCreated : report.displayName.toLowerCase()));
// For archived reports ensure that most recent reports are at the top by reversing the order of the arrays because underscore will only sort them in ascending order
if (isInDefaultMode) {
archivedReports.reverse();
}
// Now that we have all the reports grouped and sorted, they must be flattened into an array and only return the reportID.
// The order the arrays are concatenated in matters and will determine the order that the groups are displayed in the sidebar.
return _.pluck([].concat(pinnedReports).concat(outstandingIOUReports).concat(draftReports).concat(nonArchivedReports).concat(archivedReports), 'reportID');
}
/**
* Gets all the data necessary for rendering an OptionRowLHN component
*
* @param {String} reportID
* @returns {Object}
*/
function getOptionData(reportID) {
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
const report = allReports[reportKey];
// When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for
// this method to be called after the Onyx data has been cleared out. In that case, it's fine to do
// a null check here and return early.
if (!report || !personalDetails) {
return;
}
const result = {
text: null,
alternateText: null,
pendingAction: null,
allReportErrors: null,
brickRoadIndicator: null,
icons: null,
tooltipText: null,
ownerEmail: null,
ownerAccountID: null,
subtitle: null,
participantsList: null,
login: null,
accountID: null,
reportID: null,
phoneNumber: null,
payPalMeAddress: null,
isUnread: null,
isUnreadWithMention: null,
hasDraftComment: false,
keyForList: null,
searchText: null,
isPinned: false,
hasOutstandingIOU: false,
iouReportID: null,
isIOUReportOwner: null,
iouReportAmount: 0,
isChatRoom: false,
isArchivedRoom: false,
shouldShowSubscript: false,
isPolicyExpenseChat: false,
isMoneyRequestReport: false,
};
const participantPersonalDetailList = _.values(OptionsListUtils.getPersonalDetailsForAccountIDs(report.participantAccountIDs, personalDetails));
const personalDetail = participantPersonalDetailList[0] || {};
result.isThread = ReportUtils.isThread(report);
result.isChatRoom = ReportUtils.isChatRoom(report);
result.isTaskReport = ReportUtils.isTaskReport(report);
if (result.isTaskReport) {
result.isTaskCompleted = ReportUtils.isTaskCompleted(report);
result.isTaskAssignee = ReportUtils.isTaskAssignee(report);
}
result.isArchivedRoom = ReportUtils.isArchivedRoom(report);
result.isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(report);
result.isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
result.shouldShowSubscript = ReportUtils.shouldReportShowSubscript(report);
result.pendingAction = report.pendingFields ? report.pendingFields.addWorkspaceRoom || report.pendingFields.createChat : null;
result.allReportErrors = OptionsListUtils.getAllReportErrors(report, reportActions);
result.brickRoadIndicator = !_.isEmpty(result.allReportErrors) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : '';
result.ownerEmail = report.ownerEmail;
result.ownerAccountID = report.ownerAccountID;
result.reportID = report.reportID;
result.isUnread = ReportUtils.isUnread(report);
result.isUnreadWithMention = ReportUtils.isUnreadWithMention(report);
result.hasDraftComment = report.hasDraft;
result.isPinned = report.isPinned;
result.iouReportID = report.iouReportID;
result.keyForList = String(report.reportID);
result.tooltipText = ReportUtils.getReportParticipantsTitle(report.participantAccountIDs || []);
result.hasOutstandingIOU = report.hasOutstandingIOU;
result.parentReportID = report.parentReportID || null;
const hasMultipleParticipants = participantPersonalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat;
const subtitle = ReportUtils.getChatRoomSubtitle(report);
const login = Str.removeSMSDomain(lodashGet(personalDetail, 'login', ''));
const formattedLogin = Str.isSMSLogin(login) ? LocalePhoneNumber.formatPhoneNumber(login) : login;
// 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((participantPersonalDetailList || []).slice(0, 10), hasMultipleParticipants);
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 = personalDetails[report.lastActorAccountID] || null;
if (!lastActorDetails && visibleReportActionItems[report.reportID]) {
const lastActorDisplayName = lodashGet(visibleReportActionItems[report.reportID], 'person[0].text');
lastActorDetails = lastActorDisplayName
? {
displayName: lastActorDisplayName,
login: report.lastActorEmail,
accountID: report.lastActorAccountID,
}
: null;
}
let lastMessageText =
hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID && Number(lastActorDetails.accountID) !== currentUserAccountID ? `${lastActorDetails.displayName}: ` : '';
lastMessageText += report ? 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 || report.lastActorEmail,
policyName: ReportUtils.getPolicyName(report),
});
}
if ((result.isChatRoom || result.isPolicyExpenseChat || result.isThread || result.isTaskReport) && !result.isArchivedRoom) {
const lastAction = visibleReportActionItems[report.reportID];
if (lodashGet(lastAction, 'actionName', '') === CONST.REPORT.ACTIONS.TYPE.RENAMED) {
const newName = lodashGet(lastAction, 'originalMessage.newName', '');
result.alternateText = Localize.translate(preferredLocale, 'newRoomPage.roomRenamedTo', {newName});
} 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') +
_.map(displayNamesWithTooltips, ({displayName, pronouns}, index) => {
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')}`;
}
if (index < displayNamesWithTooltips.length - 2) {
return `${formattedText},`;
}
}).join(' ');
}
result.alternateText = lastMessageText || formattedLogin;
}
result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result, allReports);
result.iouReportAmount = ReportUtils.getMoneyRequestTotal(result, allReports);
if (!hasMultipleParticipants) {
result.accountID = personalDetail.accountID;
result.login = personalDetail.login;
result.phoneNumber = personalDetail.phoneNumber;
result.payPalMeAddress = personalDetail.payPalMeAddress;
}
const reportName = ReportUtils.getReportName(report);
result.text = reportName;
result.subtitle = subtitle;
result.participantsList = participantPersonalDetailList;
result.icons = ReportUtils.getIcons(report, personalDetails, UserUtils.getAvatar(personalDetail.avatar, personalDetail.accountID), true);
result.searchText = OptionsListUtils.getSearchText(report, reportName, participantPersonalDetailList, result.isChatRoom || result.isPolicyExpenseChat, result.isThread);
result.displayNamesWithTooltips = displayNamesWithTooltips;
return result;
}
export default {
getOptionData,
getOrderedReportIDs,
setIsSidebarLoadedReady,
isSidebarLoadedReady,
resetIsSidebarLoadedReadyPromise,
};