-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathfileMatchContainer.tsx
More file actions
156 lines (138 loc) · 5.21 KB
/
fileMatchContainer.tsx
File metadata and controls
156 lines (138 loc) · 5.21 KB
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
'use client';
import { PathHeader } from "@/app/[domain]/components/pathHeader";
import { Separator } from "@/components/ui/separator";
import { DoubleArrowDownIcon, DoubleArrowUpIcon } from "@radix-ui/react-icons";
import { useMemo } from "react";
import { FileMatch } from "./fileMatch";
import { RepositoryInfo, SearchResultFile } from "@/features/search";
import { Button } from "@/components/ui/button";
export const MAX_MATCHES_TO_PREVIEW = 3;
interface FileMatchContainerProps {
file: SearchResultFile;
onOpenFilePreview: (matchIndex?: number) => void;
showAllMatches: boolean;
onShowAllMatchesButtonClicked: () => void;
isBranchFilteringEnabled: boolean;
repoInfo: Record<number, RepositoryInfo>;
yOffset: number;
}
export const FileMatchContainer = ({
file,
onOpenFilePreview,
showAllMatches,
onShowAllMatchesButtonClicked,
isBranchFilteringEnabled,
repoInfo,
yOffset,
}: FileMatchContainerProps) => {
const matchCount = useMemo(() => {
return file.chunks.length;
}, [file]);
const matches = useMemo(() => {
const sortedMatches = file.chunks.sort((a, b) => {
return a.contentStart.lineNumber - b.contentStart.lineNumber;
});
if (!showAllMatches) {
return sortedMatches.slice(0, MAX_MATCHES_TO_PREVIEW);
}
return sortedMatches;
}, [file, showAllMatches]);
const fileNameRange = useMemo(() => {
if (file.fileName.matchRanges.length > 0) {
const range = file.fileName.matchRanges[0];
return {
from: range.start.column - 1,
to: range.end.column - 1,
}
}
return undefined;
}, [file.fileName.matchRanges]);
const isMoreContentButtonVisible = useMemo(() => {
return matchCount > MAX_MATCHES_TO_PREVIEW;
}, [matchCount]);
const branches = useMemo(() => {
if (!file.branches) {
return [];
}
return file.branches;
}, [file.branches]);
const revisionName = useMemo(() => {
return branches.length > 0 ? branches[0] : undefined;
}, [branches]);
const branchDisplayName = useMemo(() => {
return revisionName ? `${revisionName}${branches.length > 1 ? ` +${branches.length - 1}` : ''}` : undefined;
}, [branches.length, revisionName]);
const repo = useMemo(() => {
return repoInfo[file.repositoryId];
}, [repoInfo, file.repositoryId]);
return (
<div>
{/* Title */}
<div
className="bg-accent primary-foreground px-2 py-0.5 flex flex-row items-center justify-between sticky top-0 z-10"
style={{
top: `-${yOffset}px`,
}}
>
<PathHeader
repo={{
name: repo.name,
codeHostType: repo.codeHostType,
displayName: repo.displayName,
webUrl: repo.webUrl,
}}
path={file.fileName.text}
pathHighlightRange={fileNameRange}
revisionName={revisionName}
branchDisplayName={branchDisplayName}
isBranchDisplayNameVisible={isBranchFilteringEnabled}
branchDisplayTitle={branches.join(", ")}
/>
<Button
variant="link"
className="text-blue-500 h-5"
onClick={() => {
onOpenFilePreview();
}}
>
Preview
</Button>
</div>
{/* Matches */}
{matches.map((match, index) => (
<div
key={index}
>
<FileMatch
match={match}
file={file}
/>
{(index !== matches.length - 1 || isMoreContentButtonVisible) && (
<Separator className="bg-accent" />
)}
</div>
))}
{/* Show more button */}
{isMoreContentButtonVisible && (
<div
tabIndex={0}
className="px-4 bg-accent p-0.5 group focus:outline-none"
onKeyDown={(e) => {
if (e.key !== "Enter") {
return;
}
onShowAllMatchesButtonClicked();
}}
onClick={onShowAllMatchesButtonClicked}
>
<p
className="text-blue-500 w-fit cursor-pointer text-sm flex flex-row items-center gap-2 group-focus:ring-2 group-focus:ring-blue-500 rounded-sm"
>
{showAllMatches ? <DoubleArrowUpIcon className="w-3 h-3" /> : <DoubleArrowDownIcon className="w-3 h-3" />}
{showAllMatches ? `Show fewer matches` : `Show ${matchCount - MAX_MATCHES_TO_PREVIEW} more matches`}
</p>
</div>
)}
</div>
);
}