forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitRefTag.tsx
71 lines (63 loc) · 2.3 KB
/
GitRefTag.tsx
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
import GithubCircleIcon from 'mdi-react/GithubCircleIcon'
import SourceBranchIcon from 'mdi-react/SourceBranchIcon'
import TagIcon from 'mdi-react/TagIcon'
import React from 'react'
import * as GQL from '../../../shared/src/graphql/schema'
interface Props {
gitRef: GQL.IGitRef
/**
* Called when the mousedown event is triggered on the element.
*/
onMouseDown?: () => void
}
export const GitRefTag: React.FunctionComponent<Props> = ({ gitRef, onMouseDown }: Props) => {
// TODO(sqs): make not github specific
const githubRepoURL = gitRef.repository.name.startsWith('github.com/')
? `https://${gitRef.repository.name}`
: undefined
const abbrevName = gitRef.name.slice(gitRef.prefix.length)
let kind = ''
let url = githubRepoURL || ''
let Icon: React.ComponentType<{ className?: string }> | undefined
switch (gitRef.prefix) {
case 'refs/heads/':
kind = 'branch'
url = url && `${url}/compare/${encodeURIComponent(abbrevName)}`
Icon = SourceBranchIcon
break
case 'refs/tags/':
kind = 'tag'
url = url && `${url}/releases/tag/${encodeURIComponent(abbrevName)}`
Icon = TagIcon
break
case 'refs/pull/':
if (gitRef.name.endsWith('/head')) {
kind = 'pull request'
url = url && `${url}/pull/${abbrevName.split('/')[0]}`
Icon = GithubCircleIcon
} else if (gitRef.name.endsWith('/merge')) {
kind = 'pull request merge'
url = url && `${url}/pull/${abbrevName.split('/')[0]}`
Icon = GithubCircleIcon
}
break
}
// Render <a> if there's a canonical URL for the Git ref. Otherwise render <span>.
const children: (React.ReactChild | undefined)[] = [
Icon && <Icon key={1} className="icon-inline" />,
<span key={2} className="git-ref-tag__display-name">
{gitRef.displayName}
</span>,
]
const props = {
title: `${gitRef.name} ${kind ? `(${kind})` : ''}`,
className: 'git-ref-tag',
}
return url ? (
<a href={url} {...props} onMouseDown={onMouseDown}>
{children}
</a>
) : (
<span {...props}>{children}</span>
)
}