This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathSearchResults.svelte
More file actions
265 lines (233 loc) · 9.1 KB
/
SearchResults.svelte
File metadata and controls
265 lines (233 loc) · 9.1 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
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
<svelte:options immutable />
<script context="module" lang="ts">
export type SearchResultsCapture = number
interface ResultStateCache {
count: number
expanded: Set<SearchMatch>
preview: ContentMatch | SymbolMatch | PathMatch | null
}
const cache = new Map<string, ResultStateCache>()
const DEFAULT_INITIAL_ITEMS_TO_SHOW = 15
const INCREMENTAL_ITEMS_TO_SHOW = 10
</script>
<script lang="ts">
import { mdiCloseOctagonOutline } from '@mdi/js'
import type { Observable } from 'rxjs'
import { onMount, tick } from 'svelte'
import { writable } from 'svelte/store'
import { beforeNavigate, goto } from '$app/navigation'
import { limitHit } from '$lib/branded'
import { observeIntersection } from '$lib/intersection-observer'
import type { URLQueryFilter } from '$lib/search/dynamicFilters'
import { createRecentSearchesStore } from '$lib/search/input/recentSearches'
import { getQueryURL, type QueryStateStore } from '$lib/search/state'
import { SVELTE_LOGGER, SVELTE_TELEMETRY_EVENTS, codeCopiedEvent } from '$lib/telemetry'
import {
type AggregateStreamingSearchResults,
type PathMatch,
type SearchMatch,
type SymbolMatch,
type ContentMatch,
} from '$lib/shared'
import type { QueryState } from '$lib/search/state'
import Icon from '$lib/Icon.svelte'
import Panel from '$lib/wildcard/resizable-panel/Panel.svelte'
import PanelGroup from '$lib/wildcard/resizable-panel/PanelGroup.svelte'
import PanelResizeHandle from '$lib/wildcard/resizable-panel/PanelResizeHandle.svelte'
import SearchInput from '$lib/search/input/SearchInput.svelte'
import DynamicFiltersSidebar from '$lib/search/dynamicFilters/Sidebar.svelte'
import GlobalHeaderPortal from '$lib/navigation/GlobalHeaderPortal.svelte'
import PreviewPanel from './PreviewPanel.svelte'
import SearchAlert from './SearchAlert.svelte'
import { getSearchResultComponent } from './searchResultFactory'
import { setSearchResultsContext } from './searchResultsContext'
import StreamingProgress from './StreamingProgress.svelte'
export let stream: Observable<AggregateStreamingSearchResults>
export let queryFromURL: string
export let selectedFilters: URLQueryFilter[]
export let queryState: QueryStateStore
export function capture(): SearchResultsCapture {
return resultContainer?.scrollTop ?? 0
}
export function restore(capture?: SearchResultsCapture): void {
if (resultContainer) {
resultContainer.scrollTop = capture ?? 0
}
}
let resultContainer: HTMLElement | null = null
const recentSearches = createRecentSearchesStore()
$: state = $stream.state // 'loading', 'error', 'complete'
$: results = $stream.results
$: if (state !== 'loading') {
recentSearches.addRecentSearch({
query: queryFromURL,
limitHit: limitHit($stream.progress),
resultCount: $stream.progress.matchCount,
})
}
// Logic for maintaining list state (scroll position, rendered items, open
// items) for backwards navigation.
$: cacheEntry = cache.get(queryFromURL)
$: count = cacheEntry?.count ?? DEFAULT_INITIAL_ITEMS_TO_SHOW
$: resultsToShow = results.slice(0, count)
$: expandedSet = cacheEntry?.expanded || new Set<SearchMatch>()
$: previewResult = writable(cacheEntry?.preview ?? null)
setSearchResultsContext({
isExpanded(match: SearchMatch): boolean {
return expandedSet.has(match)
},
setExpanded(match: SearchMatch, expanded: boolean): void {
if (expanded) {
expandedSet.add(match)
} else {
expandedSet.delete(match)
}
},
setPreview(result: ContentMatch | SymbolMatch | PathMatch | null): void {
previewResult.set(result)
},
queryState,
})
beforeNavigate(() => {
cache.set(queryFromURL, { count, expanded: expandedSet, preview: $previewResult })
})
onMount(() => {
SVELTE_LOGGER.logViewEvent(SVELTE_TELEMETRY_EVENTS.ViewSearchResultsPage)
})
function loadMore(event: { detail: boolean }) {
if (event.detail) {
count += INCREMENTAL_ITEMS_TO_SHOW
}
}
// FIXME: Not a great solution since it relies on implementation details of
// the progress component
async function onResubmitQuery(event: SubmitEvent) {
const target = event.currentTarget as HTMLElement | null
const filters = Array.from(target?.querySelectorAll('[name="query"]') ?? [])
.filter(input => (input as HTMLInputElement).checked)
.map(input => (input as HTMLInputElement).value)
.join(' ')
queryState.setQuery(query => query + ' ' + filters)
await tick()
void goto(getQueryURL($queryState))
}
function handleResultCopy(): void {
SVELTE_LOGGER.log(...codeCopiedEvent('search-result'))
}
function handleSearchResultClick(): void {
SVELTE_LOGGER.log(SVELTE_TELEMETRY_EVENTS.SearchResultClick)
}
function handleSubmit(state: QueryState) {
SVELTE_LOGGER.log(
SVELTE_TELEMETRY_EVENTS.SearchSubmit,
{ source: 'nav', query: state.query },
{ source: 'nav', patternType: state.patternType }
)
}
</script>
<svelte:head>
<title>{queryFromURL} - Sourcegraph</title>
</svelte:head>
<GlobalHeaderPortal>
<div class="search-header">
<SearchInput {queryState} size="compat" onSubmit={handleSubmit} />
</div>
</GlobalHeaderPortal>
<div class="search-results">
<PanelGroup id="search-results-panels">
<Panel id="search-results-filters" order={1} defaultSize={25} maxSize={35} minSize={15}>
<DynamicFiltersSidebar
{selectedFilters}
streamFilters={$stream.filters}
searchQuery={queryFromURL}
{state}
/>
</Panel>
<PanelResizeHandle />
<Panel id="search-results-content" order={2} minSize={35}>
<div class="results">
<aside class="actions">
<StreamingProgress {state} progress={$stream.progress} on:submit={onResubmitQuery} />
</aside>
<div class="result-list" bind:this={resultContainer}>
{#if $stream.alert}
<div class="message-container">
<SearchAlert alert={$stream.alert} />
</div>
{/if}
<ol on:click={handleSearchResultClick} on:copy={handleResultCopy}>
{#each resultsToShow as result, i}
{@const component = getSearchResultComponent(result)}
{#if i === resultsToShow.length - 1}
<li use:observeIntersection on:intersecting={loadMore}>
<svelte:component this={component} {result} />
</li>
{:else}
<li><svelte:component this={component} {result} /></li>
{/if}
{/each}
</ol>
{#if resultsToShow.length === 0 && state !== 'loading'}
<div class="message-container">
<Icon svgPath={mdiCloseOctagonOutline} />
<p>No results found</p>
</div>
{/if}
</div>
</div>
</Panel>
{#if $previewResult}
<PanelResizeHandle />
<Panel id="search-results-file-preview" order={3} minSize={30}>
<PreviewPanel result={$previewResult} />
</Panel>
{/if}
</PanelGroup>
</div>
<style lang="scss">
.search-header {
width: 100%;
// This ensures that the search suggestions panel is displayed above the
// search results panel.
z-index: 1;
}
.search-results {
display: flex;
flex: 1;
overflow: auto;
// Isolate everything in search results so they won't be displayed over
// the search suggestions. Previously, hovering over separator would
// overlap the suggestions panel.
isolation: isolate;
}
.results {
flex: 1;
height: 100%;
overflow: auto;
min-height: 0;
display: flex;
flex-direction: column;
.actions {
flex-shrink: 0;
display: flex;
align-items: center;
padding: 0.5rem;
border-bottom: 1px solid var(--border-color);
}
.result-list {
overflow: auto;
ol {
padding: 0;
margin: 0;
list-style: none;
}
}
.message-container {
display: flex;
flex-direction: column;
margin: 2rem;
align-items: center;
color: var(--text-muted);
}
}
</style>