-
Notifications
You must be signed in to change notification settings - Fork 3k
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 'useCurrentReportID.js' hook to TypeScript #29264
[TS migration] Migrate 'useCurrentReportID.js' hook to TypeScript #29264
Conversation
…s-migration/useCurrentReportID/hook
@VickyStash fixed :) |
|
||
export default function withCurrentReportID(WrappedComponent) { | ||
const WithCurrentReportID = forwardRef((props, ref) => ( | ||
export default function withCurrentReportID<TComponentProps extends CurrentReportIDContextValue>(WrappedComponent: ComponentType<TComponentProps>) { |
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.
- Maybe rename to
TComponentProps
toTProps
for better visibility? - HOCs need to be more generic in terms of ref, that's why we need to add another type parameter
TRef
. Without a type parameter, typescript complains with this test code:
const X = forwardRef((props: CurrentReportIDContextValue, ref: React.Ref<HTMLInputElement>) => {
console.log(props.currentReportID);
console.log(props.updateCurrentReportID);
return <input ref={ref} />;
});
function Test() {
const Wrapped = withCurrentReportID(X);
const testRef = useRef<HTMLInputElement>(null);
const x = <Wrapped ref={testRef} />;
}
- According to the guidelines (discussion on slack). We should use
forwardRef
a bit different, extract the component and define it in a separate line. Wrap it inforwardRef
on the last line ofwithCurrentReportID
HOC.
Final code looks like this:
export default function withCurrentReportID<TProps extends CurrentReportIDContextValue, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) {
function WithCurrentReportID(props: Omit<TProps, keyof CurrentReportIDContextValue>, ref: ForwardedRef<TRef>) {
return (
<CurrentReportIDContext.Consumer>
{(currentReportIDUtils) => (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...currentReportIDUtils}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(props as TProps)}
ref={ref}
/>
)}
</CurrentReportIDContext.Consumer>
);
}
WithCurrentReportID.displayName = `withCurrentReportID(${getComponentDisplayName(WrappedComponent)})`;
return forwardRef(WithCurrentReportID);
}
currentReportID: string; | ||
}; | ||
type CurrentReportIDContextProviderProps = { | ||
children: React.ReactNode; |
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.
NAB: Copy the comment from propTypes:
/** Actual content wrapped by this component */
}, | ||
[setCurrentReportID], | ||
); | ||
|
||
/** | ||
* The context this component exposes to child components | ||
* @returns {Object} currentReportID to share between central pane and LHN | ||
* @returns currentReportID to share between central pane and LHN |
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.
* @returns currentReportID to share between central pane and LHN | |
* @returns currentReportID to share between central pane and LHN |
@blazejkustra thanks for the comments, fixed 😄 |
@AndrewGable 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] |
@kubabutkiewicz Again, when you marked this PR as ready no C+ was assigned. @AndrewGable Can you assign some C+ so that they can complete the reviewer checklist? |
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.
Please write QA tests
QA step is still missing |
…s-migration/useCurrentReportID/hook
QA steps added |
*/ | ||
const updateCurrentReportID = useCallback( | ||
(state: NavigationState) => { | ||
setCurrentReportID(Navigation.getTopmostReportId(state) ?? ''); |
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.
why is this change needed?
I think it's original logic to return undefined or null as is
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.
@situchan So
App/src/components/withCurrentReportID.js
Lines 20 to 22 in a094cbb
function CurrentReportIDContextProvider(props) { | |
const [currentReportID, setCurrentReportID] = useState(''); | |
The default state is empty string so useState is infering from this that currentReportID will be always string, and
Navigation.getTopmostReportId
is returning string | undefined
we need to use ??
to put empty string in caseNavigation.getTopmostReportId
would return undefined
@kubabutkiewicz Also, please fix conflict |
…s-migration/useCurrentReportID/hook
@situchan fixed |
@situchan kindly bumping for review 😄 |
will review this in an hr |
Please pull main |
WithCurrentReportID.displayName = `withCurrentReportID(${getComponentDisplayName(WrappedComponent)})`; | ||
|
||
return forwardRef(WithCurrentReportID); |
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.
I expected this would cause console error since displayName is set to component wrapped with forwardRef.
But actually not happening. Not sure why.
Reviewer Checklist
Screenshots/VideosAndroid: Nativeandroid.movAndroid: mWeb Chromemchrome.moviOS: Nativeios.moviOS: mWeb Safarimsafari.movMacOS: Chrome / Safariweb.movMacOS: Desktopdesktop.mov |
…s-migration/useCurrentReportID/hook
@AndrewGable all yours |
✋ 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 production by https://github.com/puneetlath in version: 1.3.97-7 🚀
|
🚀 Deployed to staging by https://github.com/AndrewGable in version: 1.3.98-0 🚀
|
🚀 Deployed to production by https://github.com/roryabraham in version: 1.3.98-5 🚀
|
Details
Fixed Issues
$ 24944
$ 24956
Tests
Offline tests
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Android: Native
android.mp4
Android: mWeb Chrome
iOS: Native
ios.mp4
iOS: mWeb Safari
MacOS: Chrome / Safari
web.mp4
MacOS: Desktop
desktop.mp4