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

Adds new ReportUtils for Violations #31448

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
154 commits
Select commit Hold shift + click to select a range
a917e06
adds violations functions to report utils and implements
cdanwards Nov 16, 2023
3756ef9
Small fix in ReportUtils
cdanwards Nov 16, 2023
3cc2e0f
Fix Onyx Connection for reportActions
cdanwards Nov 16, 2023
4db7207
Fixed merge conflicts
cdanwards Nov 16, 2023
35fd77e
Fixed more merge conflicts
cdanwards Nov 16, 2023
7942b39
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Nov 20, 2023
89997ce
Merge branch 'main' of github.com:Expensify/App into cdanwards/violat…
cdanwards Nov 20, 2023
a98b3a1
Fix Permissions import
cdanwards Nov 21, 2023
f7dc731
Correctly pass down betas
cdanwards Nov 22, 2023
93ea5b7
Update tests
cdanwards Nov 22, 2023
1cfeec0
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Nov 22, 2023
1f9d645
Fixing some beta props
cdanwards Nov 27, 2023
2a03cc9
Fixed merge conflicts
cdanwards Nov 27, 2023
813c856
Fixed merge conflicts
cdanwards Nov 27, 2023
6d47be5
Started TS conversion
cdanwards Nov 27, 2023
6a16af0
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Nov 28, 2023
5849803
Fixed up the Onyx Types for Violations & I think I got the functions …
cdanwards Nov 29, 2023
f7fa4bf
Finally got the tests up and working correctly
cdanwards Nov 29, 2023
9ce9595
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Nov 29, 2023
02fa714
Fixed types and property checks for violations
cdanwards Nov 29, 2023
63ab544
Added some doc comments and also fixed the violation type
cdanwards Nov 30, 2023
bef0d73
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Nov 30, 2023
c6dca6a
Fixed merge conflicts
cdanwards Nov 30, 2023
c69c1df
Fixed perf test
cdanwards Nov 30, 2023
5e122ef
Removed unnecessary export
cdanwards Nov 30, 2023
e53902e
Restore betas in test
cdanwards Nov 30, 2023
c49b03f
Switch to destructuring
cdanwards Nov 30, 2023
375eabb
Update src/libs/ReportUtils.ts
cdanwards Dec 5, 2023
b6c24db
Update src/libs/ReportUtils.ts
cdanwards Dec 5, 2023
8bee286
Fixed merge conflicts
cdanwards Dec 5, 2023
e1ca1ac
Merge branch 'cdanwards/violations/transaction-thread-violations' of …
cdanwards Dec 5, 2023
ccd3eb3
remove unneeded changes
cdanwards Dec 5, 2023
1d2cfa2
Remove duplicate TRANSACTION_VIOLATIONS constant
cdanwards Dec 5, 2023
bcd1ee8
Update src/ONYXKEYS.ts
cdanwards Dec 6, 2023
cae1e0d
Working on removing the onyx connect and using withOnyx
cdanwards Dec 7, 2023
59ba57e
Merge branch 'cdanwards/violations/transaction-thread-violations' of …
cdanwards Dec 11, 2023
90b7138
Starting so swap in the transactionViolations
cdanwards Dec 11, 2023
35de23f
Fixed tests and created new hook mock
cdanwards Dec 12, 2023
bbfd1fa
Switched to just passing down parentReportAction
cdanwards Dec 12, 2023
20a0fc3
Fixed Merge COnflicts
cdanwards Dec 12, 2023
2c52edf
Move the report actions selector to the SidebarUtils file
cdanwards Dec 12, 2023
73971f0
Correct type import
cdanwards Dec 13, 2023
1234b88
Another type correction
cdanwards Dec 13, 2023
9223374
Final fix for the test of the report actions
cdanwards Dec 13, 2023
9e1753b
Merge branch 'main' of github.com:infinitered/ExpensifyApp into cdanw…
cdanwards Dec 13, 2023
4366d64
Update Comments
cdanwards Dec 13, 2023
5716800
A few fixes
cdanwards Dec 13, 2023
adf1933
Fixed some prop types and also added a beta guard
cdanwards Dec 13, 2023
da8f376
Added usePermissions mock to ReportPreview
cdanwards Dec 13, 2023
cda1aee
Trying this out
cdanwards Dec 13, 2023
cd6586a
Trying this out
cdanwards Dec 13, 2023
03f9d96
Merge branch 'cdanwards/violations/transaction-thread-violations' of …
cdanwards Dec 13, 2023
6478b94
fix for perf test
cdanwards Dec 13, 2023
1736c21
Remove unused type
cdanwards Dec 13, 2023
16a0a9a
Fixing some issues with tests
cdanwards Dec 13, 2023
feb3596
Was accidentally importing named instead of default
cdanwards Dec 13, 2023
c7aa6c4
Fix merge conflicts
cdanwards Dec 13, 2023
69b767b
Update src/libs/ReportUtils.ts
cdanwards Dec 14, 2023
9535a2b
Update src/components/ReportActionItem/ReportPreview.js
cdanwards Dec 14, 2023
99d3c0d
Update src/hooks/__mocks__/usePermissions.ts
cdanwards Dec 14, 2023
0720910
Remove unused import
cdanwards Dec 14, 2023
7faf50b
Fix transactionHasViolation function and
cdanwards Dec 14, 2023
bf401af
Remove unnecessary code in shouldHideReport
cdanwards Dec 14, 2023
f0a360b
Remove unnecessary comment in ReportUtils.ts
cdanwards Dec 14, 2023
4088e8f
Fix betas check to be outside of the function
cdanwards Dec 14, 2023
766d37e
Add comment about using usePermissions hook mock for beta tests
cdanwards Dec 14, 2023
561a4d4
Add default value for transactionViolations
cdanwards Dec 14, 2023
abad008
Make linter happy
cdanwards Dec 14, 2023
563ca7b
Refactor getOrderedReportIDs function to improve
cdanwards Dec 14, 2023
32fb912
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Dec 14, 2023
2e5c39b
feat(Violations): implement types from money-request-preview branch
trevor-coleman Dec 14, 2023
30a77c9
feat(Violations): replace transactionViolationsProp with imported pro…
trevor-coleman Dec 14, 2023
2e3d3e7
feat(Violations): make reportActions mandatory and remove parentRepor…
trevor-coleman Dec 14, 2023
9503748
feat(Violations): remove whitespace
trevor-coleman Dec 14, 2023
2871cd8
feat(Violations): simplify
trevor-coleman Dec 14, 2023
5dd7d01
feat(Violations): update comment
trevor-coleman Dec 14, 2023
d169a43
feat(Violations): simplify
trevor-coleman Dec 14, 2023
aa7822e
feat(Violations): fix type of transactionViolations propType
trevor-coleman Dec 14, 2023
d86616d
feat(Violations): revert simplifications
trevor-coleman Dec 15, 2023
ccb12cc
feat(Violations): add second withOnyx
trevor-coleman Dec 15, 2023
e3d4ae8
feat(Violations): get transactions directly
trevor-coleman Dec 15, 2023
37e0f2e
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Dec 19, 2023
d467853
feat(Violations): fix propsTypes
trevor-coleman Dec 19, 2023
fdab084
feat(Violations): rename
trevor-coleman Dec 19, 2023
bf37bb0
feat(Violations): rename
trevor-coleman Dec 19, 2023
db5fa36
feat(Violations): move ReportUtils.transactionHasViolation to Transac…
trevor-coleman Dec 19, 2023
ba5e940
feat(Violations): restore selector
trevor-coleman Dec 19, 2023
e873fa4
feat(Violations): remove null guard
trevor-coleman Dec 19, 2023
68f933a
feat(Violations): revert line width changes
trevor-coleman Dec 19, 2023
b41a3de
feat(Violations): change signature of shouldReportBeInOptionList
trevor-coleman Dec 19, 2023
2492bf9
feat(Violations): rename resolvedParentReportAction
trevor-coleman Dec 19, 2023
90101f2
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Dec 19, 2023
ea6bf2e
feat(Violations): add whitespace
trevor-coleman Dec 21, 2023
4d4c476
feat(Violations): use imported propType
trevor-coleman Dec 21, 2023
ec2429a
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Dec 21, 2023
04ed595
feat(Violations): revert commment wrapping
trevor-coleman Dec 22, 2023
aa14258
feat(Violations): cast return value to type
trevor-coleman Dec 22, 2023
0214a12
feat(Violations): use correct key
trevor-coleman Dec 22, 2023
a89d888
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 2, 2024
cd4e987
feat(Violations): fix comment wrapping
trevor-coleman Jan 2, 2024
93d08a0
feat(Violations): fix proptypes of allReportActions
trevor-coleman Jan 3, 2024
c24db94
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 3, 2024
b3b367d
feat(Violations): revert change to reportActionsSelector
trevor-coleman Jan 3, 2024
00ce2cf
feat(Violations): restore proptypes
trevor-coleman Jan 3, 2024
a5ecebe
feat(Violations): unwrap comment
trevor-coleman Jan 3, 2024
2f4a14a
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 4, 2024
93a300b
fix types broken by merge
trevor-coleman Jan 4, 2024
289d00f
feat(Violations): remove comment
trevor-coleman Jan 4, 2024
0bba695
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 4, 2024
c2667a8
feat(Violations): remove duplicate violations
trevor-coleman Jan 4, 2024
d1a6ee1
feat(Violations): fix import paths
trevor-coleman Jan 4, 2024
6cabad0
feat(Violations): use reportActions
trevor-coleman Jan 4, 2024
ea487ba
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 5, 2024
c8ebe6f
feat(Violations): remove transaction violations
trevor-coleman Jan 5, 2024
ec7dc4f
feat(Violations): remove transaction violations
trevor-coleman Jan 5, 2024
aacb894
feat(Violations): lint
trevor-coleman Jan 5, 2024
9e57641
feat(Violations): create params object for getOptionData
trevor-coleman Jan 8, 2024
79ac93a
feat(Violations): create params object for shouldReportBeInOptionsList
trevor-coleman Jan 8, 2024
13fe960
feat(Violations): invert control and pass doesTransactionThreadHaveVi…
trevor-coleman Jan 8, 2024
4a26a24
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 8, 2024
66c181e
feat(Violations): fix TransactionViolations export
trevor-coleman Jan 8, 2024
2f75e74
feat(Violations): take parentReportAction as param an update callers
trevor-coleman Jan 8, 2024
e110f14
feat(Violations): fix import
trevor-coleman Jan 8, 2024
00a8bec
feat(Violations): pass proper parentReportAction in to doesTransactio…
trevor-coleman Jan 8, 2024
0512719
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 8, 2024
6968650
feat(Violations): remove unneeded imports and lint
trevor-coleman Jan 8, 2024
ad15ca6
fix type
trevor-coleman Jan 8, 2024
85fa56d
feat(Violations): remove unneeded import
trevor-coleman Jan 8, 2024
2452d31
fix filtering
trevor-coleman Jan 9, 2024
44a96ca
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 9, 2024
192899c
feat(Violations): remove optional param
trevor-coleman Jan 9, 2024
0c41aec
feat(Violations): remove check for undefined report
trevor-coleman Jan 9, 2024
891c3d2
feat(Violations): PR review comments
trevor-coleman Jan 9, 2024
09d08e2
feat(Violations): rename param
trevor-coleman Jan 10, 2024
ae525eb
feat(Violations): use !! instead of Boolean to allow type narrowing
trevor-coleman Jan 10, 2024
4547321
feat(Violations): make all params required
trevor-coleman Jan 10, 2024
3b66eeb
feat(Violations): fix comment
trevor-coleman Jan 10, 2024
da7bf97
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 10, 2024
a5833d0
feat(Violations): use shorthand
trevor-coleman Jan 10, 2024
44ef928
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 11, 2024
587a44c
feat(Violations): use lodash
trevor-coleman Jan 12, 2024
c732ddd
feat(Violations): ensure reportActionCount defaults to 1
trevor-coleman Jan 12, 2024
3eaf3d4
feat(Violations): refactor for readability
trevor-coleman Jan 12, 2024
27e8315
feat(Violations): restore guard
trevor-coleman Jan 12, 2024
96b9ff7
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 12, 2024
de97f71
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 15, 2024
8e0250c
feat(Violations): change STATE_NUM to reflect changes from 54e7f76d9f…
trevor-coleman Jan 15, 2024
00fefc1
prettier
trevor-coleman Jan 15, 2024
9498292
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 15, 2024
e3882e7
Update src/libs/ReportUtils.ts
trevor-coleman Jan 15, 2024
64ab957
feat(Violations): clarify import from TransactionUtils
trevor-coleman Jan 16, 2024
ae91d30
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 18, 2024
7305a48
feat(Violations): fix const/let
trevor-coleman Jan 18, 2024
6e8ebca
Merge remote-tracking branch 'upstream/main' into cdanwards/violation…
trevor-coleman Jan 18, 2024
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
8 changes: 7 additions & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3069,7 +3069,8 @@ const CONST = {
},

/**
* Constants for maxToRenderPerBatch parameter that is used for FlatList or SectionList. This controls the amount of items rendered per batch, which is the next chunk of items rendered on every scroll.
* Constants for maxToRenderPerBatch parameter that is used for FlatList or SectionList. This controls the amount of items rendered per batch, which is the next chunk of items
* rendered on every scroll.
Comment on lines +3072 to +3073

This comment was marked as resolved.

This comment was marked as outdated.

*/
MAX_TO_RENDER_PER_BATCH: {
DEFAULT: 5,
Expand All @@ -3081,6 +3082,11 @@ const CONST = {
RBR: 'RBR',
},

/**
* Constants for types of violations.
* Defined here because they need to be referenced by the type system to generate the
* ViolationNames type.
*/
VIOLATIONS: {
ALL_TAG_LEVELS_REQUIRED: 'allTagLevelsRequired',
AUTO_REPORTED_REJECTED_EXPENSE: 'autoReportedRejectedExpense',
Expand Down
1 change: 1 addition & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.TRANSACTION_DRAFT]: OnyxTypes.Transaction;
[ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags;
[ONYXKEYS.COLLECTION.SELECTED_TAB]: string;
[ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS]: OnyxTypes.TransactionViolations;
[ONYXKEYS.COLLECTION.PRIVATE_NOTES_DRAFT]: string;
[ONYXKEYS.COLLECTION.NEXT_STEP]: OnyxTypes.ReportNextStep;

Expand Down
31 changes: 30 additions & 1 deletion src/components/LHNOptionsList/LHNOptionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import _ from 'underscore';
import participantPropTypes from '@components/participantPropTypes';
import transactionPropTypes from '@components/transactionPropTypes';
import withCurrentReportID, {withCurrentReportIDDefaultProps, withCurrentReportIDPropTypes} from '@components/withCurrentReportID';
import usePermissions from '@hooks/usePermissions';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
import {transactionViolationsPropType} from '@libs/Violations/propTypes';
import reportActionPropTypes from '@pages/home/report/reportActionPropTypes';
import reportPropTypes from '@pages/reportPropTypes';
import stylePropTypes from '@styles/stylePropTypes';
Expand Down Expand Up @@ -63,8 +65,13 @@ const propTypes = {

/** The transaction from the parent report action */
transactions: PropTypes.objectOf(transactionPropTypes),

/** List of draft comments */
draftComments: PropTypes.objectOf(PropTypes.string),

/** The list of transaction violations */
transactionViolations: transactionViolationsPropType,

...withCurrentReportIDPropTypes,
Copy link
Contributor

Choose a reason for hiding this comment

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

As new lines are added above, let's add here too

Suggested change
...withCurrentReportIDPropTypes,
...withCurrentReportIDPropTypes,

Copy link
Contributor

Choose a reason for hiding this comment

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

done

};

Expand All @@ -78,6 +85,7 @@ const defaultProps = {
personalDetails: {},
transactions: {},
draftComments: {},
transactionViolations: {},
...withCurrentReportIDDefaultProps,
};

Expand All @@ -98,8 +106,10 @@ function LHNOptionsList({
transactions,
draftComments,
currentReportID,
transactionViolations,
}) {
const styles = useThemeStyles();
const {canUseViolations} = usePermissions();
/**
* Function which renders a row in the list
*
Expand Down Expand Up @@ -137,10 +147,26 @@ function LHNOptionsList({
onSelectRow={onSelectRow}
preferredLocale={preferredLocale}
comment={itemComment}
transactionViolations={transactionViolations}
canUseViolations={canUseViolations}
/>
);
},
[currentReportID, draftComments, onSelectRow, optionMode, personalDetails, policy, preferredLocale, reportActions, reports, shouldDisableFocusOptions, transactions],
[
currentReportID,
draftComments,
onSelectRow,
optionMode,
personalDetails,
policy,
preferredLocale,
reportActions,
reports,
shouldDisableFocusOptions,
transactions,
transactionViolations,
canUseViolations,
],
);

return (
Expand Down Expand Up @@ -189,5 +215,8 @@ export default compose(
draftComments: {
key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT,
},
transactionViolations: {
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, is there a reason we're taking all the transaction violations, and not setting this to a function that takes a transactionID and returns the transaction violations for a specific transaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are we able to determine the transactionId within the withOnyx call? I'm not aware of how we would narrow that down otherwise without doing the same computation we do later on after it's passed down to transactionThreadHasViolations

Copy link
Contributor

Choose a reason for hiding this comment

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

@cead22 -- this I'll have to look into a bit more as this is my first contact with withOnyx

Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work?

export default compose(
    withCurrentReportID,
    withOnyx({
        // ..
        transactionViolations: {
            key: ({currentReportId}) => `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${currentReportId}`,
        },
    }),
)(LHNOptionsList);

Copy link
Contributor

@situchan situchan Jan 4, 2024

Choose a reason for hiding this comment

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

why need to filter out with currentReportId here? This is OptionsList, not OptionRow
So I meant that current code is fine

},
}),
)(LHNOptionsList);
21 changes: 19 additions & 2 deletions src/components/LHNOptionsList/OptionRowLHNData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import _ from 'underscore';
import participantPropTypes from '@components/participantPropTypes';
import transactionPropTypes from '@components/transactionPropTypes';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import SidebarUtils from '@libs/SidebarUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import {transactionViolationsPropType} from '@libs/Violations/propTypes';
import reportActionPropTypes from '@pages/home/report/reportActionPropTypes';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -42,6 +44,9 @@ const propTypes = {
/** The transaction from the parent report action */
transaction: transactionPropTypes,

/** Any violations associated with the transaction */
transactionViolations: transactionViolationsPropType,

...basePropTypes,
};

Expand Down Expand Up @@ -73,6 +78,8 @@ function OptionRowLHNData({
receiptTransactions,
parentReportAction,
transaction,
transactionViolations,
canUseViolations,
...propsToForward
}) {
const reportID = propsToForward.reportID;
Expand All @@ -85,9 +92,19 @@ function OptionRowLHNData({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fullReport.reportID, receiptTransactions, reportActions]);

const hasViolations = canUseViolations && ReportUtils.doesTransactionThreadHaveViolations(fullReport, transactionViolations, parentReportAction);

const optionItem = useMemo(() => {
// Note: ideally we'd have this as a dependent selector in onyx!
const item = SidebarUtils.getOptionData(fullReport, reportActions, personalDetails, preferredLocale, policy, parentReportAction);
const item = SidebarUtils.getOptionData({
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we go back to individual params instead of a single object param? Or can you share why this change was made?

Copy link
Contributor

Choose a reason for hiding this comment

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

There are a number of benefits to using an object for params over a long list of parameters.

For one, it means that the order of the parameters no longer matters, so if you need to add or remove a parameter in the future, it becomes a lot faster an easier, and when calling the function, you don't need to remember the order of the parameters.

Also it means that every parameter is labeled with what it is. Which becomes important when there are long lists that include, for instance, boolean flags.

To understandgetNextAvailableDate(newDate(), true, true, false) you need to know what those things mean, and it's also easy to transpose or forget one of the items.

But with:

getNextAvailableDate({
  startDate: newDate('2024-09-01'),
  useWeekends: true,
  allowDoubleBooking: false,
  cancelConflictingMeetings: false
  })

It's very clear what each of those values mean.

It also lets default values for boolean flags be omitted because you can define them as an optional boolean if they are rarely used. So the call can be reduced to:

getNextAvailableDate({
  startDate: newDate('2024-09-01'),
  useWeekends: true
})

In typescript you will get type checking on properties of objects so if something is not optional, then the compuiler will raise a type error if you omit a property from the object, the same as you would if you omitted a parameter.

On the other end, destructuring lets us access the params exactly as if they were written as separate params, and it maes things like default values a little clearer:

function getNextAvailableDate({
  startDate = newDate(), 
  useWeekends, 
  cancelConflictingMeetings, 
  allowDoubleBooking
  }:GetNextAvailableDateParams) {  
       // ...
  }

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair, but the main reason I'm pushing back on this is because I'm trying to get all these PRs focused on the functionality and not add scope by refactoring. That said, I won't block on this, as I want to get this merged (which is another reason to limit the scope of the PR)

For one, it means that the order of the parameters no longer matters

Doesn't TS have named params?

For one, it means that the order of the parameters no longer matters

Do we encourage this kinda magic? I think I would prefer always having those values passed, or set with explicit destructuring, than magically have them set to false or to undefined which is falsy and have the code rely on that

Copy link
Contributor

@trevor-coleman trevor-coleman Jan 10, 2024

Choose a reason for hiding this comment

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

I'm trying to get all these PRs focused on the functionality and not add scope by refactoring

This wasn't refactoring for the sake of refactoring, I had to do this to be able to work with these functions. Having to chase the flow of data through the various levels of callers was made far more annoying by dealing with long lists of params. The next person is going to have the same problem.

Doesn't TS have named params?

This is how we do that in typescript.

Do we encourage this kinda magic?

There's no magic there -- this is bog standard JS/TS. But i'll make all the params required.

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel you, cause I use VIM. I assumed knowing the positions and names of parameters wasn't a problem for most people because they're editors make this easy. I actually downloaded visual studio code recently to make working in App a bit easier for myself

Copy link
Contributor

@trevor-coleman trevor-coleman Jan 10, 2024

Choose a reason for hiding this comment

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

I use webstorm which has parameter labels, but I still find it much easier to work with functions when the order of the params doesn't matter.

For an extreme case say you have a function with 10 boolean params and you delete the seventh one. The labels are all wrong now, but there won't be any type errors or issues. And you need to go to each and count to the seventh param, and then make sure the seventh one is the right one to delete. But if you make a mistake counting at any point you've deleted the wrong thing and might not even notice until later.

The labels wont help here because you've already deleted the param from the function, so there is no label for it anymore.

When the params are in an object you can just follow the red squiggles and delete the one you know is the right one.

Even passing in named variables can be wrong or worse misleading, because if the types are compatible, the compiler will accept it, but with an object using shorthand properties, you know that the variable with that name is being passed in the right place and will be consumed correctly by the called function.

With something like VSCode lables you need to do the work of matching each label to each variable name, and the compiler won't help you out.

report: fullReport,
reportActions,
personalDetails,
preferredLocale,
policy,
parentReportAction,
hasViolations,
});
if (deepEqual(item, optionItemRef.current)) {
return optionItemRef.current;
}
Expand All @@ -96,7 +113,7 @@ function OptionRowLHNData({
// Listen parentReportAction to update title of thread report when parentReportAction changed
// Listen to transaction to update title of transaction report when transaction changed
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fullReport, linkedTransaction, reportActions, personalDetails, preferredLocale, policy, parentReportAction, transaction]);
}, [fullReport, linkedTransaction, reportActions, personalDetails, preferredLocale, policy, parentReportAction, transaction, transactionViolations, canUseViolations]);

useEffect(() => {
if (!optionItem || optionItem.hasDraftComment || !comment || comment.length <= 0 || isFocused) {
Expand Down
14 changes: 13 additions & 1 deletion src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Text from '@components/Text';
import transactionPropTypes from '@components/transactionPropTypes';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import useLocalize from '@hooks/useLocalize';
import usePermissions from '@hooks/usePermissions';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
Expand All @@ -27,6 +28,7 @@ import * as ReceiptUtils from '@libs/ReceiptUtils';
import * as ReportActionUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import {transactionViolationsPropType} from '@libs/Violations/propTypes';
import reportActionPropTypes from '@pages/home/report/reportActionPropTypes';
import reportPropTypes from '@pages/reportPropTypes';
import * as IOU from '@userActions/IOU';
Expand Down Expand Up @@ -108,6 +110,9 @@ const propTypes = {
/** All the transactions, used to update ReportPreview label and status */
transactions: PropTypes.objectOf(transactionPropTypes),

/** All of the transaction violations */
transactionViolations: transactionViolationsPropType,

...withLocalizePropTypes,
};

Expand All @@ -121,6 +126,9 @@ const defaultProps = {
accountID: null,
},
isWhisper: false,
transactionViolations: {
violations: [],
},
policy: {
isHarvestingEnabled: false,
},
Expand All @@ -131,6 +139,7 @@ function ReportPreview(props) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const {canUseViolations} = usePermissions();

const {hasMissingSmartscanFields, areAllRequestsBeingSmartScanned, hasOnlyDistanceRequests, hasNonReimbursableTransactions} = useMemo(
() => ({
Expand Down Expand Up @@ -162,7 +171,7 @@ function ReportPreview(props) {
const numberOfScanningReceipts = _.filter(transactionsWithReceipts, (transaction) => TransactionUtils.isReceiptBeingScanned(transaction)).length;
const hasReceipts = transactionsWithReceipts.length > 0;
const isScanning = hasReceipts && areAllRequestsBeingSmartScanned;
const hasErrors = hasReceipts && hasMissingSmartscanFields;
const hasErrors = (hasReceipts && hasMissingSmartscanFields) || (canUseViolations && ReportUtils.hasViolations(props.iouReportID, props.transactionViolations));
const lastThreeTransactionsWithReceipts = transactionsWithReceipts.slice(-3);
const lastThreeReceipts = _.map(lastThreeTransactionsWithReceipts, (transaction) => ReceiptUtils.getThumbnailAndImageURIs(transaction));
let formattedMerchant = numberOfRequests === 1 && hasReceipts ? TransactionUtils.getMerchant(transactionsWithReceipts[0]) : null;
Expand Down Expand Up @@ -365,5 +374,8 @@ export default compose(
transactions: {
key: ONYXKEYS.COLLECTION.TRANSACTION,
},
transactionViolations: {
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
},
}),
)(ReportPreview);
2 changes: 1 addition & 1 deletion src/components/ViolationMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {useMemo} from 'react';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import ViolationsUtils from '@libs/ViolationsUtils';
import ViolationsUtils from '@libs/Violations/ViolationsUtils';
import type {TransactionViolation} from '@src/types/onyx';
import Text from './Text';

Expand Down
7 changes: 7 additions & 0 deletions src/hooks/__mocks__/usePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @returns A mock of the usePermissions hook.
*/
const usePermissions = () => ({
canUseViolations: true,
});
export default usePermissions;
18 changes: 17 additions & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ function getOptions(
recentlyUsedTags = [],
canInviteUser = true,
includeSelectedOptions = false,
transactionViolations = {},
includePolicyTaxRates,
policyTaxRates,
},
Expand Down Expand Up @@ -1357,7 +1358,22 @@ function getOptions(
const searchValue = parsedPhoneNumber.possible ? parsedPhoneNumber.number.e164 : searchInputValue.toLowerCase();

// Filter out all the reports that shouldn't be displayed
const filteredReports = _.filter(reports, (report) => ReportUtils.shouldReportBeInOptionList(report, Navigation.getTopmostReportId(), false, betas, policies));
const filteredReports = _.filter(reports, (report) => {
const {parentReportID, parentReportActionID} = report || {};
Copy link
Contributor

Choose a reason for hiding this comment

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

When would report be falsy?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure -- it happened once while I was building this but that may have been an artifact of some bad state.

Reports here is an object, so it's possible that one of the properties of that object could be null. Especially in javascript.

So it's mostly for safety, as it will cause the app to crash if you try and destructure undefined or null.

Copy link
Contributor

Choose a reason for hiding this comment

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

I get the safety argument, but is it possible for report not to be defined, since it comes from reports, and if reports is empty the function won't run?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't know if it's possible, I don't think we should add the fallback, at least not without some logging to understand why it happens.

If we know it's possible, then let's add a comment explaining when that can happen

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed the guard

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah here is where it is null: #31448 (comment)

Not sure why -- i guess this is called when creating the list for new chats.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a comment explaining why it's necessary to have the fallback

const canGetParentReport = parentReportID && parentReportActionID && allReportActions;
const parentReportAction = canGetParentReport ? lodashGet(allReportActions, [parentReportID, parentReportActionID], {}) : {};
const doesReportHaveViolations = betas.includes(CONST.BETAS.VIOLATIONS) && ReportUtils.doesTransactionThreadHaveViolations(report, transactionViolations, parentReportAction);

return ReportUtils.shouldReportBeInOptionList({
report,
currentReportId: Navigation.getTopmostReportId(),
betas,
policies,
doesReportHaveViolations,
isInGSDMode: false,
excludeEmptyChats: false,
});
});

// Sorting the reports works like this:
// - Order everything by the last message timestamp (descending)
Expand Down
Loading
Loading