Skip to content

Commit

Permalink
fix: missing scheme on runtime when using expo-updates (achorein#48)
Browse files Browse the repository at this point in the history
* fix: missing scheme on runtime when using expo-updates

* bump example deps

* add utils to compute scheme from expo-linking when missing
  • Loading branch information
achorein authored Apr 10, 2024
1 parent 25ad80c commit 65c7d66
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 56 deletions.
2 changes: 1 addition & 1 deletion example/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"postinstall": "patch-package"
},
"dependencies": {
"expo": "~50.0.14",
"expo": "~50.0.15",
"expo-dev-client": "~3.3.11",
"expo-splash-screen": "~0.26.4",
"expo-status-bar": "~1.11.1",
Expand Down
18 changes: 9 additions & 9 deletions example/basic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4005,10 +4005,10 @@ [email protected]:
find-up "^5.0.0"
fs-extra "^9.1.0"

[email protected].12:
version "1.11.12"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.12.tgz#d5c7b3ed7ab57d4fb6885a0d8e10287dcf1ffe5f"
integrity sha512-/e8g4kis0pFLer7C0PLyx98AfmztIM6gU9jLkYnB1pU9JAfQf904XEi3bmszO7uoteBQwSL6FLp1m3TePKhDaA==
[email protected].13:
version "1.11.13"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.13.tgz#a8e63ad844e966dce78dea40b50839af6c3bc518"
integrity sha512-2H5qrGUvmLzmJNPDOnovH1Pfk5H/S/V0BifBmOQyDc9aUh9LaDwkqnChZGIXv8ZHDW8JRlUW0QqyWxTggkbw1A==
dependencies:
invariant "^2.2.4"

Expand Down Expand Up @@ -4051,10 +4051,10 @@ expo-updates@~0.24.12:
fbemitter "^3.0.0"
resolve-from "^5.0.0"

expo@~50.0.14:
version "50.0.14"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.14.tgz#ddcae86aa0ba8d1be3da9ad1bdda23fa539dc97d"
integrity sha512-yLPdxCMVAbmeEIpzzyAuJ79wvr6ToDDtQmuLDMAgWtjqP8x3CGddXxUe07PpKEQgzwJabdHvCLP5Bv94wMFIjQ==
expo@~50.0.15:
version "50.0.15"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.15.tgz#18c5c3ee0125fd42fe828b6791410eed68f7e74a"
integrity sha512-tsyRmMHjA8lPlM7AsqH1smSH8hzmn1+x/vsP+xgbKYJTGtYccdY/wsm6P84VJWeK5peWSVqrWNos+YuPqXKLSQ==
dependencies:
"@babel/runtime" "^7.20.0"
"@expo/cli" "0.17.8"
Expand All @@ -4068,7 +4068,7 @@ expo@~50.0.14:
expo-font "~11.10.3"
expo-keep-awake "~12.8.2"
expo-modules-autolinking "1.10.3"
expo-modules-core "1.11.12"
expo-modules-core "1.11.13"
fbemitter "^3.0.0"
whatwg-url-without-unicode "8.0.0-3"

Expand Down
2 changes: 1 addition & 1 deletion example/expo-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"postinstall": "patch-package"
},
"dependencies": {
"expo": "~50.0.14",
"expo": "~50.0.15",
"expo-constants": "~15.4.5",
"expo-linking": "~6.2.2",
"expo-router": "~3.4.8",
Expand Down
18 changes: 9 additions & 9 deletions example/expo-router/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4176,10 +4176,10 @@ [email protected]:
find-up "^5.0.0"
fs-extra "^9.1.0"

[email protected].12:
version "1.11.12"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.12.tgz#d5c7b3ed7ab57d4fb6885a0d8e10287dcf1ffe5f"
integrity sha512-/e8g4kis0pFLer7C0PLyx98AfmztIM6gU9jLkYnB1pU9JAfQf904XEi3bmszO7uoteBQwSL6FLp1m3TePKhDaA==
[email protected].13:
version "1.11.13"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.13.tgz#a8e63ad844e966dce78dea40b50839af6c3bc518"
integrity sha512-2H5qrGUvmLzmJNPDOnovH1Pfk5H/S/V0BifBmOQyDc9aUh9LaDwkqnChZGIXv8ZHDW8JRlUW0QqyWxTggkbw1A==
dependencies:
invariant "^2.2.4"

Expand Down Expand Up @@ -4210,10 +4210,10 @@ expo-status-bar@~1.11.1:
resolved "https://registry.yarnpkg.com/expo-status-bar/-/expo-status-bar-1.11.1.tgz#a11318741d361048c11db2b16c4364a79a74af30"
integrity sha512-ddQEtCOgYHTLlFUe/yH67dDBIoct5VIULthyT3LRJbEwdpzAgueKsX2FYK02ldh440V87PWKCamh7R9evk1rrg==

expo@~50.0.14:
version "50.0.14"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.14.tgz#ddcae86aa0ba8d1be3da9ad1bdda23fa539dc97d"
integrity sha512-yLPdxCMVAbmeEIpzzyAuJ79wvr6ToDDtQmuLDMAgWtjqP8x3CGddXxUe07PpKEQgzwJabdHvCLP5Bv94wMFIjQ==
expo@~50.0.15:
version "50.0.15"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.15.tgz#18c5c3ee0125fd42fe828b6791410eed68f7e74a"
integrity sha512-tsyRmMHjA8lPlM7AsqH1smSH8hzmn1+x/vsP+xgbKYJTGtYccdY/wsm6P84VJWeK5peWSVqrWNos+YuPqXKLSQ==
dependencies:
"@babel/runtime" "^7.20.0"
"@expo/cli" "0.17.8"
Expand All @@ -4227,7 +4227,7 @@ expo@~50.0.14:
expo-font "~11.10.3"
expo-keep-awake "~12.8.2"
expo-modules-autolinking "1.10.3"
expo-modules-core "1.11.12"
expo-modules-core "1.11.13"
fbemitter "^3.0.0"
whatwg-url-without-unicode "8.0.0-3"

Expand Down
18 changes: 11 additions & 7 deletions example/react-navigation/app/Navigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import { RootStackParamList } from "./types";

import HomeScreen from "./HomeScreen";
import ShareIntentScreen from "./ShareIntentScreen";
import { addStateListener, hasShareIntent } from "expo-share-intent";
import {
addStateListener,
getScheme,
getShareExtensionKey,
hasShareIntent,
} from "expo-share-intent";

const Stack = createNativeStackNavigator();

const PREFIX = Linking.createURL("/");
const PACKAGE_NAME =
Constants.expoConfig?.android?.package ||
Constants.expoConfig?.ios?.bundleIdentifier;
const SHARE_EXTENSION_KEY = `${Constants.expoConfig?.scheme}ShareKey`;

export const navigationRef =
createRef<NavigationContainerRef<RootStackParamList>>();
Expand All @@ -43,7 +47,7 @@ const linking: LinkingOptions<RootStackParamList> = {
// see: https://reactnavigation.org/docs/configuring-links/#advanced-cases
getStateFromPath(path, config) {
// REQUIRED FOR iOS FIRST LAUNCH
if (path.includes(`dataUrl=${SHARE_EXTENSION_KEY}`)) {
if (path.includes(`dataUrl=${getShareExtensionKey()}`)) {
// redirect to the ShareIntent Screen to handle data with the hook
console.debug(
"react-navigation[getStateFromPath] redirect to ShareIntent screen",
Expand All @@ -61,13 +65,13 @@ const linking: LinkingOptions<RootStackParamList> = {
subscribe(listener: (url: string) => void): undefined | void | (() => void) {
console.debug("react-navigation[subscribe]", PREFIX, PACKAGE_NAME);
const onReceiveURL = ({ url }: { url: string }) => {
if (url.includes(SHARE_EXTENSION_KEY)) {
if (url.includes(getShareExtensionKey())) {
// REQUIRED FOR iOS WHEN APP IS IN BACKGROUND
console.debug(
"react-navigation[onReceiveURL] Redirect to ShareIntent Screen",
url,
);
listener(`${Constants.expoConfig?.scheme}://shareintent`);
listener(`${getScheme()}://shareintent`);
} else {
console.debug("react-navigation[onReceiveURL] OPEN URL", url);
listener(url);
Expand All @@ -80,7 +84,7 @@ const linking: LinkingOptions<RootStackParamList> = {
event.value,
);
if (event.value === "pending") {
listener(`${Constants.expoConfig?.scheme}://shareintent`);
listener(`${getScheme()}://shareintent`);
}
});
const urlEventSubscription = Linking.addEventListener("url", onReceiveURL);
Expand All @@ -93,7 +97,7 @@ const linking: LinkingOptions<RootStackParamList> = {
// https://reactnavigation.org/docs/deep-linking/#third-party-integrations
async getInitialURL() {
// REQUIRED FOR ANDROID FIRST LAUNCH
const needRedirect = hasShareIntent(SHARE_EXTENSION_KEY);
const needRedirect = hasShareIntent(getShareExtensionKey());
console.debug(
"react-navigation[getInitialURL] redirect to ShareIntent screen:",
needRedirect,
Expand Down
2 changes: 1 addition & 1 deletion example/react-navigation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@react-navigation/native": "^6.1.16",
"@react-navigation/native-stack": "^6.9.25",
"@react-navigation/stack": "^6.3.28",
"expo": "~50.0.14",
"expo": "~50.0.15",
"expo-splash-screen": "~0.26.4",
"expo-status-bar": "~1.11.1",
"patch-package": "^8.0.0",
Expand Down
18 changes: 9 additions & 9 deletions example/react-navigation/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4009,10 +4009,10 @@ [email protected]:
find-up "^5.0.0"
fs-extra "^9.1.0"

[email protected].12:
version "1.11.12"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.12.tgz#d5c7b3ed7ab57d4fb6885a0d8e10287dcf1ffe5f"
integrity sha512-/e8g4kis0pFLer7C0PLyx98AfmztIM6gU9jLkYnB1pU9JAfQf904XEi3bmszO7uoteBQwSL6FLp1m3TePKhDaA==
[email protected].13:
version "1.11.13"
resolved "https://registry.yarnpkg.com/expo-modules-core/-/expo-modules-core-1.11.13.tgz#a8e63ad844e966dce78dea40b50839af6c3bc518"
integrity sha512-2H5qrGUvmLzmJNPDOnovH1Pfk5H/S/V0BifBmOQyDc9aUh9LaDwkqnChZGIXv8ZHDW8JRlUW0QqyWxTggkbw1A==
dependencies:
invariant "^2.2.4"

Expand All @@ -4028,10 +4028,10 @@ expo-status-bar@~1.11.1:
resolved "https://registry.yarnpkg.com/expo-status-bar/-/expo-status-bar-1.11.1.tgz#a11318741d361048c11db2b16c4364a79a74af30"
integrity sha512-ddQEtCOgYHTLlFUe/yH67dDBIoct5VIULthyT3LRJbEwdpzAgueKsX2FYK02ldh440V87PWKCamh7R9evk1rrg==

expo@~50.0.14:
version "50.0.14"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.14.tgz#ddcae86aa0ba8d1be3da9ad1bdda23fa539dc97d"
integrity sha512-yLPdxCMVAbmeEIpzzyAuJ79wvr6ToDDtQmuLDMAgWtjqP8x3CGddXxUe07PpKEQgzwJabdHvCLP5Bv94wMFIjQ==
expo@~50.0.15:
version "50.0.15"
resolved "https://registry.yarnpkg.com/expo/-/expo-50.0.15.tgz#18c5c3ee0125fd42fe828b6791410eed68f7e74a"
integrity sha512-tsyRmMHjA8lPlM7AsqH1smSH8hzmn1+x/vsP+xgbKYJTGtYccdY/wsm6P84VJWeK5peWSVqrWNos+YuPqXKLSQ==
dependencies:
"@babel/runtime" "^7.20.0"
"@expo/cli" "0.17.8"
Expand All @@ -4045,7 +4045,7 @@ expo@~50.0.14:
expo-font "~11.10.3"
expo-keep-awake "~12.8.2"
expo-modules-autolinking "1.10.3"
expo-modules-core "1.11.12"
expo-modules-core "1.11.13"
fbemitter "^3.0.0"
whatwg-url-without-unicode "8.0.0-3"

Expand Down
6 changes: 3 additions & 3 deletions src/ExpoShareIntentModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
Subscription,
} from "expo-modules-core";

import { SHARE_EXTENSION_KEY } from ".";
import {
ChangeEventPayload,
StateEventPayload,
} from "./ExpoShareIntentModule.types";
import { getShareExtensionKey } from "./utils";

// Import the native module. it will be resolved on native platforms to ExpoShareIntentModule.ts
// It loads the native module object from the JSI or falls back to
Expand All @@ -22,11 +22,11 @@ export function getShareIntent(url = ""): string {
}

export function clearShareIntent(key: string) {
return ExpoShareIntentModule.clearShareIntent(key ?? SHARE_EXTENSION_KEY);
return ExpoShareIntentModule.clearShareIntent(key ?? getShareExtensionKey());
}

export function hasShareIntent(key: string): boolean {
return ExpoShareIntentModule.hasShareIntent(key ?? SHARE_EXTENSION_KEY);
return ExpoShareIntentModule.hasShareIntent(key ?? getShareExtensionKey());
}

const emitter = new EventEmitter(
Expand Down
1 change: 1 addition & 0 deletions src/ExpoShareIntentModule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ShareIntentOptions = {
debug?: boolean;
resetOnBackground?: boolean;
disabled?: boolean;
scheme?: string;
onResetShareIntent?: () => void;
};

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Constants from "expo-constants";

export { ShareIntent } from "./ExpoShareIntentModule.types";

export const SHARE_EXTENSION_KEY = `${Constants.expoConfig?.scheme}ShareKey`;
export {
hasShareIntent,
getShareIntent,
Expand All @@ -13,6 +12,9 @@ export {
} from "./ExpoShareIntentModule";

export { default as useShareIntent } from "./useShareIntent";

export { getScheme, getShareExtensionKey } from "./utils";

export {
ShareIntentProvider,
useShareIntentContext,
Expand Down
25 changes: 10 additions & 15 deletions src/useShareIntent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Constants from "expo-constants";
import { useURL } from "expo-linking";
import { useEffect, useRef, useState } from "react";
import { AppState, Platform } from "react-native";
Expand All @@ -15,6 +14,7 @@ import {
ShareIntentFile,
ShareIntentOptions,
} from "./ExpoShareIntentModule.types";
import { getScheme, getShareExtensionKey } from "./utils";

export const SHAREINTENT_DEFAULTVALUE: ShareIntent = {
files: null,
Expand All @@ -29,12 +29,12 @@ export const SHAREINTENT_OPTIONS_DEFAULT: ShareIntentOptions = {
disabled: false,
};

const IOS_SHARE_TYPE_MAPPING = {
0: "media",
1: "text",
2: "weburl",
3: "file",
};
// const IOS_SHARE_TYPE_MAPPING = {
// 0: "media",
// 1: "text",
// 2: "weburl",
// 3: "file",
// };

const parseShareIntent = (value, options): ShareIntent => {
let result = SHAREINTENT_DEFAULTVALUE;
Expand Down Expand Up @@ -103,8 +103,7 @@ export default function useShareIntent(
const resetShareIntent = (clearNativeModule = true) => {
if (options.disabled) return;
setError(null);
clearNativeModule &&
clearShareIntent(`${Constants.expoConfig?.scheme}ShareKey`);
clearNativeModule && clearShareIntent(getShareExtensionKey(options));
if (shareIntent?.text || shareIntent?.files) {
setSharedIntent(SHAREINTENT_DEFAULTVALUE);
options.onResetShareIntent?.();
Expand All @@ -116,7 +115,7 @@ export default function useShareIntent(
*/
const refreshShareIntent = () => {
options.debug && console.debug("useShareIntent[refresh]", url);
if (url?.startsWith(`${Constants.expoConfig?.scheme}://dataUrl`)) {
if (url?.includes(`${getScheme(options)}://dataUrl=`)) {
// iOS only
getShareIntent(url);
} else if (Platform.OS === "android") {
Expand All @@ -127,11 +126,7 @@ export default function useShareIntent(
useEffect(() => {
if (options.disabled) return;
options.debug &&
console.debug(
"useShareIntent[mount]",
Constants.expoConfig?.scheme,
options,
);
console.debug("useShareIntent[mount]", getScheme(options), options);
refreshShareIntent();
}, [url, options.disabled]);

Expand Down
34 changes: 34 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Constants from "expo-constants";
import { createURL } from "expo-linking";

import { ShareIntentOptions } from "./ExpoShareIntentModule.types";

export const getScheme = (options?: ShareIntentOptions) => {
if (options?.scheme !== undefined) {
options?.debug &&
console.debug("expoShareIntent[scheme] from option:", options.scheme);
return options.scheme;
}
if (Constants.expoConfig?.scheme) {
options?.debug &&
console.debug(
"expoShareIntent[scheme] from expoConfig:",
Constants.expoConfig?.scheme,
);
return Constants.expoConfig?.scheme;
}
const deepLinkUrl = createURL("dataUrl=");
const extracted = deepLinkUrl.match(/^([^:]+)/gi)?.[0] || null;
options?.debug &&
console.debug(
"expoShareIntent[scheme] from linking url:",
deepLinkUrl,
extracted,
);
return extracted;
};

export const getShareExtensionKey = (options?: ShareIntentOptions) => {
const scheme = getScheme(options);
return `${scheme}ShareKey`;
};

0 comments on commit 65c7d66

Please sign in to comment.