1import type { SharePayload } from "expo-sharing";
2
3const IOS_APP_GROUP_UNAVAILABLE_ERROR_CODE = "ERR_FAILED_TO_RESOLVE_APP_GROUP_ID";
4
5function errorCode(error: unknown): string | null {
6 if (typeof error !== "object" || error === null || !("code" in error)) {
7 return null;
8 }
9 return typeof error.code === "string" ? error.code : null;
10}
11
12/**
13 * Normalizes the native "share into" capability to an empty inbox. Personal
14 * Team builds cannot include the App Group that expo-sharing reads from.
15 */
16export function createIncomingSharePayloadReader(input: {
17 readonly platform: string;
18 readonly readPayloads: () => ReadonlyArray<SharePayload>;
19}): () => ReadonlyArray<SharePayload> {
20 let isUnavailable = false;
21
22 return () => {
23 if (isUnavailable) {
24 return [];
25 }
26
27 try {
28 return input.readPayloads();
29 } catch (error) {
30 if (input.platform === "ios" && errorCode(error) === IOS_APP_GROUP_UNAVAILABLE_ERROR_CODE) {
31 isUnavailable = true;
32 return [];
33 }
34 throw error;
35 }
36 };
37}