-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathbyline.js
171 lines (158 loc) · 4.9 KB
/
byline.js
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
import React from "react";
import { connect } from "react-redux";
import { withTranslation } from 'react-i18next';
import styled from 'styled-components';
import { headerFont } from "../../globalStyles";
/**
* React component for the byline of the current dataset.
* This details (non-dynamic) information about the dataset, such as the
* maintainers, source, data provenance etc.
*/
@connect((state) => {
return {
metadata: state.metadata
};
})
class Byline extends React.Component {
render() {
const { t } = this.props;
return (
<>
{renderAvatar(t, this.props.metadata)}
{renderBuildInfo(t, this.props.metadata)}
{renderMaintainers(t, this.props.metadata)}
{renderDataProvenance(t, this.props.metadata)}
</>
);
}
}
const AvatarImg = styled.img`
margin-right: 5px;
margin-bottom: 2px;
`;
/**
* Renders the GitHub avatar of the current dataset for datasets with a `buildUrl`
* which is a GitHub repo. The avatar image is fetched from GitHub (by the client).
*/
function renderAvatar(t, metadata) {
const repo = metadata.buildUrl;
if (typeof repo === 'string') {
const match = repo.match(/(https?:\/\/)?(www\.)?github.com\/([^/]+)/);
if (match) {
return (
<AvatarImg alt="avatar" width="28" src={`https://github.com/${match[3]}.png?size=200`}/>
);
}
}
return null;
}
/**
* Returns a React component detailing the source of the build (pipeline).
* Renders a <span> containing "Built with X", where X derives from `metadata.buildUrl`
*/
function renderBuildInfo(t, metadata) {
if (Object.prototype.hasOwnProperty.call(metadata, "buildUrl")) {
const repo = metadata.buildUrl;
if (typeof repo === 'string') {
if (repo.startsWith("https://") || repo.startsWith("http://") || repo.startsWith("www.")) {
return (
<span>
{t("Built with")}
{" "}
<Link url={repo}>
{repo.replace(/^(http[s]?:\/\/)/, "").replace(/^www\./, "").replace(/^github.com\//, "")}
</Link>
{". "}
</span>
);
}
}
}
return null;
}
/**
* Returns a React component detailing the maintainers of the build (pipeline).
* Renders a <span> containing "Maintained by X", where X derives from `metadata.maintainers`
*/
function renderMaintainers(t, metadata) {
let maintainersArray;
if (Object.prototype.hasOwnProperty.call(metadata, "maintainers")) {
maintainersArray = metadata.maintainers;
if (Array.isArray(maintainersArray) && maintainersArray.length) {
return (
<span>
{t("Maintained by") + " "}
{maintainersArray.map((m, i) => (
<React.Fragment key={m.name}>
{m.url ? <Link url={m.url}>{m.name}</Link> : m.name}
{i === maintainersArray.length-1 ? "" : i === maintainersArray.length-2 ? " and " : ", "}
</React.Fragment>
))}
{". "}
</span>
);
}
}
return null;
}
/**
* Returns a React component detailing the data provenance of the build (pipeline).
* Renders a <span> containing "Enabled by data from X", where X derives from `metadata.dataProvenance`
* Note that this function includes logic to special-case certain values which may appear there.
*/
function renderDataProvenance(t, metadata) {
if (!Array.isArray(metadata.dataProvenance)) return null;
const sources = metadata.dataProvenance
.filter((source) => typeof source === "object")
.filter((source) => Object.prototype.hasOwnProperty.call(source, "name"))
.map((source) => {
if (source.name.toUpperCase() === "GISAID") { // SPECIAL CASE
return (<Link url="https://www.gisaid.org" key={source.name}>
<img key={source.name} src="https://www.gisaid.org/fileadmin/gisaid/img/schild.png" alt="gisaid-logo" width="65"/>
</Link>);
}
const url = parseUrl(source.url);
if (url) {
return <Link url={url} key={source.name}>{source.name}</Link>;
}
return source.name;
});
if (!sources.length) return null;
return (
<span>
{t("Enabled by data from") + " "}
{makePrettyList(sources)}
{"."}
</span>
);
}
function makePrettyList(els) {
if (els.length<2) return els;
return Array.from({length: els.length*2-1})
.map((_, idx) => idx%2===0 ? els[idx/2] : idx===els.length*2-3 ? " and " : ", ");
}
/**
* Attempts to parse a url. Returns a valid-looking URL string or `false`.
*/
function parseUrl(potentialUrl) {
try {
const urlObj = new URL(potentialUrl);
return urlObj.href;
} catch (err) {
return false;
}
}
const BylineLink = styled.a`
font-family: ${headerFont};
font-size: 15;
font-weight: 500;
`;
function Link({url, children}) {
return (
<BylineLink rel="noopener noreferrer" href={url} target="_blank">
{children}
</BylineLink>
);
}
const WithTranslation = withTranslation()(Byline);
export default WithTranslation;