-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[TS Migration] Migrate '[Remaining Group 2]' hook to TypeScript #32484
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
Changes from all commits
54f298d
4b88248
44280d9
4ec450a
6c420d6
1e156cc
fc72803
7fbb054
ed9a26b
5c42c73
b620ee8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,29 @@ | ||
| import {useFocusEffect} from '@react-navigation/native'; | ||
| import {useCallback, useContext, useEffect, useRef, useState} from 'react'; | ||
| import {InteractionManager} from 'react-native'; | ||
| import {InteractionManager, TextInput} from 'react-native'; | ||
| import CONST from '@src/CONST'; | ||
| import * as Expensify from '@src/Expensify'; | ||
|
|
||
| export default function useAutoFocusInput() { | ||
| type UseAutoFocusInput = { | ||
| inputCallbackRef: (ref: TextInput | null) => void; | ||
| }; | ||
|
|
||
| export default function useAutoFocusInput(): UseAutoFocusInput { | ||
| const [isInputInitialized, setIsInputInitialized] = useState(false); | ||
| const [isScreenTransitionEnded, setIsScreenTransitionEnded] = useState(false); | ||
|
|
||
| // @ts-expect-error TODO: Remove this when Expensify.js is migrated. | ||
| const {isSplashHidden} = useContext(Expensify.SplashScreenHiddenContext); | ||
|
|
||
| const inputRef = useRef(null); | ||
| const focusTimeoutRef = useRef(null); | ||
| const inputRef = useRef<TextInput | null>(null); | ||
| const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!isScreenTransitionEnded || !isInputInitialized || !inputRef.current || !isSplashHidden) { | ||
| return; | ||
| } | ||
| InteractionManager.runAfterInteractions(() => { | ||
| inputRef.current.focus(); | ||
| inputRef.current?.focus(); | ||
|
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. Is this necessary? Wouldn't the above early return protect against
Contributor
Author
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. It is necessary, when I remove it, there is an error saying it could be undefined. Do you think we should remove it from the if statement in that case?
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. I think it's unnecessary to have both the early return and the optional chaining. I think we should do one or the other. From what I've seen, we favor the optional chaining, so I would remove the early returns, yeah. The early returns are good for when it's good to avoid unnecessary processing like In this case, maybe the early return is good to keep. If there is no
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. Sorry, I said to do two different things there. Let's keep the early return and the optional chaining (since that throws an error). No changes necessary.
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. @tgolen The optional chaining is necessary because |
||
| setIsScreenTransitionEnded(false); | ||
| }); | ||
| }, [isScreenTransitionEnded, isInputInitialized, isSplashHidden]); | ||
|
|
@@ -38,7 +43,7 @@ export default function useAutoFocusInput() { | |
| }, []), | ||
| ); | ||
|
|
||
| const inputCallbackRef = (ref) => { | ||
| const inputCallbackRef = (ref: TextInput | null) => { | ||
| inputRef.current = ref; | ||
| setIsInputInitialized(true); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** This direct import is required, because this function was added by a patch, | ||
| * and its typings are not supported by default */ | ||
| import {useTabAnimation} from '@react-navigation/material-top-tabs/src/utils/useTabAnimation'; | ||
|
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. Can you help me understand the purpose of this file and why it's necessary?
Contributor
Author
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. Sure! So this function was not originally in the library, afaik it was added with a patch, and it was good enough when we used JS, but once migrated to TypeScript it wasn't good enough anymore. I tried a couple solutions and this is the only one I found that works fully, importing it directly from the file and augmenting the types. We discussed it in our TS team and this is what we agreed on. Hope that answers your question!
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. OK, thanks! That's good context. Would you mind adding a code comment to this file which explains that?
Contributor
Author
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. Sure thing, done! |
||
|
|
||
| declare module '@react-navigation/material-top-tabs' { | ||
| // eslint-disable-next-line import/prefer-default-export | ||
| export {useTabAnimation}; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.