refactor: isolates buildOptimisticPolicyRecentlyUsedTags from Onyx.connect data#70393
Conversation
tests/actions/Policy/BuildOptimisticPolicyRecentlyUsedTagsTest.ts
Outdated
Show resolved
Hide resolved
… to PolicyTagTest.ts
…x-connect/1-src-libs-actions-Policy-Tag.ts
src/libs/actions/Policy/Tag.ts
Outdated
| transactionTags?: string; | ||
| }; | ||
|
|
||
| function buildOptimisticPolicyRecentlyUsedTags({policyTags, policyRecentlyUsedTags, policyID, transactionTags}: BuildOptimisticPolicyRecentlyUsedTagsProps): RecentlyUsedTags { |
There was a problem hiding this comment.
I think we can get rid of policyID 👀
There was a problem hiding this comment.
You're right. Good thing you noticed.
src/libs/actions/Policy/Tag.ts
Outdated
| /** | ||
| * @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access. | ||
| * All usages of this function should be replaced with useOnyx hook in React components. | ||
| */ | ||
| function getPolicyRecentlyUsedTagsData(policyID: string | undefined) { | ||
| return allRecentlyUsedTags?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`] ?? {}; | ||
| } |
There was a problem hiding this comment.
How about creating a follow-up PR now and merging it as soon as this one gets merged?
There was a problem hiding this comment.
I'd prefer not to do this, as it falls under the responsibility of the person refactoring the src/libs/actions/IOU.ts file.
But I'll actually move the entire getPolicyRecentlyUsedTagsData implementation to src/libs/actions/IOU.ts to remove the completely invalid code from the src/libs/actions/Policy/Tag.ts file.
There was a problem hiding this comment.
I don't think you are allowed to do that. If you are adding Onyx.connect somewhere, you need to remove that usage later.
cc @tgolen
There was a problem hiding this comment.
I think that as long as it's part of the issue to remove it completely, and it's all part of trying to make smaller incremental steps to not break a bunch of things with a big refactor, then what @dariusz-biela is doing here is probably OK. Just as long as whoever is doing IOU.ts knows about it and @dariusz-biela is still going to do the work to remove the Onyx.connect() in the end.
There was a problem hiding this comment.
@tgolen, I've commented this code:
// TODO: remove `allRecentlyUsedTags` from this file
// `allRecentlyUsedTags` was moved here temporarily from `src/libs/actions/Policy/Tag.ts` during the `Deprecate Onyx.connect` refactor.
// All uses of this variable should be replaced with `useOnyx`.
let allRecentlyUsedTags: OnyxCollection<RecentlyUsedTags> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
waitForCollectionCallback: true,
callback: (val) => (allRecentlyUsedTags = val),
});Lately, I've had less time due to being involved in another project as part of Expensify. But I still plan to work on the src/libs/actions/Policy/Tag.ts and src/libs/actions/Transaction.ts files as part of the refactor, removing Onyx.connect.
I've found that it's most efficient to decouple one function at a time from Onyx.connect, regardless of how many Onyx.connects it has attached.
This allows me to focus on a single functionality in PRs and unit tests.
@tgolen, it might be worth adding an additional subissue to src/libs/actions/IOU.ts to remove ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS after merging this PR.
There was a problem hiding this comment.
I've found that it's most efficient to decouple one function at a time from Onyx.connect, regardless of how many Onyx.connects it has attached.
Yep, I 100% agree with this! I'll get that issue created and assigned to you.
tests/actions/PolicyTagTest.ts
Outdated
| it('should return empty object when policyID is undefined', () => { | ||
| const result = buildOptimisticPolicyRecentlyUsedTags({ | ||
| policyTags: {}, | ||
| policyRecentlyUsedTags: {}, | ||
| policyID: undefined, | ||
| transactionTags: 'tag1', | ||
| }); | ||
| expect(result).toEqual({}); | ||
| }); |
There was a problem hiding this comment.
Once we remove the policyID from the function we can get rid of this test
tests/actions/PolicyTagTest.ts
Outdated
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('should return empty object when policyID is empty string', () => { |
tests/actions/PolicyTagTest.ts
Outdated
| const existingRecentlyUsedTags: RecentlyUsedTags = { | ||
| [tagListName]: ['Marketing', 'Sales'], | ||
| }; |
There was a problem hiding this comment.
I'd remove the const and create the object in the function call. There should be no confusion since arguments are named and the object is small
| const policyTags: PolicyTagLists = { | ||
| Tag: { | ||
| name: 'Tag', | ||
| orderWeight: 0, | ||
| required: false, | ||
| tags: { | ||
| Engineering: {name: 'Engineering', enabled: true}, | ||
| Marketing: {name: 'Marketing', enabled: true}, | ||
| Sales: {name: 'Sales', enabled: true}, | ||
| }, | ||
| }, | ||
| }; |
There was a problem hiding this comment.
We use this one a few times. How about extracting it and creating once with some meaningful name?
| policyTags: PolicyTagLists; | ||
| policyRecentlyUsedTags: RecentlyUsedTags; |
There was a problem hiding this comment.
Shouldn't this function handle the missing values? So we don't have to ?? {} each time?
There was a problem hiding this comment.
I'd honestly prefer to avoid this, as this function needs this data to function properly. They might be empty at times, but they should always be passed.
…x-connect/1-src-libs-actions-Policy-Tag.ts
|
@shubham1206agra Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
…x-connect/1-src-libs-actions-Policy-Tag.ts # Conflicts: # tests/actions/PolicyTagTest.ts
|
Is this ready to review again @dariusz-biela ? |
|
bump please @dariusz-biela |
|
It's now ready for further review. I apologize for not responding. I've been writing documentation for the last two days, which is why I haven't been on GitHub. |
src/libs/actions/IOU.ts
Outdated
| const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags(iouReport?.policyID, transactionChanges.tag); | ||
| const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({ | ||
| policyTags: getPolicyTagsData(iouReport?.policyID), | ||
| // TODO: Replace getPolicyRecentlyUsedTagsData with useOnyx hook |
There was a problem hiding this comment.
For all these TODOs, can you please include a link to the GH that it will be removed with?
There was a problem hiding this comment.
I will add link to this issue #66507
and write a comment there about it
There was a problem hiding this comment.
I see that you created new issue #71491 for file src/libs/actions/Policy/Tag.ts
but this code it is not related to src/libs/actions/Policy/Tag.ts anymore, now it is in src/libs/actions/IOU.ts
@tgolen could you change the name of the issue to reflect that?
also I think that parent issue should be set to #66507
Additional context
In this PR, I moved this code to src/libs/actions/IOU.ts:
let allRecentlyUsedTags: OnyxCollection<RecentlyUsedTags> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
waitForCollectionCallback: true,
callback: (val) => (allRecentlyUsedTags = val),
});Because src/libs/actions/Policy/Tag.ts doesnt need this data.
There was a problem hiding this comment.
I added a link like this to the issue you created:
// TODO: Replace getPolicyRecentlyUsedTagsData with useOnyx hook
// (https://github.com/Expensify/App/issues/71491)
There was a problem hiding this comment.
OK, thanks! I made the adjustments.
|
There is a newer version of the checklist that you'll need to update the PR description with in order to get the tests to pass. |
|
Let me do the review now. |
|
@shubham1206agra this is waiting on you for review |
Reviewer Checklist
Screenshots/VideosScreen.Recording.2025-10-06.at.6.03.35.PM.mov |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🚀 Deployed to staging by https://github.com/tgolen in version: 9.2.24-0 🚀
|
|
🚀 Deployed to staging by https://github.com/tgolen in version: 9.2.26-0 🚀
|
|
🚀 Deployed to production by https://github.com/lakchote in version: 9.2.26-7 🚀
|
Explanation of Change
This PR is part of a refactor to remove
Onyx.connectfrom the ssrc/libs/actions/Policy/Tag.tsfile and replace it withuseOnyx.It isolates the
buildOptimisticPolicyRecentlyUsedTagsfunction from theOnyx.connectdata.To ensure this refactor doesn't break anything, it adds automated tests to the
buildOptimisticPolicyRecentlyUsedTagsfunction.Important
I've temporarily added a function,
getPolicyRecentlyUsedTagsData, that returns data from Onyx.connect.I created it to avoid refactoring functions that use
buildOptimisticPolicyRecentlyUsedTagsin this PR.I've marked
getPolicyRecentlyUsedTagsDataasdeprecatedand added the comment// TODO: Replace getPolicyRecentlyUsedTagsData with useOnyx hookabove each use.Fixed Issues
This PR partially addresses these issues:
$ #69022
$ #69023
PROPOSAL:
Tests
Prerequisites:
Offline tests
Same as in the Tests section
QA Steps
Same as in the Tests section
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Nagranie.z.ekranu.2025-09-12.o.12.45.05.mov