UNPKG

1.18 kBPlain TextView Raw
1import qs from 'query-string';
2
3import { MailComposerOptions, MailComposerResult, MailComposerStatus } from './MailComposer.types';
4
5function removeNullishValues(obj) {
6 for (const propName in obj) {
7 if (obj[propName] == null) {
8 delete obj[propName];
9 }
10 }
11 return obj;
12}
13
14function checkValue(value?: string[] | string): string | null {
15 if (!value) {
16 return null;
17 }
18
19 const arr = Array.isArray(value) ? value : [value];
20 return arr.join(',');
21}
22
23export default {
24 get name(): string {
25 return 'ExpoMailComposer';
26 },
27 async composeAsync(options: MailComposerOptions): Promise<MailComposerResult> {
28 const email = removeNullishValues({
29 cc: checkValue(options.ccRecipients),
30 bcc: checkValue(options.bccRecipients),
31 subject: options.subject,
32 body: options.body,
33 });
34
35 const query = qs.stringify(email);
36 const queryComponent = query ? '?' + query : '';
37 const to = checkValue(options.recipients) || '';
38 const mailto = `mailto:${to}${queryComponent}`;
39
40 window.open(mailto);
41
42 return { status: MailComposerStatus.UNDETERMINED };
43 },
44 async isAvailableAsync(): Promise<boolean> {
45 return true;
46 },
47};