({
link,
text,
logObject,
cid,
}: UseShareOrCopyLinkProps)
| 15 | cid?: ReferralCampaignKey; |
| 16 | } |
| 17 | export function useShareOrCopyLink({ |
| 18 | link, |
| 19 | text, |
| 20 | logObject, |
| 21 | cid, |
| 22 | }: UseShareOrCopyLinkProps): ReturnType<typeof useCopyLink> { |
| 23 | const { logEvent } = useLogContext(); |
| 24 | const [copying, copyLink] = useCopyLink(); |
| 25 | const { getShortUrl } = useGetShortUrl(); |
| 26 | |
| 27 | const onShareOrCopy: CopyNotifyFunction = async () => { |
| 28 | const shortLink = cid ? await getShortUrl(link, cid) : link; |
| 29 | const logShareEvent = (provider: ShareProvider): void => { |
| 30 | if (!logObject) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | logEvent(logObject(provider)); |
| 35 | }; |
| 36 | |
| 37 | if (shouldUseNativeShare()) { |
| 38 | try { |
| 39 | await navigator.share({ |
| 40 | text: `${text}\n${shortLink}`, |
| 41 | }); |
| 42 | logShareEvent(ShareProvider.Native); |
| 43 | } catch (err) { |
| 44 | // Do nothing |
| 45 | } |
| 46 | } else { |
| 47 | logShareEvent(ShareProvider.CopyLink); |
| 48 | copyLink({ link: shortLink }); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | return [copying, onShareOrCopy]; |
| 53 | } |
no test coverage detected