-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathTaskUtils.ts
55 lines (50 loc) · 2.57 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
import type {OnyxCollection, 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 Localize from './Localize';
import {getReportActionHtml, getReportActionText} from './ReportActionsUtils';
let allReports: OnyxCollection<Report> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (reports) => {
allReports = reports;
},
});
/**
* 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.TASK_COMPLETED:
return {text: Localize.translateLocal('task.messages.completed')};
case CONST.REPORT.ACTIONS.TYPE.TASK_CANCELLED:
return {text: Localize.translateLocal('task.messages.canceled')};
case CONST.REPORT.ACTIONS.TYPE.TASK_REOPENED:
return {text: Localize.translateLocal('task.messages.reopened')};
case CONST.REPORT.ACTIONS.TYPE.TASK_EDITED:
return {
text: getReportActionText(action),
html: getReportActionHtml(action),
};
default:
return {text: Localize.translateLocal('task.task')};
}
}
function getTaskTitle(taskReportID: string, fallbackTitle = ''): string {
const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${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') && 'reportName' in taskReport && typeof taskReport.reportName === 'string' ? taskReport.reportName : fallbackTitle;
}
function getTaskCreatedMessage(reportAction: OnyxEntry<ReportAction>) {
const taskReportID = reportAction?.childReportID ?? '-1';
const taskTitle = getTaskTitle(taskReportID, reportAction?.childReportName);
return taskTitle ? Localize.translateLocal('task.messages.created', {title: taskTitle}) : '';
}
export {getTaskReportActionMessage, getTaskTitle, getTaskCreatedMessage};