forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreePage.tsx
364 lines (351 loc) · 14.9 KB
/
TreePage.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { LoadingSpinner } from '@sourcegraph/react-loading-spinner'
import * as H from 'history'
import FolderIcon from 'mdi-react/FolderIcon'
import HistoryIcon from 'mdi-react/HistoryIcon'
import SourceBranchIcon from 'mdi-react/SourceBranchIcon'
import SourceCommitIcon from 'mdi-react/SourceCommitIcon'
import SourceRepositoryIcon from 'mdi-react/SourceRepositoryIcon'
import TagIcon from 'mdi-react/TagIcon'
import UserIcon from 'mdi-react/UserIcon'
import React, { useState, useMemo, useCallback, useEffect } from 'react'
import { Link } from 'react-router-dom'
import { Observable } from 'rxjs'
import { catchError, map } from 'rxjs/operators'
import { ActionItem } from '../../../shared/src/actions/ActionItem'
import { ActionsContainer } from '../../../shared/src/actions/ActionsContainer'
import { ContributableMenu } from '../../../shared/src/api/protocol'
import { ActivationProps } from '../../../shared/src/components/activation/Activation'
import { displayRepoName } from '../../../shared/src/components/RepoFileLink'
import { ExtensionsControllerProps } from '../../../shared/src/extensions/controller'
import { gql } from '../../../shared/src/graphql/graphql'
import * as GQL from '../../../shared/src/graphql/schema'
import { PlatformContextProps } from '../../../shared/src/platform/context'
import { SettingsCascadeProps } from '../../../shared/src/settings/settings'
import { asError, createAggregateError, ErrorLike, isErrorLike } from '../../../shared/src/util/errors'
import { memoizeObservable } from '../../../shared/src/util/memoizeObservable'
import { queryGraphQL } from '../backend/graphql'
import { FilteredConnection } from '../components/FilteredConnection'
import { PageTitle } from '../components/PageTitle'
import { isDiscussionsEnabled } from '../discussions'
import { DiscussionsList } from '../discussions/DiscussionsList'
import { PatternTypeProps, CaseSensitivityProps } from '../search'
import { eventLogger, EventLoggerProps } from '../tracking/eventLogger'
import { basename } from '../util/path'
import { fetchTreeEntries } from './backend'
import { GitCommitNode, GitCommitNodeProps } from './commits/GitCommitNode'
import { gitCommitFragment } from './commits/RepositoryCommitsPage'
import { ThemeProps } from '../../../shared/src/theme'
import { ErrorAlert } from '../components/alerts'
import { subYears, formatISO } from 'date-fns'
import { pluralize } from '../../../shared/src/util/strings'
import { useObservable } from '../../../shared/src/util/useObservable'
const TreeEntry: React.FunctionComponent<{
isDir: boolean
name: string
parentPath: string
url: string
}> = ({ isDir, name, parentPath, url }) => {
const filePath = parentPath ? parentPath + '/' + name : name
return (
<Link to={url} className={`tree-entry ${isDir ? 'font-weight-bold' : ''}`} title={filePath}>
{name}
{isDir && '/'}
</Link>
)
}
/**
* Use a multi-column layout for tree entries when there are at least this many. See TreePage.scss
* for more information.
*/
const MIN_ENTRIES_FOR_COLUMN_LAYOUT = 6
const TreeEntriesSection: React.FunctionComponent<{
title: string
parentPath: string
entries: Pick<GQL.ITreeEntry, 'name' | 'isDirectory' | 'url'>[]
}> = ({ title, parentPath, entries }) =>
entries.length > 0 ? (
<section className="tree-page__section">
<h3 className="tree-page__section-header">{title}</h3>
<div className={entries.length > MIN_ENTRIES_FOR_COLUMN_LAYOUT ? 'tree-page__entries--columns' : undefined}>
{entries.map((e, i) => (
<TreeEntry
key={e.name + String(i)}
isDir={e.isDirectory}
name={e.name}
parentPath={parentPath}
url={e.url}
/>
))}
</div>
</section>
) : null
const fetchTreeCommits = memoizeObservable(
(args: {
repo: GQL.ID
revspec: string
first?: number
filePath?: string
after?: string
}): Observable<GQL.IGitCommitConnection> =>
queryGraphQL(
gql`
query TreeCommits($repo: ID!, $revspec: String!, $first: Int, $filePath: String, $after: String) {
node(id: $repo) {
... on Repository {
commit(rev: $revspec) {
ancestors(first: $first, path: $filePath, after: $after) {
nodes {
...GitCommitFields
}
pageInfo {
hasNextPage
}
}
}
}
}
}
${gitCommitFragment}
`,
args
).pipe(
map(({ data, errors }) => {
if (!data || !data.node) {
throw createAggregateError(errors)
}
const repo = data.node as GQL.IRepository
if (!repo.commit || !repo.commit.ancestors || !repo.commit.ancestors.nodes) {
throw createAggregateError(errors)
}
return repo.commit.ancestors
})
),
args => `${args.repo}:${args.revspec}:${String(args.first)}:${String(args.filePath)}:${String(args.after)}`
)
interface Props
extends SettingsCascadeProps,
ExtensionsControllerProps,
PlatformContextProps,
ThemeProps,
EventLoggerProps,
ActivationProps,
PatternTypeProps,
CaseSensitivityProps {
repoName: string
repoID: GQL.ID
repoDescription: string
/** The tree's path in TreePage. We call it filePath for consistency elsewhere. */
filePath: string
commitID: string
rev: string
location: H.Location
history: H.History
}
export const TreePage: React.FunctionComponent<Props> = ({
repoName,
repoID,
repoDescription,
commitID,
rev,
filePath,
patternType,
caseSensitive,
settingsCascade,
...props
}) => {
useEffect(() => {
if (filePath === '') {
eventLogger.logViewEvent('Repository')
} else {
eventLogger.logViewEvent('Tree')
}
}, [filePath])
const [showOlderCommits, setShowOlderCommits] = useState(false)
const onShowOlderCommitsClicked = useCallback(
(e: React.MouseEvent): void => {
e.preventDefault()
setShowOlderCommits(true)
},
[setShowOlderCommits]
)
const treeOrError = useObservable(
useMemo(
() =>
fetchTreeEntries({
repoName,
commitID,
rev,
filePath,
first: 2500,
}).pipe(catchError((err): [ErrorLike] => [asError(err)])),
[repoName, commitID, rev, filePath]
)
)
const getPageTitle = (): string => {
const repoStr = displayRepoName(repoName)
if (filePath) {
return `${basename(filePath)} - ${repoStr}`
}
return `${repoStr}`
}
const queryCommits = useCallback(
(args: { first?: number }): Observable<GQL.IGitCommitConnection> => {
const after: string | undefined = showOlderCommits ? undefined : formatISO(subYears(Date.now(), 1))
return fetchTreeCommits({
...args,
repo: repoID,
revspec: rev || '',
filePath,
after,
})
},
[filePath, repoID, rev, showOlderCommits]
)
const emptyElement = showOlderCommits ? (
<>No commits in this tree.</>
) : (
<div className="e2e-tree-page-no-recent-commits">
No commits in this tree in the past year.
<br />
<button
type="button"
className="btn btn-secondary btn-sm e2e-tree-page-show-all-commits"
onClick={onShowOlderCommitsClicked}
>
Show all commits
</button>
</div>
)
const TotalCountSummary: React.FunctionComponent<{ totalCount: number }> = ({ totalCount }) => (
<div className="mt-2">
{showOlderCommits ? (
<>{totalCount} total commits in this tree.</>
) : (
<>
{totalCount} {pluralize('commit', totalCount)} in this tree in the past year.
<br />
<button type="button" className="btn btn-secondary btn-sm mt-1" onClick={onShowOlderCommitsClicked}>
Show all commits
</button>
</>
)}
</div>
)
return (
<div className="tree-page">
<PageTitle title={getPageTitle()} />
{treeOrError === undefined ? (
<div>
<LoadingSpinner className="icon-inline tree-page__entries-loader" /> Loading files and directories
</div>
) : isErrorLike(treeOrError) ? (
<ErrorAlert error={treeOrError} />
) : (
<>
{treeOrError.isRoot ? (
<header>
<h2 className="tree-page__title">
<SourceRepositoryIcon className="icon-inline" /> {displayRepoName(repoName)}
</h2>
{repoDescription && <p>{repoDescription}</p>}
<div className="btn-group mb-3">
<Link className="btn btn-secondary" to={`${treeOrError.url}/-/commits`}>
<SourceCommitIcon className="icon-inline" /> Commits
</Link>
<Link className="btn btn-secondary" to={`/${repoName}/-/branches`}>
<SourceBranchIcon className="icon-inline" /> Branches
</Link>
<Link className="btn btn-secondary" to={`/${repoName}/-/tags`}>
<TagIcon className="icon-inline" /> Tags
</Link>
<Link
className="btn btn-secondary"
to={
rev
? `/${repoName}/-/compare/...${encodeURIComponent(rev)}`
: `/${repoName}/-/compare`
}
>
<HistoryIcon className="icon-inline" /> Compare
</Link>
<Link className="btn btn-secondary" to={`/${repoName}/-/stats/contributors`}>
<UserIcon className="icon-inline" /> Contributors
</Link>
</div>
</header>
) : (
<header>
<h2 className="tree-page__title">
<FolderIcon className="icon-inline" /> {filePath}
</h2>
</header>
)}
<TreeEntriesSection
title="Files and directories"
parentPath={filePath}
entries={treeOrError.entries}
/>
{isDiscussionsEnabled(settingsCascade) && (
<div className="tree-page__section mt-2 tree-page__section--discussions">
<h3 className="tree-page__section-header">Discussions</h3>
<DiscussionsList
{...props}
repoID={repoID}
rev={rev}
filePath={filePath + '/**' || undefined}
noun="discussion in this tree"
pluralNoun="discussions in this tree"
defaultFirst={2}
hideSearch={true}
compact={false}
/>
</div>
)}
{/* eslint-disable react/jsx-no-bind */}
<ActionsContainer
{...props}
menu={ContributableMenu.DirectoryPage}
render={items => (
<section className="tree-page__section">
<h3 className="tree-page__section-header">Actions</h3>
{items.map(item => (
<ActionItem
{...props}
key={item.action.id}
{...item}
className="btn btn-secondary mr-1 mb-1"
/>
))}
</section>
)}
empty={null}
/>
{/* eslint-enable react/jsx-no-bind */}
<div className="tree-page__section">
<h3 className="tree-page__section-header">Changes</h3>
<FilteredConnection<GQL.IGitCommit, Pick<GitCommitNodeProps, 'className' | 'compact'>>
location={props.location}
className="mt-2 tree-page__section--commits"
listClassName="list-group list-group-flush"
noun="commit in this tree"
pluralNoun="commits in this tree"
queryConnection={queryCommits}
nodeComponent={GitCommitNode}
nodeComponentProps={{
className: 'list-group-item',
compact: true,
}}
updateOnChange={`${repoName}:${rev}:${filePath}:${String(showOlderCommits)}`}
defaultFirst={7}
useURLQuery={false}
hideSearch={true}
emptyElement={emptyElement}
// eslint-disable-next-line react/jsx-no-bind
totalCountSummaryComponent={TotalCountSummary}
/>
</div>
</>
)}
</div>
)
}