Skip to content

Commit

Permalink
refactor: clean TS errors, add TS rules, cmn types (#45)
Browse files Browse the repository at this point in the history
* refactor: clean TS errors, add TS rules, cmn types

---------

Co-authored-by: Anselme Chorein <[email protected]>
Co-authored-by: achorein <[email protected]>
  • Loading branch information
3 people committed Apr 19, 2024
1 parent d870601 commit f78fc3c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 45 deletions.
105 changes: 73 additions & 32 deletions src/ExpoShareIntentModule.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,104 @@ export type StateEventPayload = {
value: "pending" | "none";
};

/**
* Options for configuring the `useShareIntent` hook.
*/
export type ShareIntentOptions = {
/**
* If `true`, includes additional logs for debugging.
* @default false
*/
debug?: boolean;
/**
* If `true`, resets the shared content when the
* app goes into the background / foreground.
* @default true
*/
resetOnBackground?: boolean;
/**
* If `true`, disables shared intent.
* @default false
*/
disabled?: boolean;
/**
* Optional force application scheme to retreive ShareIntent on iOS.
*/
scheme?: string;
/**
* Optional callback function that is triggered when the shared media resets.
*/
onResetShareIntent?: () => void;
};

export type ShareIntentMeta = {
title?: string;
};

export type ShareIntent = {
/**
* Base type for what shared content is common between both platforms.
*/
interface BaseShareIntent {
meta?: ShareIntentMeta;
text?: string | null;
}

/**
* Shared intent to represent both platforms.
*/
export type ShareIntent = BaseShareIntent & {
files: ShareIntentFile[] | null;
text: string | null;
webUrl: string | null;
type: "media" | "file" | "text" | "weburl" | null;
meta?: ShareIntentMeta;
webUrl: string | null;
};

export interface ShareIntentFile {
path: string;
mimeType: string;
fileName: string;
size: number | null;
}

interface BaseNativeShareIntent {
text?: string;
meta?: ShareIntentMeta;
/**
* Shared intent type for Android.
*/
export interface AndroidShareIntent extends BaseShareIntent {
files?: AndroidShareIntentFile[];
type: "file" | "text";
}

export type IosShareIntent = BaseNativeShareIntent & {
/**
* Shared intent type for iOS.
*/
export interface IosShareIntent extends BaseShareIntent {
files?: IosShareIntentFile[];
type: "media" | "file" | "text" | "weburl";
};
}

export interface IosShareIntentFile {
path: string;
type: string;
/**
* ShareIntentFile that is common among both platforms
*/
export type ShareIntentFile = {
fileName: string;
mimeType: string;
fileSize?: number;
}

export type AndroidShareIntent = BaseNativeShareIntent & {
files?: AndroidShareIntentFile[];
type: "file" | "text";
path: string;
size: number | null;
};

/**
* ShareIntentFile in iOS
*/
export interface IosShareIntentFile {
fileSize?: number; // in octet
fileName: string; // original filename
mimeType: string; // ex: image/png
path: string; // computed full path of file
type: "0" | "1" | "2" | "3"; // native type ("0": media, "1": text, "2": weburl, "3": file)
}

/**
* ShareIntentFile in Android
*/
export interface AndroidShareIntentFile {
fileName: string;
filePath: string;
mimeType: string;
fileSize?: string;
contentUri: string;
contentUri: string; // original android uri of file
mimeType: string; // ex: image/png
fileName: string; // original filename
filePath: string; // computed full path of file
fileSize?: string; // in octet
}

export type NativeShareIntent = IosShareIntent | AndroidShareIntent;
export type NativeShareIntentFile = IosShareIntentFile | AndroidShareIntentFile;
export type NativeShareIntent = AndroidShareIntent | IosShareIntent;
export type NativeShareIntentFile = AndroidShareIntentFile | IosShareIntentFile;
39 changes: 26 additions & 13 deletions src/useShareIntent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getShareIntent,
} from "./ExpoShareIntentModule";
import {
NativeShareIntent,
AndroidShareIntent,
IosShareIntent,
ShareIntent,
ShareIntentFile,
ShareIntentOptions,
Expand All @@ -36,20 +37,27 @@ export const SHAREINTENT_OPTIONS_DEFAULT: ShareIntentOptions = {
// 3: "file",
// };

const parseShareIntent = (value, options): ShareIntent => {
const parseShareIntent = (
value: string | AndroidShareIntent,
options: ShareIntentOptions,
): ShareIntent => {
let result = SHAREINTENT_DEFAULTVALUE;
if (!value) return result;
let shareIntent: NativeShareIntent;
let shareIntent;
// ios native module send a raw string of the json, try to parse it
if (typeof value === "string") {
shareIntent = JSON.parse(value.replaceAll("\n", "\\n")); // iOS
shareIntent = JSON.parse(value.replaceAll("\n", "\\n")) as IosShareIntent; // iOS
} else {
shareIntent = value; // Android
}

if (shareIntent.text) {
// Try to find the webURL in the SharedIntent text
const webUrl =
shareIntent.text.match(
/[(http(s)?)://(www.)?-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/gi,
)?.[0] || null;

result = {
...SHAREINTENT_DEFAULTVALUE,
text: shareIntent.text,
Expand All @@ -60,33 +68,38 @@ const parseShareIntent = (value, options): ShareIntent => {
},
};
} else {
// Ensure we got a valid file. some array value are emply
const files =
// @ts-ignore
shareIntent?.files?.filter((f: any) => f.path || f.contentUri) || [];
shareIntent?.files?.filter((file: any) => file.path || file.contentUri) ||
[];
const isMedia = files.every(
(f) => f.mimeType.startsWith("image/") || f.mimeType.startsWith("video/"),
(file) =>
file.mimeType.startsWith("image/") ||
file.mimeType.startsWith("video/"),
);
result = {
...SHAREINTENT_DEFAULTVALUE,
files: shareIntent?.files
? // @ts-ignore
shareIntent.files.reduce((acc: ShareIntentFile[], f: any) => {
if (!f.path && !f.contentUri) return acc;
shareIntent.files.reduce((acc: ShareIntentFile[], file: any) => {
if (!file.path && !file.contentUri) return acc;
return [
...acc,
{
path: f.path || f.contentUri || null,
mimeType: f.mimeType || null,
fileName: f.fileName || null,
size: f.fileSize ? Number(f.fileSize) : null,
path: file.path || file.contentUri || null,
mimeType: file.mimeType || null,
fileName: file.fileName || null,
size: file.fileSize ? Number(file.fileSize) : null,
},
];
}, [])
: null,
type: isMedia ? "media" : "file",
};
}
options.debug && console.debug("useShareIntent[parsed] ", result);
options.debug &&
console.debug("useShareIntent[parsed] ", JSON.stringify(result, null, 4));
return result;
};

Expand Down

0 comments on commit f78fc3c

Please sign in to comment.