about summary refs log tree commit diff
path: root/src/lib/sharing.ts
blob: cdad6afe5036fa791216051500fc4dcd11bf46ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import {Share} from 'react-native'
// import * as Sharing from 'expo-sharing'
import {setStringAsync} from 'expo-clipboard'
// TODO: replace global i18n instance with one returned from useLingui -sfn
import {t} from '@lingui/macro'

import {isAndroid, isIOS} from '#/platform/detection'
import * as Toast from '#/view/com/util/Toast'

/**
 * This function shares a URL using the native Share API if available, or copies it to the clipboard
 * and displays a toast message if not (mostly on web)
 * @param {string} url - A string representing the URL that needs to be shared or copied to the
 * clipboard.
 */
export async function shareUrl(url: string) {
  if (isAndroid) {
    await Share.share({message: url})
  } else if (isIOS) {
    await Share.share({url})
  } else {
    // React Native Share is not supported by web. Web Share API
    // has increasing but not full support, so default to clipboard
    setStringAsync(url)
    Toast.show(t`Copied to clipboard`, 'clipboard-check')
  }
}

/**
 * This function shares a text using the native Share API if available, or copies it to the clipboard
 * and displays a toast message if not (mostly on web)
 *
 * @param {string} text - A string representing the text that needs to be shared or copied to the
 * clipboard.
 */
export async function shareText(text: string) {
  if (isAndroid || isIOS) {
    await Share.share({message: text})
  } else {
    await setStringAsync(text)
    Toast.show(t`Copied to clipboard`, 'clipboard-check')
  }
}