Skip to content

Commit 2bbaad6

Browse files
committed
Working around a url linking issue on Android
1 parent ef16fbc commit 2bbaad6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/hooks/useReviewCredentials.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react';
22

33
import { Credentials } from './useAuth';
44
import { useURLMemo } from './useURLMemo';
5+
import { Platform } from 'react-native';
56

67
/**
78
* Parses a launch URL and if is a review environment connection URL, uses the embedded credentials to sign in to the app.
@@ -13,7 +14,7 @@ import { useURLMemo } from './useURLMemo';
1314
*
1415
*/
1516
const parseLaunchURLCredentials = (url: URL): Credentials | null => {
16-
const serverURL = url.searchParams.get('apiHost');
17+
var serverURL = url.searchParams.get('apiHost');
1718
const apiKey = url.searchParams.get('apiKey');
1819

1920
// TODO: support userExternalID as well
@@ -25,6 +26,15 @@ const parseLaunchURLCredentials = (url: URL): Credentials | null => {
2526
console.warn('Could not parse credentials from launch URL: ', url.toString());
2627
return null;
2728
}
29+
30+
// Map localhost on Android emulators
31+
// https://stackoverflow.com/a/6310592/25724
32+
if (Platform.OS === 'android' && serverURL.indexOf('localhost')) {
33+
var url = new URL(serverURL);
34+
url.hostname = '10.0.2.2';
35+
serverURL = url.href;
36+
}
37+
2838
const credentials: Credentials = {
2939
serverURL,
3040
apiKey,

src/hooks/useURLMemo.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useURL } from 'expo-linking';
1+
import { useURL, useLinkingURL } from 'expo-linking';
22

33
import React, { createContext, useContext, useEffect, useState } from 'react';
44

@@ -18,11 +18,12 @@ export const useURLMemo = () => {
1818

1919
export default function URLMemoProvider({ children }: { children: React.ReactElement }) {
2020
const url = useURL();
21-
const [lastURL, setLastURL] = useState<string | null>(url);
21+
const linkingURL = useLinkingURL();
22+
const [lastURL, setLastURL] = useState<string | null>(url || linkingURL);
2223

2324
useEffect(() => {
24-
setLastURL(url);
25-
}, [url]);
25+
setLastURL(url || linkingURL);
26+
}, [url, linkingURL]);
2627

2728
return <URLMemoContext.Provider value={{ url: lastURL }}>{children}</URLMemoContext.Provider>;
2829
}

0 commit comments

Comments
 (0)