All files / owid-grapher/grapher/controls ShareMenu.tsx

31.19% Statements 68/218
100% Branches 0/0
0% Functions 0/20
31.19% Lines 68/218

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 2411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x 1x     1x 1x     1x 1x     1x 1x     1x 1x     1x 1x       1x 1x     1x 1x     1x 1x               1x 1x                           1x 1x         1x 1x               1x 1x             1x 1x                                                                                                                                             1x 1x 1x                                                                                      
import { observer } from "mobx-react"
import React from "react"
import { computed, action } from "mobx"
import copy from "copy-to-clipboard"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faTwitter } from "@fortawesome/free-brands-svg-icons/faTwitter"
import { faFacebook } from "@fortawesome/free-brands-svg-icons/faFacebook"
import { faCode } from "@fortawesome/free-solid-svg-icons/faCode"
import { faShareAlt } from "@fortawesome/free-solid-svg-icons/faShareAlt"
import { faCopy } from "@fortawesome/free-solid-svg-icons/faCopy"
import { faEdit } from "@fortawesome/free-solid-svg-icons/faEdit"
 
export interface ShareMenuManager {
    slug?: string
    currentTitle?: string
    canonicalUrl?: string
    embedUrl?: string
    embedDialogAdditionalElements?: React.ReactElement
    editUrl?: string
    addPopup: (popup: any) => void
    removePopup: (popup: any) => void
}
 
interface ShareMenuProps {
    manager: ShareMenuManager
    onDismiss: () => void
}
 
interface ShareMenuState {
    copied: boolean
}
 
@observer
export class ShareMenu extends React.Component<ShareMenuProps, ShareMenuState> {
    dismissable = true
 
    constructor(props: ShareMenuProps) {
        super(props)

        this.state = {
            copied: false,
        }
    }
 
    @computed get manager(): ShareMenuManager {
        return this.props.manager
    }
 
    @computed get title(): string {
        return this.manager.currentTitle ?? ""
    }
 
    @computed get isDisabled(): boolean {
        return !this.manager.slug
    }
 
    @computed get canonicalUrl(): string | undefined {
        return this.manager.canonicalUrl
    }
 
    @action.bound dismiss(): void {
        this.props.onDismiss()
    }
 
    @action.bound onClickSomewhere(): void {
        if (this.dismissable) this.dismiss()
        else this.dismissable = true
    }
 
    componentDidMount(): void {
        document.addEventListener("click", this.onClickSomewhere)
    }
 
    componentWillUnmount(): void {
        document.removeEventListener("click", this.onClickSomewhere)
    }
 
    @action.bound onEmbed(): void {
        if (!this.canonicalUrl) return

        this.manager.addPopup(
            <EmbedMenu key="EmbedMenu" manager={this.manager} />
        )
        this.dismiss()
    }
 
    @action.bound async onNavigatorShare(): Promise<void> {
        if (!this.canonicalUrl || !navigator.share) return

        const shareData = {
            title: this.title,
            url: this.canonicalUrl,
        }

        try {
            await navigator.share(shareData)
        } catch (err) {
            console.error("couldn't share using navigator.share", err)
        }
    }
 
    @action.bound onCopy(): void {
        if (!this.canonicalUrl) return

        if (copy(this.canonicalUrl)) this.setState({ copied: true })
    }
 
    @computed get twitterHref(): string {
        let href =
            "https://twitter.com/intent/tweet/?text=" +
            encodeURIComponent(this.title)
        if (this.canonicalUrl)
            href += "&url=" + encodeURIComponent(this.canonicalUrl)
        return href
    }
 
    @computed get facebookHref(): string {
        let href =
            "https://www.facebook.com/dialog/share?app_id=1149943818390250&display=page"
        if (this.canonicalUrl)
            href += "&href=" + encodeURIComponent(this.canonicalUrl)
        return href
    }
 
    render(): JSX.Element {
        const { twitterHref, facebookHref, isDisabled, manager } = this
        const { editUrl } = manager

        return (
            <div
                className={"ShareMenu" + (isDisabled ? " disabled" : "")}
                onClick={action(() => (this.dismissable = false))}
            >
                <h2>Share</h2>
                <a
                    className="btn"
                    target="_blank"
                    title="Tweet a link"
                    data-track-note="chart-share-twitter"
                    href={twitterHref}
                    rel="noopener"
                >
                    <FontAwesomeIcon icon={faTwitter} /> Twitter
                </a>
                <a
                    className="btn"
                    target="_blank"
                    title="Share on Facebook"
                    data-track-note="chart-share-facebook"
                    href={facebookHref}
                    rel="noopener"
                >
                    <FontAwesomeIcon icon={faFacebook} /> Facebook
                </a>
                <a
                    className="btn"
                    title="Embed this visualization in another HTML document"
                    data-track-note="chart-share-embed"
                    onClick={this.onEmbed}
                >
                    <FontAwesomeIcon icon={faCode} /> Embed
                </a>
                {"share" in navigator && (
                    <a
                        className="btn"
                        title="Share this visualization with an app on your device"
                        data-track-note="chart-share-navigator"
                        onClick={this.onNavigatorShare}
                    >
                        <FontAwesomeIcon icon={faShareAlt} /> Share via&hellip;
                    </a>
                )}
                <a
                    className="btn"
                    title="Copy link to clipboard"
                    data-track-note="chart-share-copylink"
                    onClick={this.onCopy}
                >
                    <FontAwesomeIcon icon={faCopy} />
                    {this.state.copied ? "Copied!" : "Copy link"}
                </a>
                {editUrl && (
                    <a
                        className="btn"
                        target="_blank"
                        title="Edit chart"
                        href={editUrl}
                        rel="noopener"
                    >
                        <FontAwesomeIcon icon={faEdit} /> Edit
                    </a>
                )}
            </div>
        )
    }
}
 
@observer
class EmbedMenu extends React.Component<{
    manager: ShareMenuManager
}> {
    dismissable = true

    @action.bound onClickSomewhere(): void {
        if (this.dismissable) this.manager.removePopup(EmbedMenu)
        else this.dismissable = true
    }

    @computed get manager(): ShareMenuManager {
        return this.props.manager
    }

    @action.bound onClick(): void {
        this.dismissable = false
    }

    componentDidMount(): void {
        document.addEventListener("click", this.onClickSomewhere)
    }
 
    componentWillUnmount(): void {
        document.removeEventListener("click", this.onClickSomewhere)
    }
 
    render(): JSX.Element {
        const url = this.manager.embedUrl ?? this.manager.canonicalUrl
        return (
            <div className="embedMenu" onClick={this.onClick}>
                <h2>Embed</h2>
                <p>Paste this into any HTML page:</p>
                <textarea
                    readOnly={true}
                    onFocus={(evt): void => evt.currentTarget.select()}
                    value={`<iframe src="${url}" loading="lazy" style="width: 100%; height: 600px; border: 0px none;"></iframe>`}
                />
                {this.manager.embedDialogAdditionalElements}
            </div>
        )
    }
}