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

[TS migration] Migrate Onyx private methods to TS #523

Merged
merged 53 commits into from
Apr 16, 2024

Conversation

blazejkustra
Copy link
Contributor

@blazejkustra blazejkustra commented Mar 27, 2024

Details

This PR migrates "private" Onyx.js methods that we use in NewDot, "private" methods were moved to OnyxUtils.js in this PR.

Related Issues

Part of Expensify/App#34344

Automated Tests

N/A

Manual Tests

  • Tested NewDot thoroughly on every platform, requested money, logged in and out, sent messages

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
android.webm
Android: mWeb Chrome
android-web.webm
iOS: Native
ios.mp4
iOS: mWeb Safari
ios-web.mp4
MacOS: Chrome / Safari
web.mov
MacOS: Desktop
desktop.mov

@tgolen tgolen changed the title [TS migration] Migrate Onyx private methods to TS [HOLD] [TS migration] Migrate Onyx private methods to TS Mar 28, 2024
@tgolen
Copy link
Collaborator

tgolen commented Mar 28, 2024

I'm placing this on HOLD right now in order for QA to be done on the latest version of Onyx without more un-QAed PRs from being merged.

@blazejkustra
Copy link
Contributor Author

Absolutely! The PR is still WIP, so no need to worry that is going to be merged too soon

@melvin-bot melvin-bot bot requested a review from aldo-expensify April 9, 2024 11:33
@blazejkustra blazejkustra marked this pull request as draft April 9, 2024 11:34
@blazejkustra
Copy link
Contributor Author

blazejkustra commented Apr 9, 2024

Sorry @aldo-expensify, I miss-clicked. @fabioh8010 is still reviewing this PR, once it's approved I'll mark it as ready for review 😄

Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

Also let's make sure all functions have return types!

lib/Onyx.ts Outdated Show resolved Hide resolved
lib/Onyx.ts Outdated Show resolved Hide resolved
lib/Onyx.ts Outdated Show resolved Hide resolved
lib/Onyx.ts Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
lib/types.ts Outdated Show resolved Hide resolved
lib/OnyxUtils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

LGTM 🔥 🚀

@blazejkustra blazejkustra marked this pull request as ready for review April 12, 2024 10:18
@blazejkustra
Copy link
Contributor Author

@aldo-expensify All yours! It was already tested in NewDot by me and @fabioh8010 😄

Copy link
Contributor

@aldo-expensify aldo-expensify left a comment

Choose a reason for hiding this comment

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

Overall looks good to me, I just left some comments regarding casts that I think we should avoid if possible

lib/useOnyx.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
lib/DevTools.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
Copy link
Contributor

@aldo-expensify aldo-expensify left a comment

Choose a reason for hiding this comment

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

Left a comment

lib/useOnyx.ts Show resolved Hide resolved
lib/useOnyx.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
lib/OnyxUtils.ts Show resolved Hide resolved
Comment on lines +292 to +303
type DefaultConnectOptions<TKey extends OnyxKey> = {
key: TKey;
callback?: DefaultConnectCallback<TKey>;
waitForCollectionCallback?: false;
};

/** Represents the callback function used in `Onyx.connect()` method with a collection key. */
type CollectionConnectOptions<TKey extends OnyxKey> = {
key: TKey extends CollectionKeyBase ? TKey : never;
callback?: CollectionConnectCallback<TKey>;
waitForCollectionCallback: true;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Last comment/question: Wondering why these types were not defined like this:

Suggested change
type DefaultConnectOptions<TKey extends OnyxKey> = {
key: TKey;
callback?: DefaultConnectCallback<TKey>;
waitForCollectionCallback?: false;
};
/** Represents the callback function used in `Onyx.connect()` method with a collection key. */
type CollectionConnectOptions<TKey extends OnyxKey> = {
key: TKey extends CollectionKeyBase ? TKey : never;
callback?: CollectionConnectCallback<TKey>;
waitForCollectionCallback: true;
};
type DefaultConnectOptions<TKey extends Key> = {
key: TKey;
callback?: DefaultConnectCallback<TKey>;
waitForCollectionCallback?: false;
};
/** Represents the callback function used in `Onyx.connect()` method with a collection key. */
type CollectionConnectOptions<TKey extends CollectionKeyBase> = {
key: TKey;
callback?: CollectionConnectCallback<TKey>;
waitForCollectionCallback: true;
};

DefaultConnectOptions<TKey extends OnyxKey> feels a bit wrong since OnyxKey can be a collection key, then the waitForCollectionCallback: false would be wrong for such key. Also CollectionConnectOptions<TKey extends CollectionKeyBase> allows you to do key: TKey instead of the more complex TKey extends CollectionKeyBase ? TKey : never;.

Do you see any downside of specifying these types like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was like, the type looks weird indeed but it got me thinking that it was defined like this for a reason 😅

I checked in NewDot, this type works until you use it with a key (not a collection key) and add waitForCollectionCallback: true:

let allCards: OnyxValues[typeof ONYXKEYS.CARD_LIST] = {};
Onyx.connect({
    key: ONYXKEYS.CARD_LIST,
    callback: (val) => {
        if (!val || Object.keys(val).length === 0) {
            return;
        }

        allCards = val;
    },
    waitForCollectionCallback: true,
});

It makes val: OnyxCollection<CardList> while it should be val: OnyxEntry<CardList>. With the old type errors properly: Type 'true' is not assignable to type 'false'

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, interesting, wouldn't that mean that ONYXKEYS.CARD_LIST is a valid CollectionKeyBase when it shouldn't be?

Copy link
Contributor

@aldo-expensify aldo-expensify Apr 15, 2024

Choose a reason for hiding this comment

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

In your example, if things worked properly, I would have expected one of these two errors:

  • key: ONYXKEYS.CARD_LIST is wrong because key must be a CollectionKeyBase and is not, or
  • waitForCollectionCallback: true has an error because ONYXKEYS.CARD_LIST is a Key and therefore, waitForCollectionCallback must be false or undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, interesting, wouldn't that mean that ONYXKEYS.CARD_LIST is a valid CollectionKeyBase when it shouldn't be?

I checked it in ONYXKEYS.ts in NewDot and ONYXKEYS.CARD_LIST is not a valid CollectionKeyBase:

const cardList1: OnyxCollectionKey = 'cardList'; // errors
const cardList2: OnyxValueKey = 'cardList'; // works fine

For context, OnyxCollectionKey and OnyxValueKey are used in src/types/modules/react-native-onyx.d.ts to augment onyx keys:

declare module 'react-native-onyx' {
    // eslint-disable-next-line @typescript-eslint/consistent-type-definitions
    interface CustomTypeOptions {
        keys: OnyxValueKey | OnyxFormKey | OnyxFormDraftKey;
        collectionKeys: OnyxCollectionKey;
        values: OnyxValues;
    }
}

This way keys, and collectionKeys are no longer string but instead are union of literal strings

Copy link
Contributor Author

@blazejkustra blazejkustra Apr 16, 2024

Choose a reason for hiding this comment

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

In your example, if things worked properly, I would have expected one of these two errors:

key: ONYXKEYS.CARD_LIST is wrong because key must be a CollectionKeyBase and is not, or
waitForCollectionCallback: true has an error because ONYXKEYS.CARD_LIST is a Key and therefore, waitForCollectionCallback must be false or undefined

As you already noted, all Key, OnyxKey and CollectionKeyBase are of type string in react-native-onyx. It's later augmented in the end-user repo like NewDot, that's why it is so tricky to understand.

While this seems like a great idea, it comes with a problem. It does work until all Key, OnyxKey and CollectionKeyBase are of type string, but once we augment these values through src/types/modules/react-native-onyx.d.ts file, ConnectOptions type breaks:

type Key = 'account' | 'isSidebarLoaded' ;
type CollectionKeyBase = 'report_';
type OnyxKey = Key | CollectionKey;

type DefaultConnectOptions<TKey extends Key> = {
    key: TKey;
    callback?: DefaultConnectCallback<TKey>;
    waitForCollectionCallback?: false;
};

type CollectionConnectOptions<TKey extends CollectionKeyBase> = {
    key: TKey;
    callback?: CollectionConnectCallback<TKey>;
    waitForCollectionCallback?: true;
};

type ConnectOptions<TKey extends OnyxKey> = CollectionConnectOptions<TKey> | DefaultConnectOptions<TKey>; 
// Type 'TKey' does not satisfy the constraint 'Key'.
  // Type 'OnyxKey' is not assignable to type 'Key'.

That is why TS struggles to show an error here - when defining the type in onyx it thinks it is all right to use Key, OnyxKey and CollectionKeyBase interchangeably, but once these types are detached from each other (augmented in NewDot) TS doesn't throw an error when it should.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should leave ConnectOptions as is, @fabioh8010 maybe you have some idea as you created this type?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, well, thanks a lot for trying and following up with this. Meanwhile, I'll merge since the PR leaves us in a better place anyway and I don't want to keep blocking here. If you come up with some improvements later, just lets do them in a follow up PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is the first time I see type "augmentation" in typescript, pretty cool feature!

Copy link
Contributor

Choose a reason for hiding this comment

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

I think is better to keep the way it is for now, this was the best solution we found at that time to add this type safety around waitForCollectionCallback and collection keys.

@aldo-expensify aldo-expensify merged commit 3146d48 into Expensify:main Apr 16, 2024
4 checks passed
Copy link
Contributor

🚀Published to npm in v2.0.28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants