import { Prop, toNative } from 'vue-facing-decorator'; import { globalState } from '../../app/global-state'; import PowerduckState from '../../app/powerduck-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { PortalUtils } from '../../common/utils/utils'; import NotificationProvider from '../ui/notification'; import './css/share.css'; interface ShareComponentBindingArgs { } interface ShareComponentArgs { shareUrl: string; shareTitle: string; } export class ShareHelper { static nativeShare(title: string, url: string): void { navigator.share({ title, text: '', url, }) .then(() => { // on success }) .catch((error) => { console.error('Error sharing:', error); }); } } @Component class ShareComponentComponent extends TsxComponent implements ShareComponentArgs { @Prop() shareUrl: string; @Prop() shareTitle: string; FB_APP_ID: string = '144914712372771'; private isInAppBrowser(): boolean { const ua = (navigator.userAgent || '').toLowerCase(); return ua.includes('fbav') || ua.includes('fban') || ua.includes('instagram') || ua.includes('twitter'); } private getUrl(): string { return this.shareUrl; } private isMobile(): boolean { return PortalUtils.isAndroid() || PortalUtils.isIOS(); } private facebookShare(): void { globalState.open(`https://www.facebook.com/sharer/sharer.php?u=${this.getUrl()}`); } private twitterShare(): void { const text = this.shareTitle; const url = `https://twitter.com/intent/tweet?url=${this.getUrl()}&text=${encodeURIComponent(text)}`; globalState.open(url); } private facebookMessengerShare(): void { if (this.isInAppBrowser()) { globalState.alert('ERROR - Unable to open in Facebook browser, open the entry in your system browser and try again'); return; } if (this.isMobile()) { globalState.open(`fb-messenger://share?link=${encodeURIComponent(this.getUrl())}&app_id=${encodeURIComponent(this.FB_APP_ID)}`, '_system'); } else { globalState.open(`https://www.facebook.com/dialog/send?link=${ encodeURIComponent(this.getUrl()) }&app_id=${ encodeURIComponent(this.FB_APP_ID) }&redirect_uri=${ encodeURIComponent(this.getUrl())}`, '_system'); } } private whatsAppShare(): void { if (this.isMobile()) { globalState.open(`whatsapp://send?text=${encodeURIComponent(this.getUrl())}`); } else { globalState.open(`https://web.whatsapp.com/send?text=${encodeURIComponent(this.getUrl())}`); } } private redditShare(): void { globalState.open(`https://reddit.com/submit?url=${encodeURIComponent(this.getUrl())}&title=${encodeURIComponent(this.shareTitle)}`); } private pinterestShare(): void { globalState.open(`https://pinterest.com/pin/create/button/?url=${encodeURIComponent(this.getUrl())}&description=${encodeURIComponent(this.shareTitle)}`); } private linkedInShare(): void { globalState.open(`https://www.linkedin.com/shareArticle?url=${encodeURIComponent(this.getUrl())}&title=${encodeURIComponent(this.shareTitle)}`); } private emailShare(): void { globalState.location.href = `mailto:?body=${encodeURIComponent(this.shareTitle)}

${this.getUrl()}`; } private smsShare(): void { globalState.open(`sms:?body=${encodeURIComponent(`${this.shareTitle}, ${this.getUrl()}`)}`); } private copyToClipboard(): void { (navigator.permissions as any).query({ name: 'clipboard-write' }).then((result) => { if (result.state == 'granted' || result.state == 'prompt') { navigator.clipboard.writeText(this.getUrl()).then(() => { NotificationProvider.showSuccessMessage(PowerduckState.getResourceValue('copyToClipboardSuccess')); }); } }); } private render(h) { if (PortalUtils.hasNativeShare()) { return null; } else { return (
{this.isMobile() && ( )}
); } } } const ShareComponent = toNative(ShareComponentComponent); export default ShareComponent;