forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitRef.tsx
143 lines (134 loc) · 4.74 KB
/
GitRef.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
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
import * as React from 'react'
import { Link } from 'react-router-dom'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { LinkOrSpan } from '../../../shared/src/components/LinkOrSpan'
import { gql } from '../../../shared/src/graphql/graphql'
import * as GQL from '../../../shared/src/graphql/schema'
import { createAggregateError } from '../../../shared/src/util/errors'
import { memoizeObservable } from '../../../shared/src/util/memoizeObservable'
import { numberWithCommas } from '../../../shared/src/util/strings'
import { queryGraphQL } from '../backend/graphql'
import { Timestamp } from '../components/time/Timestamp'
interface GitRefNodeProps {
node: GQL.IGitRef
/** Link URL; if undefined, node.url is used. */
url?: string
/** Whether the top-level element is a link. */
rootIsLink?: boolean
children?: React.ReactNode
}
export const GitRefNode: React.FunctionComponent<GitRefNodeProps> = ({ node, url, rootIsLink, children }) => {
const mostRecentSig =
node.target.commit &&
(node.target.commit.committer && node.target.commit.committer.date > node.target.commit.author.date
? node.target.commit.committer
: node.target.commit.author)
const behindAhead = node.target.commit && node.target.commit.behindAhead
url = url !== undefined ? url : node.url
return (
<LinkOrSpan key={node.id} className="git-ref-node list-group-item" to={url}>
<span>
{rootIsLink ? (
<code className="git-ref-tag-2">{node.displayName}</code>
) : (
<Link className="git-ref-tag-2" to={url}>
<code>{node.displayName}</code>
</Link>
)}
{mostRecentSig && (
<small className="text-muted pl-2">
Updated <Timestamp date={mostRecentSig.date} />{' '}
{mostRecentSig.person && <>by {mostRecentSig.person.displayName}</>}
</small>
)}
</span>
{behindAhead && (
<small className="text-muted">
{numberWithCommas(behindAhead.behind)} behind, {numberWithCommas(behindAhead.ahead)} ahead
</small>
)}
{children}
</LinkOrSpan>
)
}
export const gitRefFragment = gql`
fragment GitRefFields on GitRef {
id
displayName
name
abbrevName
url
target {
commit {
author {
...SignatureFields
}
committer {
...SignatureFields
}
behindAhead(revspec: "HEAD") @include(if: $withBehindAhead) {
behind
ahead
}
}
}
}
fragment SignatureFields on Signature {
person {
displayName
user {
username
}
}
date
}
`
export const queryGitRefs = memoizeObservable(
(args: {
repo: GQL.ID
first?: number
query?: string
type: GQL.GitRefType
withBehindAhead?: boolean
}): Observable<GQL.IGitRefConnection> =>
queryGraphQL(
gql`
query RepositoryGitRefs(
$repo: ID!
$first: Int
$query: String
$type: GitRefType!
$withBehindAhead: Boolean!
) {
node(id: $repo) {
... on Repository {
gitRefs(first: $first, query: $query, type: $type, orderBy: AUTHORED_OR_COMMITTED_AT) {
nodes {
...GitRefFields
}
totalCount
pageInfo {
hasNextPage
}
}
}
}
}
${gitRefFragment}
`,
{
...args,
withBehindAhead:
args.withBehindAhead !== undefined ? args.withBehindAhead : args.type === GQL.GitRefType.GIT_BRANCH,
}
).pipe(
map(({ data, errors }) => {
if (!data || !data.node || !(data.node as GQL.IRepository).gitRefs) {
throw createAggregateError(errors)
}
return (data.node as GQL.IRepository).gitRefs
})
),
args => `${args.repo}:${String(args.first)}:${String(args.query)}:${args.type}`
)