-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
StreamingSearchResultsList.tsx
281 lines (260 loc) · 10.9 KB
/
StreamingSearchResultsList.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
import React, { useCallback } from 'react'
import classNames from 'classnames'
import AlphaSBoxIcon from 'mdi-react/AlphaSBoxIcon'
import FileDocumentIcon from 'mdi-react/FileDocumentIcon'
import FileIcon from 'mdi-react/FileIcon'
import { useLocation } from 'react-router'
import { Observable } from 'rxjs'
import { HoverMerged } from '@sourcegraph/client-api'
import { Hoverifier } from '@sourcegraph/codeintellify'
import { SearchContextProps } from '@sourcegraph/search'
import { CommitSearchResult, RepoSearchResult, FileSearchResult } from '@sourcegraph/search-ui'
import { ActionItemAction } from '@sourcegraph/shared/src/actions/ActionItem'
import { FetchFileParameters } from '@sourcegraph/shared/src/backend/file'
import { FilePrefetcher, PrefetchableFile } from '@sourcegraph/shared/src/components/PrefetchableFile'
import { displayRepoName } from '@sourcegraph/shared/src/components/RepoLink'
import { VirtualList } from '@sourcegraph/shared/src/components/VirtualList'
import { Controller as ExtensionsController } from '@sourcegraph/shared/src/extensions/controller'
import { HoverContext } from '@sourcegraph/shared/src/hover/HoverOverlay.types'
import { PlatformContextProps } from '@sourcegraph/shared/src/platform/context'
import {
AggregateStreamingSearchResults,
ContentMatch,
SymbolMatch,
PathMatch,
SearchMatch,
getMatchUrl,
getRevision,
} from '@sourcegraph/shared/src/search/stream'
import { SettingsCascadeProps } from '@sourcegraph/shared/src/settings/settings'
import { TelemetryProps } from '@sourcegraph/shared/src/telemetry/telemetryService'
import { ThemeProps } from '@sourcegraph/shared/src/theme'
import { smartSearchClickedEvent } from '../util/events'
import { NoResultsPage } from './NoResultsPage'
import { StreamingSearchResultFooter } from './StreamingSearchResultsFooter'
import { useItemsToShow } from './use-items-to-show'
import resultContainerStyles from '../components/ResultContainer.module.scss'
import styles from './StreamingSearchResultsList.module.scss'
export interface StreamingSearchResultsListProps
extends ThemeProps,
SettingsCascadeProps,
TelemetryProps,
Pick<SearchContextProps, 'searchContextsEnabled'>,
PlatformContextProps<'requestGraphQL'> {
isSourcegraphDotCom: boolean
results?: AggregateStreamingSearchResults
allExpanded: boolean
fetchHighlightedFileLineRanges: (parameters: FetchFileParameters, force?: boolean) => Observable<string[][]>
showSearchContext: boolean
/** Clicking on a match opens the link in a new tab. */
openMatchesInNewTab?: boolean
/** Available to web app through JS Context */
assetsRoot?: string
extensionsController?: Pick<ExtensionsController, 'extHostAPI'> | null
hoverifier?: Hoverifier<HoverContext, HoverMerged, ActionItemAction>
/**
* Latest run query. Resets scroll visibility state when changed.
* For example, `location.search` on web.
*/
executedQuery: string
/**
* Classname to be applied to the container of a search result.
*/
resultClassName?: string
/**
* For A/B testing on Sourcegraph.com. To be removed at latest by 12/2022.
*/
smartSearchEnabled?: boolean
prefetchFile?: FilePrefetcher
prefetchFileEnabled?: boolean
}
export const StreamingSearchResultsList: React.FunctionComponent<
React.PropsWithChildren<StreamingSearchResultsListProps>
> = ({
results,
allExpanded,
fetchHighlightedFileLineRanges,
settingsCascade,
telemetryService,
isLightTheme,
isSourcegraphDotCom,
searchContextsEnabled,
showSearchContext,
assetsRoot,
platformContext,
extensionsController,
hoverifier,
openMatchesInNewTab,
executedQuery,
resultClassName,
prefetchFile,
prefetchFileEnabled,
}) => {
const resultsNumber = results?.results.length || 0
const { itemsToShow, handleBottomHit } = useItemsToShow(executedQuery, resultsNumber)
const location = useLocation()
const logSearchResultClicked = useCallback(
(index: number, type: string) => {
telemetryService.log('SearchResultClicked')
// This data ends up in Prometheus and is not part of the ping payload.
telemetryService.log('search.ranking.result-clicked', { index, type })
// Lucky search A/B test events on Sourcegraph.com. To be removed at latest by 12/2022.
if (
!(
results?.alert?.kind === 'smart-search-additional-results' ||
results?.alert?.kind === 'smart-search-pure-results'
)
) {
telemetryService.log('SearchResultClickedAutoNone')
}
if (
(results?.alert?.kind === 'smart-search-additional-results' ||
results?.alert?.kind === 'smart-search-pure-results') &&
results?.alert?.title &&
results.alert.proposedQueries
) {
const event = smartSearchClickedEvent(
results.alert.kind,
results.alert.title,
results.alert.proposedQueries.map(entry => entry.description || '')
)
telemetryService.log(event)
}
},
[telemetryService, results]
)
const renderResult = useCallback(
(result: SearchMatch, index: number): JSX.Element => {
switch (result.type) {
case 'content':
case 'path':
case 'symbol':
return (
<PrefetchableFile
isPrefetchEnabled={prefetchFileEnabled}
prefetch={prefetchFile}
filePath={result.path}
revision={getRevision(result.branches, result.commit)}
repoName={result.repository}
// PrefetchableFile adds an extra wrapper, so we lift the <li> up and match the ResultContainer styles.
// Better approach would be to use `as` to avoid wrapping, but that requires a larger refactor of the
// child components than is worth doing right now for this experimental feature
className={resultContainerStyles.resultContainer}
as="li"
>
<FileSearchResult
index={index}
location={location}
telemetryService={telemetryService}
icon={getFileMatchIcon(result)}
result={result}
onSelect={() => logSearchResultClicked(index, 'fileMatch')}
expanded={false}
showAllMatches={false}
allExpanded={allExpanded}
fetchHighlightedFileLineRanges={fetchHighlightedFileLineRanges}
repoDisplayName={displayRepoName(result.repository)}
settingsCascade={settingsCascade}
extensionsController={extensionsController}
hoverifier={hoverifier}
openInNewTab={openMatchesInNewTab}
containerClassName={resultClassName}
/>
</PrefetchableFile>
)
case 'commit':
return (
<CommitSearchResult
index={index}
result={result}
platformContext={platformContext}
onSelect={() => logSearchResultClicked(index, 'commit')}
openInNewTab={openMatchesInNewTab}
containerClassName={resultClassName}
as="li"
/>
)
case 'repo':
return (
<RepoSearchResult
index={index}
result={result}
onSelect={() => logSearchResultClicked(index, 'repo')}
containerClassName={resultClassName}
as="li"
/>
)
}
},
[
prefetchFileEnabled,
prefetchFile,
location,
telemetryService,
allExpanded,
fetchHighlightedFileLineRanges,
settingsCascade,
extensionsController,
hoverifier,
openMatchesInNewTab,
resultClassName,
platformContext,
logSearchResultClicked,
]
)
return (
<>
<VirtualList<SearchMatch>
as="ol"
aria-label="Search results"
className={classNames('mt-2 mb-0', styles.list)}
itemsToShow={itemsToShow}
onShowMoreItems={handleBottomHit}
items={results?.results || []}
itemProps={undefined}
itemKey={itemKey}
renderItem={renderResult}
/>
{itemsToShow >= resultsNumber && (
<StreamingSearchResultFooter results={results}>
<>
{results?.state === 'complete' && resultsNumber === 0 && (
<NoResultsPage
searchContextsEnabled={searchContextsEnabled}
isSourcegraphDotCom={isSourcegraphDotCom}
isLightTheme={isLightTheme}
telemetryService={telemetryService}
showSearchContext={showSearchContext}
assetsRoot={assetsRoot}
/>
)}
</>
</StreamingSearchResultFooter>
)}
</>
)
}
function itemKey(item: SearchMatch): string {
if (item.type === 'content') {
const lineStart = item.chunkMatches
? item.chunkMatches.length > 0
? item.chunkMatches[0].contentStart.line
: 0
: 0
return `file:${getMatchUrl(item)}:${lineStart}`
}
if (item.type === 'symbol') {
return `file:${getMatchUrl(item)}`
}
return getMatchUrl(item)
}
function getFileMatchIcon(result: ContentMatch | SymbolMatch | PathMatch): React.ComponentType<{ className?: string }> {
switch (result.type) {
case 'content':
return FileDocumentIcon
case 'symbol':
return AlphaSBoxIcon
case 'path':
return FileIcon
}
}