-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathTaskUtils.ts
58 lines (53 loc) · 2.55 KB
/
TaskUtils.ts
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
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report} from '@src/types/onyx';
import type {Message} from '@src/types/onyx/ReportAction';
import type ReportAction from '@src/types/onyx/ReportAction';
import * as CollectionUtils from './CollectionUtils';
import * as Localize from './Localize';
const allReports: Record<string, Report> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
callback: (report, key) => {
if (!key || !report) {
return;
}
const reportID = CollectionUtils.extractCollectionItemID(key);
allReports[reportID] = report;
},
});
/**
* Given the Task reportAction name, return the appropriate message to be displayed and copied to clipboard.
*/
function getTaskReportActionMessage(action: OnyxEntry<ReportAction>): Pick<Message, 'text' | 'html'> {
switch (action?.actionName) {
case CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED:
return {text: Localize.translateLocal('task.messages.completed')};
case CONST.REPORT.ACTIONS.TYPE.TASKCANCELLED:
return {text: Localize.translateLocal('task.messages.canceled')};
case CONST.REPORT.ACTIONS.TYPE.TASKREOPENED:
return {text: Localize.translateLocal('task.messages.reopened')};
case CONST.REPORT.ACTIONS.TYPE.TASKEDITED:
return {
text: action?.message?.[0].text ?? '',
html: action?.message?.[0].html,
};
default:
return {text: Localize.translateLocal('task.task')};
}
}
function getTaskTitle(taskReportID: string, fallbackTitle = ''): string {
const taskReport = allReports[taskReportID] ?? {};
// We need to check for reportID, not just reportName, because when a receiver opens the task for the first time,
// an optimistic report is created with the only property – reportName: 'Chat report',
// and it will be displayed as the task title without checking for reportID to be present.
return Object.hasOwn(taskReport, 'reportID') && taskReport.reportName ? taskReport.reportName : fallbackTitle;
}
function getTaskCreatedMessage(reportAction: OnyxEntry<ReportAction>) {
const taskReportID = reportAction?.childReportID ?? '';
const taskTitle = getTaskTitle(taskReportID, reportAction?.childReportName);
return taskTitle ? Localize.translateLocal('task.messages.created', {title: taskTitle}) : '';
}
export {getTaskReportActionMessage, getTaskTitle, getTaskCreatedMessage};