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 'Navigation' lib to TypeScript #31543

Merged
merged 53 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
63bf692
Migrate getStateFromPath to TS
blazejkustra Nov 20, 2023
bb91e5f
Migrate FreezeWrapper to TS
blazejkustra Nov 20, 2023
834aa1a
Migrate linkingConfig to TS
blazejkustra Nov 20, 2023
bdb18b0
Migrate linkTo to TS
blazejkustra Nov 20, 2023
bab420e
Migrate navigationRef to TS
blazejkustra Nov 20, 2023
20cca5e
Migrate OnyxTabNavigator to TS
blazejkustra Nov 20, 2023
667e9f9
Migrate useFlipper to TS
blazejkustra Nov 20, 2023
91cbf0a
Migrate NavigationRoot to TS
blazejkustra Nov 20, 2023
11369ef
Augment react navigation to make navigation types global
blazejkustra Nov 20, 2023
b19ff6b
Migrate Navigation to TS
blazejkustra Nov 20, 2023
abbae01
Migrate getTopmostReportId
blazejkustra Nov 21, 2023
ab078fc
Rename type augmentation
blazejkustra Nov 21, 2023
604a246
Improve NavigationState type usage
blazejkustra Nov 21, 2023
bf9d3a7
Migrate shouldPreventDeeplinkPrompt
blazejkustra Nov 21, 2023
7a974b2
First type fixes
blazejkustra Nov 21, 2023
8e970d0
Remove unused import
blazejkustra Nov 21, 2023
a6b06a1
Migrate getTopmostReportActionID
blazejkustra Nov 21, 2023
a7edea8
Use RootStackParamList type instead of ReactNavigation.RootParamList
blazejkustra Nov 21, 2023
bf08755
Extract Route string union type
blazejkustra Nov 21, 2023
a9aa62d
Fix OnyxTabNavigator types
blazejkustra Nov 22, 2023
c71e7b0
Move RootStackParamList to Navigation/types
blazejkustra Nov 22, 2023
21eff8e
Clean navigation file
blazejkustra Nov 22, 2023
4551b3b
Move Navigation types to separate folder
blazejkustra Nov 22, 2023
17abe64
Update Navigation.md
blazejkustra Nov 22, 2023
aa31b00
Clean types and fix linkTo utility
blazejkustra Nov 22, 2023
c56d07b
Fix type error in App.ts
blazejkustra Nov 22, 2023
3338376
Merge branch 'main' into ts/Navigation
blazejkustra Nov 22, 2023
ff61b5f
Make sure Route isn't plain string
blazejkustra Nov 22, 2023
7bca0a9
Fix linkTo types
blazejkustra Nov 22, 2023
b721660
Get navContainsProtectedRoutes and waitForProtectedRoutes back
blazejkustra Nov 22, 2023
8e976fb
Restore old usage
blazejkustra Nov 22, 2023
a7d5b7d
Import findLastIndex
blazejkustra Nov 22, 2023
9e0271c
Remove unused navigation functions from export
blazejkustra Nov 23, 2023
0729113
Update navigation guidelines
blazejkustra Nov 23, 2023
7b43b13
Fix gramma
blazejkustra Nov 23, 2023
eda2266
Merge branch 'main' into ts/Navigation
blazejkustra Nov 23, 2023
c854942
Revert unintended change
blazejkustra Nov 23, 2023
8bf1658
Add PublicScreens pages to SCREENS.ts
blazejkustra Nov 23, 2023
a6a565a
straighten up all screen types
blazejkustra Nov 23, 2023
9524a13
Fix types
blazejkustra Nov 23, 2023
21d5c10
Fix type errors after final RootStackParamList adjustments
blazejkustra Nov 23, 2023
89c8ac8
Add missing return type
blazejkustra Nov 24, 2023
d206a34
Merge branch 'main' into ts/Navigation
blazejkustra Nov 24, 2023
f073b8c
Improve payload type in getMinimalAction
blazejkustra Nov 24, 2023
51d3176
Remove console.log
blazejkustra Nov 24, 2023
4d2722e
Fix after feedback
blazejkustra Nov 24, 2023
81a0242
Fix types after review
blazejkustra Nov 24, 2023
e522676
Another portion with small adjustements
blazejkustra Nov 24, 2023
69423c1
Improve types after final reviews
blazejkustra Nov 24, 2023
b4727e9
Adjust after internal review
blazejkustra Nov 27, 2023
70f7488
Fix lint
blazejkustra Nov 27, 2023
c392e00
Fix the logic
blazejkustra Nov 27, 2023
c28b279
Remove a variable
blazejkustra Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions contributingGuides/NAVIGATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@ When creating RHP flows, you have to remember a couple things:

An example of adding `Settings_Workspaces` page:

1. Add path to `ROUTES.js`: https://github.com/Expensify/App/blob/3531af22dcadaa94ed11eccf370517dca0b8c305/src/ROUTES.js#L36
1. Add path to `ROUTES.ts`: https://github.com/Expensify/App/blob/main/src/ROUTES.ts

```ts
export const ROUTES = {
// static route
SETTINGS_WORKSPACES: 'settings/workspaces',
// dynamic route
SETTINGS_WORKSPACES: {
route: 'settings/:accountID',
getRoute: (accountID: number) => `settings/${accountID}` as const,
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
},
};

```

2. Add `Settings_Workspaces` page to proper RHP flow in `linkingConfig.js`: https://github.com/Expensify/App/blob/3531af22dcadaa94ed11eccf370517dca0b8c305/src/libs/Navigation/linkingConfig.js#L40-L42
2. Add `Settings_Workspaces` page to proper RHP flow in `linkingConfig.ts`: https://github.com/Expensify/App/blob/3531af22dcadaa94ed11eccf370517dca0b8c305/src/libs/Navigation/linkingConfig.js#L40-L42

3. Add your page to proper navigator (it should be aligned with where you've put it in the previous step) https://github.com/Expensify/App/blob/3531af22dcadaa94ed11eccf370517dca0b8c305/src/libs/Navigation/AppNavigator/ModalStackNavigators.js#L334-L338

Expand Down
168 changes: 94 additions & 74 deletions src/ROUTES.ts

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/SCREENS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ const PROTECTED_SCREENS = {
REPORT_ATTACHMENTS: 'ReportAttachments',
} as const;

export default {
const SCREENS = {
...PROTECTED_SCREENS,
LOADING: 'Loading',
REPORT: 'Report',
NOT_FOUND: 'not-found',
TRANSITION_BETWEEN_APPS: 'TransitionBetweenApps',
VALIDATE_LOGIN: 'ValidateLogin',
UNLINK_LOGIN: 'UnlinkLogin',
SETTINGS: {
ROOT: 'Settings_Root',
PREFERENCES: 'Settings_Preferences',
Expand All @@ -40,4 +40,5 @@ export default {
SAML_SIGN_IN: 'SAMLSignIn',
} as const;

export default SCREENS;
export {PROTECTED_SCREENS};
1 change: 1 addition & 0 deletions src/components/withCurrentReportID.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type CurrentReportIDContextValue = {
updateCurrentReportID: (state: NavigationState) => void;
currentReportID: string;
};

type CurrentReportIDContextProviderProps = {
/** Actual content wrapped by this component */
children: React.ReactNode;
Expand Down
3 changes: 2 additions & 1 deletion src/components/withNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {NavigationProp, useNavigation} from '@react-navigation/native';
import React, {ComponentType, ForwardedRef, RefAttributes} from 'react';
import getComponentDisplayName from '@libs/getComponentDisplayName';
import {RootStackParamList} from '@libs/Navigation/types';

type WithNavigationProps = {
navigation: NavigationProp<ReactNavigation.RootParamList>;
navigation: NavigationProp<RootStackParamList>;
};

export default function withNavigation<TProps extends WithNavigationProps, TRef>(
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useFlipper/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/hooks/useFlipper/index.native.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/hooks/useFlipper/index.native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {useFlipper as useFlipperRN} from '@react-navigation/devtools';
import UseFlipper from './types';

const useFlipper: UseFlipper = useFlipperRN;

export default useFlipper;
5 changes: 5 additions & 0 deletions src/hooks/useFlipper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import UseFlipper from './types';

const useFlipper: UseFlipper = () => {};

export default useFlipper;
6 changes: 6 additions & 0 deletions src/hooks/useFlipper/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {NavigationContainerRefWithCurrent} from '@react-navigation/core';
import {RootStackParamList} from '@libs/Navigation/types';

type UseFlipper = (ref: NavigationContainerRefWithCurrent<RootStackParamList>) => void;

export default UseFlipper;
10 changes: 5 additions & 5 deletions src/libs/Navigation/AppNavigator/PublicScreens.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ function PublicScreens() {
component={LogInWithShortLivedAuthTokenPage}
/>
<RootStack.Screen
name="ValidateLogin"
name={SCREENS.VALIDATE_LOGIN}
options={defaultScreenOptions}
component={ValidateLoginPage}
/>
<RootStack.Screen
name="UnlinkLogin"
name={SCREENS.UNLINK_LOGIN}
options={defaultScreenOptions}
component={UnlinkLoginPage}
/>
<RootStack.Screen
name="AppleSignInDesktop"
name={SCREENS.SIGN_IN_WITH_APPLE_DESKTOP}
options={defaultScreenOptions}
component={AppleSignInDesktopPage}
/>
<RootStack.Screen
name="GoogleSignInDesktop"
name={SCREENS.SIGN_IN_WITH_GOOGLE_DESKTOP}
options={defaultScreenOptions}
component={GoogleSignInDesktopPage}
/>
<RootStack.Screen
name="SAMLSignIn"
name={SCREENS.SAML_SIGN_IN}
options={defaultScreenOptions}
component={SAMLSignInPage}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
import {useIsFocused, useNavigation, useRoute} from '@react-navigation/native';
import lodashFindIndex from 'lodash/findIndex';
import PropTypes from 'prop-types';
import React, {useEffect, useRef, useState} from 'react';
import {Freeze} from 'react-freeze';
import {InteractionManager} from 'react-native';
import ChildrenProps from '@src/types/utils/ChildrenProps';

const propTypes = {
type FreezeWrapperProps = ChildrenProps & {
/** Prop to disable freeze */
keepVisible: PropTypes.bool,
/** Children to wrap in FreezeWrapper. */
children: PropTypes.node.isRequired,
keepVisible?: boolean;
};

const defaultProps = {
keepVisible: false,
};

function FreezeWrapper(props) {
function FreezeWrapper({keepVisible = false, children}: FreezeWrapperProps) {
const [isScreenBlurred, setIsScreenBlurred] = useState(false);
// we need to know the screen index to determine if the screen can be frozen
const screenIndexRef = useRef(null);
const screenIndexRef = useRef<number | null>(null);
const isFocused = useIsFocused();
const navigation = useNavigation();
const currentRoute = useRoute();

useEffect(() => {
const index = lodashFindIndex(navigation.getState().routes, (route) => route.key === currentRoute.key);
const index = navigation.getState().routes.findIndex((route) => route.key === currentRoute.key);
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
screenIndexRef.current = index;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand All @@ -35,7 +28,7 @@ function FreezeWrapper(props) {
// if the screen is more than 1 screen away from the current screen, freeze it,
// we don't want to freeze the screen if it's the previous screen because the freeze placeholder
// would be visible at the beginning of the back animation then
if (navigation.getState().index - screenIndexRef.current > 1) {
if (navigation.getState().index - (screenIndexRef?.current ?? 0) > 1) {
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
InteractionManager.runAfterInteractions(() => setIsScreenBlurred(true));
} else {
setIsScreenBlurred(false);
Expand All @@ -44,11 +37,9 @@ function FreezeWrapper(props) {
return () => unsubscribe();
}, [isFocused, isScreenBlurred, navigation]);

return <Freeze freeze={!isFocused && isScreenBlurred && !props.keepVisible}>{props.children}</Freeze>;
return <Freeze freeze={!isFocused && isScreenBlurred && !keepVisible}>{children}</Freeze>;
}

FreezeWrapper.propTypes = propTypes;
FreezeWrapper.defaultProps = defaultProps;
FreezeWrapper.displayName = 'FreezeWrapper';

export default FreezeWrapper;
Loading
Loading