All files / owid-grapher/site/blocks ProminentLink.tsx

97.67% Statements 168/172
87.1% Branches 27/31
100% Functions 11/11
97.67% Lines 168/172

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 2141x 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 4x 4x 1x 1x 4x 4x 1x 1x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 3x 1x 1x 1x 1x 1x 4x 4x 4x 4x 3x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x                                                                                    
import * as React from "react"
import ReactDOM from "react-dom"
import * as ReactDOMServer from "react-dom/server"
import { observer } from "mobx-react"
import { computed } from "mobx"
import { union } from "../../clientUtils/Util"
import {
    getSelectedEntityNamesParam,
    migrateSelectedEntityNamesParam,
    setSelectedEntityNamesParam,
} from "../../grapher/core/EntityUrlBuilder"
import { SelectionArray } from "../../grapher/selection/SelectionArray"
import { Url } from "../../clientUtils/urls/Url"
import { EntityName } from "../../coreTable/OwidTableConstants"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faArrowRight } from "@fortawesome/free-solid-svg-icons/faArrowRight"
import { BAKED_BASE_URL } from "../../settings/clientSettings"
 
export const PROMINENT_LINK_CLASSNAME = "wp-block-owid-prominent-link"
 
export enum ProminentLinkStyles {
    thin = "is-style-thin",
    default = "is-style-default",
}
 
export const WITH_IMAGE = "with-image"
 
@observer
export class ProminentLink extends React.Component<{
    href: string
    style: string | null
    title: string | null
    content?: string | null
    image?: string | null
    globalEntitySelection?: SelectionArray
}> {
    @computed get originalUrl(): Url {
        return migrateSelectedEntityNamesParam(Url.fromURL(this.props.href))
    }
 
    @computed private get originalSelectedEntities(): EntityName[] {
        return getSelectedEntityNamesParam(this.originalUrl) ?? []
    }
 
    @computed private get entitiesInGlobalEntitySelection(): EntityName[] {
        return this.props.globalEntitySelection?.selectedEntityNames ?? []
    }
 
    @computed private get updatedUrl(): Url {
        const newEntityList = union(
            this.originalSelectedEntities,
            this.entitiesInGlobalEntitySelection
        )
        return setSelectedEntityNamesParam(this.originalUrl, newEntityList)
    }
 
    @computed private get style(): string {
        return this.props.style || ProminentLinkStyles.default
    }
 
    render() {
        const classes = [
            PROMINENT_LINK_CLASSNAME,
            this.props.image ? WITH_IMAGE : null,
        ]
 
        const renderImage = () => {
            return this.props.image ? (
                <figure
                    dangerouslySetInnerHTML={{
                        __html: this.props.image,
                    }}
                />
            ) : null
        }
 
        const renderContent = () => {
            return this.props.content ? (
                <div
                    className="content"
                    dangerouslySetInnerHTML={{
                        __html: this.props.content,
                    }}
                />
            ) : null
        }
 
        const renderThinStyle = () => {
            return (
                <>
                    {renderImage()}
                    <div className="content-wrapper">
                        {renderContent()}
                        {this.props.title ? (
                            <div className="title">
                                <span
                                    dangerouslySetInnerHTML={{
                                        __html: this.props.title,
                                    }}
                                />
                                <FontAwesomeIcon icon={faArrowRight} />
                            </div>
                        ) : null}
                    </div>
                </>
            )
        }
 
        const renderDefaultStyle = () => {
            return (
                <>
                    {this.props.title ? (
                        <h3>
                            <span
                                dangerouslySetInnerHTML={{
                                    __html: this.props.title,
                                }}
                            />
                            <FontAwesomeIcon icon={faArrowRight} />
                        </h3>
                    ) : null}
                    <div className="content-wrapper">
                        {renderImage()}
                        {renderContent()}
                    </div>
                </>
            )
        }
 
        const target = this.updatedUrl.isGrapher ? { target: "_blank" } : {}
 
        return (
            <div
                className={classes.join(" ")}
                data-no-lightbox
                data-style={this.style}
                data-title={this.props.title}
            >
                <a href={this.updatedUrl.fullUrl} {...target}>
                    {this.style === ProminentLinkStyles.thin
                        ? renderThinStyle()
                        : renderDefaultStyle()}
                </a>
            </div>
        )
    }
}
 
export const renderAuthoredProminentLinks = ($: CheerioStatic) => {
    $("block[type='prominent-link']").each((_, el: CheerioElement) => {
        const $block = $(el)
        const href = $block.find("link-url").text()
        const url = Url.fromURL(href)
 
        const style = $block.attr("style")
        const title = $block.find("title").text()
        const content = $block.find("content").html()
        const image =
            $block.find("figure").html() ||
            (url.isGrapher
                ? `<img src="${BAKED_BASE_URL}/grapher/exports/${url.pathname
                      ?.split("/")
                      .pop()}.svg" />`
                : null)
 
        const rendered = ReactDOMServer.renderToStaticMarkup(
            <div className="block-wrapper">
                <ProminentLink
                    href={href}
                    style={style}
                    title={title}
                    content={content}
                    image={image}
                />
            </div>
        )
 
        $block.after(rendered)
        $block.remove()
    })
}
 
export const hydrateProminentLink = (
    globalEntitySelection?: SelectionArray
) => {
    document
        .querySelectorAll<HTMLElement>(`.${PROMINENT_LINK_CLASSNAME}`)
        .forEach((block) => {
            const href = block.querySelector("a")?.href
            if (!href) return
 
            const style = block.getAttribute("data-style")
            const title = block.getAttribute("data-title")
            const content = block.querySelector(".content")?.innerHTML || null
            const image = block.querySelector("figure")?.innerHTML || null
 
            const rendered = (
                <ProminentLink
                    href={href}
                    style={style}
                    title={title}
                    content={content}
                    image={image}
                    globalEntitySelection={globalEntitySelection}
                />
            )
 
            // this should be a hydrate() call, but it does not work on page
            // load for some reason (works fine when interacting with the global
            // entity selector afterwards). Maybe a race condition with Mobx?
            ReactDOM.render(rendered, block.parentElement)
        })
}