import device from "current-device"; import * as _ from "lodash"; import * as React from "react"; import { setImagePossiblyRevealedDownloadSheetListeners } from "../../utilities/interactions"; import { initCopy, initFb, initTwitter } from "../../utilities/social"; import { Button, ButtonSize, ButtonType } from "../Button"; import { Sheet, ISheetProps } from "../Sheet"; import { Image } from "../Image"; import { getImageSrc } from "../../utilities/image"; /** * The shareable object that is used to build urls. */ export interface IShareable { text?: string; url?: string; } export enum SocialPlatform { Twitter = "Twitter", Facebook = "Facebook", Instagram = "Instagram", LinkedIn = "LinkedIn", } export enum ClientPlatform { Email = "Email", Permalink = "Permalink", Sms = "SMS", Phone = "Phone", Raw = "Raw", } interface IShareProps { sheet: ISheetProps; onShare?: (platform: ClientPlatform | SocialPlatform) => void; title?: string; subtitle?: string; onAction?: () => void; socials?: SocialPlatform[]; clients?: ClientPlatform[]; shareable?: IShareable; imagePrefix?: string; } interface IShareState { copied?: boolean; } const defaultShareState: IShareState = { copied: false, }; export class Share extends React.Component { public state: IShareState = defaultShareState; public componentDidMount() { initFb(); initTwitter(); initCopy(".share__shareable--permalink", () => `${this.props.shareable.url + "?utm_source=share_sheet%26utm_medium=copy_permalink"}`, () => { const confirmationNode = document.querySelector(".share__shareable-text-confirmation"); confirmationNode.classList.add("share__shareable-text-confirmation--active"); this.setState({ copied: true }, () => { try { this.props.onShare(ClientPlatform.Permalink); } catch (err) {} }) setTimeout(() => { confirmationNode.classList.remove("share__shareable-text-confirmation--active"); }, 5000); }); this.initTapAndHoldListener(); } public render() { return (
{this.props.title}
{_.isString(this.props.subtitle) && (
{this.props.subtitle}
)}
{!_.isEmpty(this.props.socials) && !_.isEmpty(this.props.shareable) && this.props.socials.map((platform) => (
{platform}
))} {!_.isEmpty(this.props.socials) && !_.isEmpty(this.props.clients) && (
)} {!_.isEmpty(this.props.clients) && !_.isEmpty(this.props.shareable) && this.props.clients.map((client) => this.renderShareableClient(client))}
); } private renderShareableClient = (client: ClientPlatform): React.ReactElement => { switch (client) { case ClientPlatform.Permalink: return (
Copy to Clipboard
{this.state.copied && "Copied!"}
); case ClientPlatform.Raw: return (
Tap & Hold for More Options
{this.props.shareable.url}
); default: return (
{client}
); } } private buildLink = (platform: SocialPlatform | ClientPlatform): string => { switch (platform) { case SocialPlatform.Twitter: return `https://twitter.com/intent/tweet?url=${encodeURI(this.props.shareable.url)}&text=${encodeURI(this.props.shareable.text)}` case SocialPlatform.Facebook: return `https://www.facebook.com/sharer/sharer.php?u=${encodeURI(this.props.shareable.url)}`; case ClientPlatform.Sms: if (device.ios()) { return `sms:&body=${this.props.shareable.url + "?utm_source=share_sheet%26utm_medium=sms"}`; } if (device.android()) { return `sms:?body=${this.props.shareable.url + "?utm_source=share_sheet%26utm_medium=sms"}`; } case ClientPlatform.Email: if (device.mobile()) { return `mailto:?subject=Photos from ${ this.props.shareable.text }&body=View and download photos anytime: ${ this.props.shareable.url }`; } else { return `mailto:?subject=Photos from ${ this.props.shareable.text }&body=View and download photos anytime: ${this.props.shareable.url}`; } case ClientPlatform.Permalink: return "#"; case ClientPlatform.Raw: default: return `${this.props.shareable.url + "?utm_source=share_sheet%26utm_medium=tap_hold_share"}`; } } private initTapAndHoldListener = () => { const rawShareableNode = document.querySelector(".share__shareable--raw"); if (_.isFunction(this.props.onShare) && rawShareableNode) { // @ts-ignore setImagePossiblyRevealedDownloadSheetListeners(rawShareableNode, () => { this.props.onShare(ClientPlatform.Raw); }); } } }