-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Decompose scan pr1 selectors getters #87083
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
Open
rinej
wants to merge
3
commits into
Expensify:main
Choose a base branch
from
callstack-internal:decompose-scan-pr1-selectors-getters
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−43
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import {activePolicySelector} from '@selectors/Policy'; | ||
| import type {OnyxEntry} from 'react-native-onyx'; | ||
| import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||
| import {useSession} from '@components/OnyxListItemProvider'; | ||
| import {canSubmitPerDiemExpenseFromWorkspace, isPaidGroupPolicy, isPolicyMemberWithoutPendingDelete, isTimeTrackingEnabled} from '@libs/PolicyUtils'; | ||
| import CONST from '@src/CONST'; | ||
|
|
@@ -31,48 +31,85 @@ function isPolicyValidForMovingExpenses(policy: OnyxEntry<Policy>, login: string | |
| ); | ||
| } | ||
|
|
||
| function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, isTimeRequest?: boolean, expensePolicyID?: string) { | ||
| const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); | ||
| const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); | ||
| const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { | ||
| selector: activePolicySelector, | ||
| }); | ||
| type PolicyQualificationResult = { | ||
| singlePolicyID: string | undefined; | ||
| isMemberOfMoreThanOnePolicy: boolean; | ||
| validExpensePolicyID: string | undefined; | ||
| }; | ||
|
|
||
| const session = useSession(); | ||
| const login = session?.email ?? ''; | ||
| /** | ||
| * Selector that computes which policies qualify for moving expenses. | ||
| * Returns only IDs and flags — stable output that prevents re-renders when unrelated policies change. | ||
| */ | ||
| function getPolicyQualificationResult( | ||
| policies: OnyxCollection<Policy>, | ||
| login: string, | ||
| isPerDiemRequest?: boolean, | ||
| isTimeRequest?: boolean, | ||
| expensePolicyID?: string, | ||
| ): PolicyQualificationResult { | ||
| if (!policies) { | ||
| return {singlePolicyID: undefined, isMemberOfMoreThanOnePolicy: false, validExpensePolicyID: undefined}; | ||
| } | ||
|
|
||
| // Early exit optimization: only need to check if we have 0, 1, or >1 policies | ||
| let singleUserPolicy; | ||
| let singlePolicyID: string | undefined; | ||
| let isMemberOfMoreThanOnePolicy = false; | ||
| for (const policy of Object.values(allPolicies ?? {})) { | ||
| for (const policy of Object.values(policies)) { | ||
| if (!isPolicyValidForMovingExpenses(policy, login, isPerDiemRequest, isTimeRequest)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (!singleUserPolicy) { | ||
| singleUserPolicy = policy; | ||
| if (!singlePolicyID) { | ||
| singlePolicyID = policy?.id; | ||
| } else { | ||
| isMemberOfMoreThanOnePolicy = true; | ||
| break; // Found 2, no need to continue | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // If an expense policy ID is provided and valid, prefer it over the active policy | ||
| // This ensures that when viewing/editing an expense from workspace B, we show workspace B | ||
| // even if the user's default workspace is A | ||
| let validExpensePolicyID: string | undefined; | ||
| if (expensePolicyID) { | ||
| const expensePolicy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${expensePolicyID}`]; | ||
| const expensePolicy = policies[`${ONYXKEYS.COLLECTION.POLICY}${expensePolicyID}`]; | ||
| if (expensePolicy && isPolicyValidForMovingExpenses(expensePolicy, login, isPerDiemRequest, isTimeRequest)) { | ||
| return {policyForMovingExpensesID: expensePolicyID, policyForMovingExpenses: expensePolicy, shouldSelectPolicy: false}; | ||
| validExpensePolicyID = expensePolicyID; | ||
| } | ||
| } | ||
|
|
||
| return {singlePolicyID, isMemberOfMoreThanOnePolicy, validExpensePolicyID}; | ||
| } | ||
|
|
||
| function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, isTimeRequest?: boolean, expensePolicyID?: string) { | ||
| const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); | ||
| const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { | ||
| selector: activePolicySelector, | ||
| }); | ||
|
|
||
| const session = useSession(); | ||
| const login = session?.email ?? ''; | ||
|
|
||
| // Contextual selector — captures login/flags from closure. | ||
| // Returns only IDs + flags (stable output) to prevent re-renders when unrelated policies change. | ||
| const policyQualificationSelector = (policies: OnyxCollection<Policy>) => getPolicyQualificationResult(policies, login, isPerDiemRequest, isTimeRequest, expensePolicyID); | ||
| const [qualificationResult] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { | ||
| selector: policyQualificationSelector, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
| }); | ||
|
|
||
| const {singlePolicyID, isMemberOfMoreThanOnePolicy, validExpensePolicyID} = qualificationResult ?? {}; | ||
|
|
||
| // Per-key lookup for the resolved policy (only fires when that specific policy changes) | ||
| const resolvedPolicyID = validExpensePolicyID ?? singlePolicyID; | ||
| const [resolvedPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${resolvedPolicyID}`); | ||
|
|
||
| // If an expense policy ID is provided and valid, prefer it over the active policy | ||
| if (validExpensePolicyID) { | ||
| return {policyForMovingExpensesID: validExpensePolicyID, policyForMovingExpenses: resolvedPolicy, shouldSelectPolicy: false}; | ||
| } | ||
|
|
||
| if (activePolicy && (!isPerDiemRequest || canSubmitPerDiemExpenseFromWorkspace(activePolicy)) && (!isTimeRequest || isTimeTrackingEnabled(activePolicy))) { | ||
| return {policyForMovingExpensesID: activePolicyID, policyForMovingExpenses: activePolicy, shouldSelectPolicy: false}; | ||
| } | ||
|
|
||
| if (singleUserPolicy && !isMemberOfMoreThanOnePolicy) { | ||
| return {policyForMovingExpensesID: singleUserPolicy.id, policyForMovingExpenses: singleUserPolicy, shouldSelectPolicy: false}; | ||
| if (singlePolicyID && !isMemberOfMoreThanOnePolicy) { | ||
| return {policyForMovingExpensesID: singlePolicyID, policyForMovingExpenses: resolvedPolicy, shouldSelectPolicy: false}; | ||
| } | ||
|
|
||
| if (isMemberOfMoreThanOnePolicy) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we return the policy here instead of the ID, similar to what we did with
useReportAttributesByID?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Policy objects are large, so returning the full object from a collection selector would require Onyx to deep compare it on every collection change, it might defeat the performance a bit
useReportAttributesByIDworks differently because it selects from a derived value where each entry is a small object, making deep comparison cheap. Here we're selecting from the full COLLECTION.POLICY, so returning only the stable ID and doing a per-key lookup is more efficientwhat do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. I've been thinking how it can be cheap if it's a
reportobject. I forgot that it's a report attributes 🤦. All good!