forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilePathBreadcrumb.tsx
69 lines (66 loc) · 2.32 KB
/
FilePathBreadcrumb.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
import * as React from 'react'
import { LinkOrSpan } from '../../../shared/src/components/LinkOrSpan'
import { RepoRev, toPrettyBlobURL } from '../../../shared/src/util/url'
import { toTreeURL } from '../util/url'
interface Props {
path: string
partToUrl: (i: number) => string | undefined
partToClassName?: (i: number) => string
}
/**
* A breadcrumb where each path component is a separate link. Use this sparingly. Usually having the entire path be
* a single link target is more usable; in that case, use RepoFileLink.
*/
const Breadcrumb: React.FunctionComponent<Props> = props => {
const parts = props.path.split('/')
const spans: JSX.Element[] = []
for (const [i, part] of parts.entries()) {
const link = props.partToUrl(i)
const className = `part ${props.partToClassName ? props.partToClassName(i) : ''} ${
i === parts.length - 1 ? 'part-last' : ''
}`
spans.push(
<LinkOrSpan key={i} className={className} to={link}>
{part}
</LinkOrSpan>
)
if (i < parts.length - 1) {
spans.push(
<span key={'sep' + i} className="breadcrumb__separator">
/
</span>
)
}
}
return (
// Important: do not put spaces between the breadcrumbs or spaces will get added when copying the path
<span className="breadcrumb">{spans}</span>
)
}
/**
* Displays a file path in a repository in breadcrumb style, with ancestor path
* links.
*/
export const FilePathBreadcrumb: React.FunctionComponent<
RepoRev & {
filePath: string
isDir: boolean
}
> = ({ repoName, rev, filePath, isDir }) => {
const parts = filePath.split('/')
return (
/* eslint-disable react/jsx-no-bind */
<Breadcrumb
path={filePath}
partToUrl={i => {
const partPath = parts.slice(0, i + 1).join('/')
if (isDir || i < parts.length - 1) {
return toTreeURL({ repoName, rev, filePath: partPath })
}
return toPrettyBlobURL({ repoName, rev, filePath: partPath })
}}
partToClassName={i => (i === parts.length - 1 ? 'part-last' : 'part-directory')}
/>
/* eslint-enable react/jsx-no-bind */
)
}