Skip to content

Commit

Permalink
fix: fixes for deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed May 27, 2023
1 parent d94cef9 commit d098377
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 39 deletions.
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"expo-splash-screen": "~0.18.2",
"expo-status-bar": "^1.4.4",
"expo-system-ui": "~2.2.1",
"navigation": "^6.1.0",
"navigation": "^6.2.0",
"navigation-react": "^4.5.1",
"navigation-react-mobile": "^3.9.0",
"navigation-react-native": "^9.2.2",
"navigation-react-mobile": "^3.10.0",
"navigation-react-native": "^9.2.3",
"navigation-react-native-web": "^1.3.0",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { BottomRoot, NavigationRoots, screens } from './Navigator';
import routes from './Routes';
import AsyncBoundaryScreen from './helpers/AsyncBoundaryScreen';
import HeaderWeb from './HeaderWeb';

function getTheme(colorScheme: ColorSchemeName): typeof MD3LightTheme {
const isDark = colorScheme === 'dark';
const baseTheme = colorScheme === 'dark' ? MD3DarkTheme : MD3LightTheme;
Expand Down Expand Up @@ -114,6 +113,7 @@ export default function App() {
SuspenseContainer={AsyncBoundaryScreen}
navigationRoot={navigationRoot}
themeSettings={theme}
initialRootKey={NavigationRoots.RootHome}
/>
</AsyncBoundary>
</PaperProvider>
Expand Down
6 changes: 4 additions & 2 deletions example/src/HeaderWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function HeaderWeb({
<>
<View
style={{
backgroundColor: '#FCFCFC',
backgroundColor: theme.colors.background,
alignItems: 'center',
flexDirection: 'row',
paddingTop: 12,
Expand All @@ -27,7 +27,9 @@ export default function HeaderWeb({
paddingHorizontal: 24,
}}
>
<Text style={{ color: '#000', fontSize: 22 }}>Logo</Text>
<Text style={{ color: theme.colors.onBackground, fontSize: 22 }}>
Logo
</Text>
<BottomTabLink to={BottomRoot.Home} params={{}}>
{({ isSelected, ...linkProps }) => (
<Button {...linkProps}>
Expand Down
2 changes: 1 addition & 1 deletion example/src/NavigatorScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const PostsScreen = registerScreen(
);

export const PostScreen = registerScreen(
'/post-test/:id',
'/post/:id',
RequireAuthHOC(lazy(() => import('./PostScreen'))),
(params) => {
const { id } = params;
Expand Down
2 changes: 2 additions & 0 deletions example/src/PostScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { queryKeyPostScreen, queryKeyPostScreenPromise } from './queryKeys';
import routes from './Routes';
import ButtonLink from './ButtonLink';
import Spacer from './helpers/Spacer';
import { useRenderLog } from './helpers/utils';

function PostScreen() {
useRenderLog('PostScreen');
// optional with react-query but could be used i.c.w. Relay.dev etc.
// for now we use this to test if it keeps working
const queryReference = usePreloadResult(routes.PostScreen);
Expand Down
8 changes: 2 additions & 6 deletions example/src/useAuthState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const useAuthState = create<UseAuthState>(() => initialState || emptyAuthState);

export function setAuthState(state: UseAuthState) {
useAuthState.setState(state);
storage.set('auth', JSON.stringify(state));
}
fetchAndSaveProfileForToken({ token: initialState.token });
export function reset() {
Expand All @@ -50,17 +51,12 @@ export async function fetchAndSaveProfileForToken({
if (!token) {
return;
}
useAuthState.setState((prev) => ({
...prev,
token,
resolving: true,
}));

const response: any = await api({
path: 'users/1',
});

useAuthState.setState({
setAuthState({
token,
user: response,
resolving: false,
Expand Down
23 changes: 14 additions & 9 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6480,32 +6480,37 @@ nanomatch@^1.2.9:
snapdragon "^0.8.1"
to-regex "^3.0.1"

navigation-react-mobile@*, navigation-react-mobile@^3.9.0:
navigation-react-mobile@*:
version "3.9.0"
resolved "https://registry.npmjs.org/navigation-react-mobile/-/navigation-react-mobile-3.9.0.tgz#4b38bd2412f3a503346c1912c48b24639a3ba5fd"
integrity sha512-ZWwRs3TAByIXgne2ZNvTLSgXa0tKK4puorEC05SUnmeJobXOMFPJpJiYDQrb/pZg0GBr69waFAqPiveGyr0CGw==

navigation-react-mobile@^3.10.0:
version "3.10.0"
resolved "https://registry.npmjs.org/navigation-react-mobile/-/navigation-react-mobile-3.10.0.tgz#ba9ef10d51b492a449b3afa9777debe86f70dabf"
integrity sha512-SEOhP6ugX02kZ/f+2KZA1G+UTMLhyBWLSJAGUfUrOAPSFqCmPv0jKJTRM2EoAvQg+FVqfwOObXNoDWf2UXEBIA==

navigation-react-native-web@^1.3.0:
version "1.3.0"
resolved "https://registry.npmjs.org/navigation-react-native-web/-/navigation-react-native-web-1.3.0.tgz#dc192205d3d9fc117adac51d7b86c83748ac92b8"
integrity sha512-FULKMnqpc7EZ3uI6UVVpg1cxUyR/19pyfZahP4Fh4q5kitwrAzwqkTxT9fB7rspnhX1sDFq2E7UL46GFdx6uNQ==
dependencies:
navigation-react-mobile "*"

navigation-react-native@^9.2.2:
version "9.2.2"
resolved "https://registry.npmjs.org/navigation-react-native/-/navigation-react-native-9.2.2.tgz#91968f9e407f1e91e654a3f94394d772094acfe0"
integrity sha512-XaxKMtczRnFF+9yLHAteP09sWVcgyADMv8dPHfHUI8DyR/RhZs3bmHtG6PX33r2nd4Rl27pYTYe4w3e1+02n+Q==
navigation-react-native@^9.2.3:
version "9.2.3"
resolved "https://registry.npmjs.org/navigation-react-native/-/navigation-react-native-9.2.3.tgz#32ba22192d3e4d75e30008490616e5926e3dad50"
integrity sha512-P6VxlEUPD+qir1zGB80u43NQPuNwxXIsskpL79Kul65MnUDKSFvuua4LDTEE2y39hAIl0et3rA++8mzUP1AjbQ==

navigation-react@^4.5.1:
version "4.5.1"
resolved "https://registry.npmjs.org/navigation-react/-/navigation-react-4.5.1.tgz#47adb8ba220f0701736328b359f347a619f2fc6c"
integrity sha512-s8FUkLATlr2JcXiyOR9DUQlIAZNyqX3oW0m2u6VCLxmSNSUHUCqueSsULtRTS2FoBAYqN81tZzLMsSw0sBQeDg==

navigation@^6.1.0:
version "6.1.0"
resolved "https://registry.npmjs.org/navigation/-/navigation-6.1.0.tgz#6addd11b212064250f6c7168c05b4ddb1fbbd2aa"
integrity sha512-lzFTF9L49o5v5/Ja3VTACLqg2cthMu63YjAa8ripzMA6+ayAVUvNmfulZcbHhU8LFX/qoUlb8fSSKFb+lNpwQQ==
navigation@^6.2.0:
version "6.2.0"
resolved "https://registry.npmjs.org/navigation/-/navigation-6.2.0.tgz#9dee79f82e1ab603fa2d9bdd5465e42441fcbf6e"
integrity sha512-PcAsKXzTJ4WSLEz2AWHeIv+R9tvagN7qdMaGO8mY0SuPaY11q5HLBbpYQi6+kwljbmRKHju0gxRMwNf17e1zDg==

ncp@~2.0.0:
version "2.0.0"
Expand Down
62 changes: 47 additions & 15 deletions src/NavigationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,55 @@ export default function NavigationProvider<ScreenItems extends BaseScreen[]>({
[preloadElement, preloadScreen, rootNavigator]
);

React.useMemo(() => {
const path = getPathFromUrl(initialUrl);
const rootKey = getRootKeyFromPath(path)!;
const root = navigationRoot[rootKey];
const rootType = root?.type;
const isBottomTabs = rootType === 'bottomTabs';
const isNative = Platform.OS !== 'web';
const openUrl = React.useCallback(
(uri: string, change: boolean, fallback?: boolean) => {
try {
const path = getPathFromUrl(uri);
const rootKey = getRootKeyFromPath(path)!;
const root = navigationRoot[rootKey];
const rootType = root?.type;
const isBottomTabs = rootType === 'bottomTabs';
const isNative = Platform.OS !== 'web';

if (isNative && isBottomTabs) {
preloadRoot(rootKey);
rootNavigator.start(rootKey);
} else {
preloadLink(initialUrl);
rootNavigator.navigateLink(initialUrl);
}
if (isNative && isBottomTabs) {
preloadRoot(rootKey);
rootNavigator.start(rootKey);
} else {
preloadLink(uri);
// rootNavigator.start(rootKey);
rootNavigator.navigateLink(uri);
}
} catch (e) {
console.log(
`Can't convert deep link to the right stack: ${initialUrl}`,
e,
{ fallback, change }
);
if (!fallback && !change) {
console.log(
'there is no app started yet, lets go to the default url}',
{ defaultUrl: initialDefaultUrl }
);
// setTimeout(() => {
openUrl(initialDefaultUrl, false, true);
// }, 1000);
}
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialUrl]);
[]
);

const onStart = () => {
openUrl(initialUrl, false, false);
return initialUrl;
};
const startedUrl = React.useRef(onStart());
React.useEffect(() => {
if (startedUrl.current !== initialUrl) {
openUrl(initialUrl, false);
}
}, [initialUrl, openUrl]);

return (
<RidgeNavigationContext.Provider
Expand Down
1 change: 1 addition & 0 deletions src/contexts/OptimizedContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function OptimizedContextProvider({
data: fallbackData,
} = React.useContext(NavigationContext);
const preloadId = state?.preloadId || fallbackState?.preloadId;

const params = data || fallbackData;

const value = React.useMemo(
Expand Down
8 changes: 6 additions & 2 deletions src/navigationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ export function getPathFromUrl(url: string): string {
const u = new URL(url);
return u.pathname + u.search;
}
const split = url.split('://');
return '/' + split[1];
if (url.includes('://')) {
const split = url.split('://');
return '/' + split[1];
}

return '/' + url;
}
export function partMatches(
urlPart: string | undefined,
Expand Down

0 comments on commit d098377

Please sign in to comment.