Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'ReportActionsUtils.js' lib to TypeScript #28570

Merged
merged 18 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.WORKSPACE_INVITE_MEMBERS_DRAFT]: Record<string, number>;
[ONYXKEYS.COLLECTION.REPORT]: OnyxTypes.Report;
[ONYXKEYS.COLLECTION.REPORT_METADATA]: OnyxTypes.ReportMetadata;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportAction;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: OnyxTypes.ReportActions;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS]: string;
[ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS]: OnyxTypes.ReportActionReactions;
[ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT]: string;
Expand Down
7 changes: 4 additions & 3 deletions src/languages/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ReportAction} from '../types/onyx';
import en from './en';

type AddressLineParams = {
Expand Down Expand Up @@ -42,15 +43,15 @@ type LocalTimeParams = {
};

type EditActionParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
};

type DeleteActionParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
};

type DeleteConfirmationParams = {
action: NonNullable<unknown>;
action: ReportAction | null;
};

type BeginningOfChatHistoryDomainRoomPartOneParams = {
Expand Down
499 changes: 207 additions & 292 deletions src/libs/ReportActionsUtils.js → src/libs/ReportActionsUtils.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView
}

updatedIOUReport.lastMessageText = iouReportLastMessageText;
updatedIOUReport.lastVisibleActionCreated = lastVisibleAction.created;
updatedIOUReport.lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved

updatedReportPreviewAction = {...reportPreviewAction};
const messageText = Localize.translateLocal('iou.payerOwesAmount', {
Expand Down Expand Up @@ -1570,7 +1570,7 @@ function deleteMoneyRequest(transactionID, reportAction, isSingleTransactionView
hasOutstandingIOU: false,
iouReportID: null,
lastMessageText: ReportActionsUtils.getLastVisibleMessage(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}).lastMessageText,
lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}).created,
lastVisibleActionCreated: lodashGet(ReportActionsUtils.getLastVisibleAction(iouReport.chatReportID, {[reportPreviewAction.reportActionID]: null}), 'created'),
},
},
]
Expand Down
10 changes: 5 additions & 5 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ function addActions(reportID, text = '', file) {
const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportActionsUtils.getLastVisibleMessage(reportID);
if (lastMessageText || lastMessageTranslationKey) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID);
const lastVisibleActionCreated = lastVisibleAction.created;
const lastActorAccountID = lastVisibleAction.actorAccountID;
const lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');
const lastActorAccountID = lodashGet(lastVisibleAction, 'actorAccountID');
failureReport = {
lastMessageTranslationKey,
lastMessageText,
Expand Down Expand Up @@ -1043,8 +1043,8 @@ function deleteReportComment(reportID, reportAction) {
const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportActionsUtils.getLastVisibleMessage(originalReportID, optimisticReportActions);
if (lastMessageText || lastMessageTranslationKey) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions);
const lastVisibleActionCreated = lastVisibleAction.created;
const lastActorAccountID = lastVisibleAction.actorAccountID;
const lastVisibleActionCreated = lodashGet(lastVisibleAction, 'created');
const lastActorAccountID = lodashGet(lastVisibleAction, 'actorAccountID');
optimisticReport = {
lastMessageTranslationKey,
lastMessageText,
Expand Down Expand Up @@ -1225,7 +1225,7 @@ function editReportComment(reportID, originalReportAction, textForNewComment) {
];

const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(originalReportID, optimisticReportActions);
if (reportActionID === lastVisibleAction.reportActionID) {
if (reportActionID === lodashGet(lastVisibleAction, 'reportActionID')) {
const lastMessageText = ReportUtils.formatReportLastMessageText(reportComment);
const optimisticReport = {
lastMessageTranslationKey: '',
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ function cancelTask(taskReportID, taskTitle, originalStateNum, originalStatusNum
key: `${ONYXKEYS.COLLECTION.REPORT}${parentReport.reportID}`,
value: {
lastMessageText: ReportActionsUtils.getLastVisibleMessage(parentReport.reportID, {[parentReportAction.reportActionID]: null}).lastMessageText,
lastVisibleActionCreated: ReportActionsUtils.getLastVisibleAction(parentReport.reportID, {[parentReportAction.reportActionID]: null}).created,
lastVisibleActionCreated: lodashGet(ReportActionsUtils.getLastVisibleAction(parentReport.reportID, {[parentReportAction.reportActionID]: null}), 'created'),
},
},
{
Expand Down
9 changes: 2 additions & 7 deletions src/libs/isReportMessageAttachment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import CONST from '../CONST';

type IsReportMessageAttachmentParams = {
text: string;
html: string;
translationKey: string;
};
import {Message} from '../types/onyx/ReportAction';

/**
* Check whether a report action is Attachment or not.
* Ignore messages containing [Attachment] as the main content. Attachments are actions with only text as [Attachment].
*
* @param reportActionMessage report action's message as text, html and translationKey
*/
export default function isReportMessageAttachment({text, html, translationKey}: IsReportMessageAttachmentParams): boolean {
export default function isReportMessageAttachment({text, html, translationKey}: Message): boolean {
if (!text || !html) {
return false;
}
Expand Down
54 changes: 48 additions & 6 deletions src/types/onyx/OriginalMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {ValueOf} from 'type-fest';
import CONST from '../../CONST';
import DeepValueOf from '../utils/DeepValueOf';

type ActionName = DeepValueOf<typeof CONST.REPORT.ACTIONS.TYPE>;

type OriginalMessageApproved = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.APPROVED;
originalMessage: unknown;
};

type IOUDetails = {
amount: number;
comment?: string;
currency: string;
};

type OriginalMessageIOU = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.IOU;
Expand All @@ -8,23 +22,38 @@ type OriginalMessageIOU = {
IOUTransactionID?: string;

IOUReportID?: number;

/** Only exists when we are sending money */
IOUDetails?: IOUDetails;

amount: number;
comment?: string;
currency: string;
lastModified?: string;
participantAccountIDs?: number[];
type: string;
type: ValueOf<typeof CONST.IOU.REPORT_ACTION_TYPE>;
};
};

type FlagSeverityName = 'spam' | 'inconsiderate' | 'bullying' | 'intimidation' | 'harassment' | 'assault';
type FlagSeverityName = ValueOf<
Pick<
typeof CONST.MODERATION,
'FLAG_SEVERITY_SPAM' | 'FLAG_SEVERITY_INCONSIDERATE' | 'FLAG_SEVERITY_INTIMIDATION' | 'FLAG_SEVERITY_BULLYING' | 'FLAG_SEVERITY_HARASSMENT' | 'FLAG_SEVERITY_ASSAULT'
>
>;
type FlagSeverity = {
accountID: number;
timestamp: string;
};

type DecisionName = ValueOf<
Pick<
typeof CONST.MODERATION,
'MODERATOR_DECISION_PENDING' | 'MODERATOR_DECISION_PENDING_HIDE' | 'MODERATOR_DECISION_PENDING_REMOVE' | 'MODERATOR_DECISION_APPROVED' | 'MODERATOR_DECISION_HIDDEN'
>
>;
type Decision = {
decision: string;
decision: DecisionName;
timestamp: string;
};

Expand Down Expand Up @@ -58,7 +87,7 @@ type OriginalMessageClosed = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.CLOSED;
originalMessage: {
policyName: string;
reason: 'default' | 'accountClosed' | 'accountMerged' | 'removedPolicy' | 'policyDeleted';
reason: ValueOf<typeof CONST.REPORT.ARCHIVE_REASON>;
lastModified?: string;
};
};
Expand Down Expand Up @@ -124,7 +153,18 @@ type OriginalMessagePolicyTask = {
originalMessage: unknown;
};

type OriginalMessageModifiedExpense = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.MODIFIEDEXPENSE;
originalMessage: unknown;
};

type OriginalMessageReimbursementQueued = {
actionName: typeof CONST.REPORT.ACTIONS.TYPE.REIMBURSEMENTQUEUED;
originalMessage: unknown;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabioh8010 Could you comment on this typing? Why is it unknown? Do we really not know the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At that time we didn't know the type (same as other types previously declared here), that's why we typed as unknown.

};

type OriginalMessage =
| OriginalMessageApproved
| OriginalMessageIOU
| OriginalMessageAddComment
| OriginalMessageClosed
Expand All @@ -133,7 +173,9 @@ type OriginalMessage =
| OriginalMessageChronosOOOList
| OriginalMessageReportPreview
| OriginalMessagePolicyChangeLog
| OriginalMessagePolicyTask;
| OriginalMessagePolicyTask
| OriginalMessageModifiedExpense
| OriginalMessageReimbursementQueued;

export default OriginalMessage;
export type {Reaction, ChronosOOOEvent};
export type {ChronosOOOEvent, Decision, Reaction, ActionName};
1 change: 1 addition & 0 deletions src/types/onyx/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Report = {
/** The report type */
type?: string;

lastMessageTranslationKey?: string;
parentReportID?: string;
parentReportActionID?: string;
isOptimisticReport?: boolean;
Expand Down
28 changes: 25 additions & 3 deletions src/types/onyx/ReportAction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import OriginalMessage, {Reaction} from './OriginalMessage';
import * as OnyxCommon from './OnyxCommon';
import OriginalMessage, {Decision, Reaction} from './OriginalMessage';

type Message = {
/** The type of the action item fragment. Used to render a corresponding component */
type: string;

/** The html content of the fragment. */
html: string;

/** The text content of the fragment. */
text: string;

Expand Down Expand Up @@ -34,6 +37,9 @@ type Message = {
isDeletedParentAction: boolean;
whisperedTo: number[];
reactions: Reaction[];

moderationDecision?: Decision;
translationKey?: string;
};

type Person = {
Expand All @@ -44,7 +50,10 @@ type Person = {

type ReportActionBase = {
/** The ID of the reportAction. It is the string representation of the a 64-bit integer. */
reportActionID?: string;
reportActionID: string;

/** @deprecated Used in old report actions before migration. Replaced by reportActionID. */
sequenceNumber?: number;

fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
/** The ID of the previous reportAction on the report. It is a string represenation of a 64-bit integer (or null for CREATED actions). */
previousReportActionID?: string;
Expand All @@ -55,7 +64,7 @@ type ReportActionBase = {
person?: Person[];

/** ISO-formatted datetime */
created?: string;
created: string;

/** report action message */
message?: Message[];
Expand All @@ -79,10 +88,23 @@ type ReportActionBase = {
childCommenterCount?: number;
childLastVisibleActionCreated?: string;
childVisibleActionCount?: number;
childMoneyRequestCount?: number;

/** ISO-formatted datetime */
lastModified?: string;

pendingAction?: OnyxCommon.PendingAction;
delegateAccountID?: string;

/** Server side errors keyed by microtime */
errors?: OnyxCommon.Errors;

isAttachment?: boolean;
};

type ReportAction = ReportActionBase & OriginalMessage;

type ReportActions = Record<string, ReportAction>;

export default ReportAction;
export type {ReportActions, Message};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Policy from './Policy';
import PolicyCategory from './PolicyCategory';
import Report from './Report';
import ReportMetadata from './ReportMetadata';
import ReportAction from './ReportAction';
import ReportAction, {ReportActions} from './ReportAction';
import ReportActionReactions from './ReportActionReactions';
import SecurityGroup from './SecurityGroup';
import Transaction from './Transaction';
Expand Down Expand Up @@ -87,6 +87,7 @@ export type {
Report,
ReportMetadata,
ReportAction,
ReportActions,
ReportActionReactions,
SecurityGroup,
Transaction,
Expand Down
Loading