-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathGitHubLink.jsx
69 lines (60 loc) · 1.46 KB
/
GitHubLink.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import { IconGitHub, IconBranch } from './icons';
const BASE = 'https://github.com';
const GitHubLink = ({
owner,
repository,
text,
branch = null,
sha = null,
icon = 'repo',
isButton = false,
}) => {
const baseHref = `${BASE}/${owner}/${repository}`;
let href = baseHref;
let title = 'View repository on GitHub';
if (branch) {
href = `${baseHref}/tree/${encodeURIComponent(branch)}`;
title = 'View branch on GitHub';
} else if (sha) {
href = `${baseHref}/commit/${sha}`;
title = 'View commit on GitHub';
}
function chooseIcon(iconStr) {
switch (iconStr) {
case 'branch':
return <IconBranch />;
case 'commit':
case 'sha':
// use no icon for these types of links
return '';
case 'repo':
default:
return <IconGitHub />;
}
}
const isButtonClassName = isButton ? 'usa-button usa-button--outline' : 'usa-link';
return (
<a
className={`${icon}-link ${isButtonClassName}`}
href={href}
title={title}
target="_blank"
rel="noopener noreferrer"
>
{chooseIcon(icon)}
{text}
</a>
);
};
GitHubLink.propTypes = {
owner: PropTypes.string.isRequired,
repository: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
branch: PropTypes.string,
isButton: PropTypes.bool,
sha: PropTypes.string,
icon: PropTypes.string,
};
export default GitHubLink;