Skip to content
Merged
Changes from all 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
5 changes: 4 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
}

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 92 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -101,7 +101,7 @@
});

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 104 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -110,13 +110,13 @@
});

let deprecatedIsNetworkOffline = false;
Onyx.connect({

Check warning on line 113 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NETWORK,
callback: (val) => (deprecatedIsNetworkOffline = val?.isOffline ?? false),
});

let deprecatedCurrentUserAccountID: number | undefined;
Onyx.connect({

Check warning on line 119 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, value is undefined
Expand Down Expand Up @@ -1174,7 +1174,10 @@

if (actionName === CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION) {
const unreportedTransactionOriginalMessage = getOriginalMessage(reportAction as OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION>>) ?? {};
const {fromReportID} = unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;
const {fromReportID, reasoning} = unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;
if (reasoning) {
Comment on lines +1177 to +1178
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just curious, would it be better to use hasReasoning here?

Suggested change
const {fromReportID, reasoning} = unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;
if (reasoning) {
const {fromReportID} = unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;
if (hasReasoning(reportAction)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, didn't know that existed. It basically does the same thing, checking whether reasoning exists or not.
The only difference is, hasReasoning accepts any type of action as the param, so it needs to check first whether the originalMessage contains reasoning or not.

function hasReasoning(action: OnyxInputOrEntry<ReportAction>): boolean {
const originalMessage = getOriginalMessage(action);
return !!originalMessage && typeof originalMessage === 'object' && 'reasoning' in originalMessage && !!originalMessage.reasoning;
}

But in our case, we already checked the action name before, even though we typecast the original message unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;, which is honestly not the best way.

if (actionName === CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION) {
const unreportedTransactionOriginalMessage = getOriginalMessage(reportAction as OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION>>) ?? {};
const {fromReportID} = unreportedTransactionOriginalMessage as OriginalMessageUnreportedTransaction;
const fromReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${fromReportID}`];
return !!fromReport;

If we want to remove the typecast, we need to create a new function to narrow down the type

function isUnreportedTransactionAction(reportAction: OnyxInputOrEntry<ReportAction>): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION> {
    return isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.UNREPORTED_TRANSACTION);
}

...

if (isUnreportedTransactionAction(reportAction)) {
    const unreportedTransactionOriginalMessage = getOriginalMessage(reportAction);
    const fromReportID = unreportedTransactionOriginalMessage?.fromReportID;
    const reasoning = unreportedTransactionOriginalMessage?.reasoning;
    if (reasoning) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

aah i like your reasoning, thanks - let's keep it as is!

return !!fromReportID;
}
const fromReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${fromReportID}`];
return !!fromReport;
}
Expand Down
Loading