Skip to content

Commit e919d50

Browse files
authored
change version within search result window (#22702)
1 parent 5ac9d72 commit e919d50

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

components/Search.tsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function Search({
4343
const { t } = useTranslation('search')
4444
const { currentVersion } = useVersion()
4545
const { languages } = useLanguages()
46+
const initialRender = useRef(true)
4647

4748
// Figure out language and version for index
4849
const { searchVersions, nonEnterpriseDefaultVersion } = useMainContext()
@@ -61,6 +62,18 @@ export function Search({
6162
}
6263
}, [])
6364

65+
// If the version changed from the dropdown version or language picker
66+
// close the search pane and clear the query
67+
// If the version changed from the search result window, keep the query
68+
// and results but reset the versionFromSearchPane value
69+
useEffect(() => {
70+
if (initialRender.current) {
71+
initialRender.current = false
72+
} else {
73+
closeSearch()
74+
}
75+
}, [currentVersion, language])
76+
6477
// When the user finishes typing, update the results
6578
async function onSearch(e: React.ChangeEvent<HTMLInputElement>) {
6679
const xquery = e.target?.value?.trim()
@@ -116,6 +129,9 @@ export function Search({
116129
function closeSearch() {
117130
setQuery('')
118131
setResults(null)
132+
if (inputRef.current) {
133+
inputRef.current.value = ''
134+
}
119135
}
120136

121137
// Prevent the page from refreshing when you "submit" the form
@@ -143,6 +159,7 @@ export function Search({
143159
results={results}
144160
closeSearch={closeSearch}
145161
debug={'debug' in router.query}
162+
query={query}
146163
/>
147164
</div>
148165
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
@@ -212,15 +229,34 @@ function ShowSearchResults({
212229
results,
213230
closeSearch,
214231
debug,
232+
query,
215233
}: {
216234
anchorRef: RefObject<HTMLElement>
217235
isHeaderSearch: boolean
218236
isLoading: boolean
219237
results: SearchResult[] | null
220238
closeSearch: () => void
221239
debug: boolean
240+
query: string | string[]
222241
}) {
223242
const { t } = useTranslation('search')
243+
const router = useRouter()
244+
const { currentVersion } = useVersion()
245+
const { allVersions } = useMainContext()
246+
const searchVersion = allVersions[currentVersion].versionTitle
247+
const latestVersions = new Set(
248+
Object.keys(allVersions)
249+
.map((version) => allVersions[version].latestVersion)
250+
.filter((version) => version !== currentVersion)
251+
)
252+
253+
const versions = Array.from(latestVersions).map((version) => {
254+
return {
255+
title: allVersions[version].versionTitle,
256+
version: version,
257+
}
258+
})
259+
const redirectQuery = query ? `?query=${query}` : ''
224260

225261
if (results !== null) {
226262
if (results.length === 0) {
@@ -263,7 +299,21 @@ function ShowSearchResults({
263299
}
264300
}
265301
>
266-
<div data-testid="search-results" className="mt-n2 mb-n2">
302+
<div data-testid="search-results">
303+
<div className="my-4">
304+
<p className="ml-4">
305+
You're searching the <span className="color-fg-attention">{searchVersion}</span>{' '}
306+
version. Didn't find what you're looking for? Click a different version to try
307+
again.
308+
</p>
309+
{versions.map(({ title, version }) => {
310+
return (
311+
<button key={version} className="btn mr-2 mt-4 ml-4" type="button">
312+
<a href={`/${router.locale}/${version}${redirectQuery}`}>{title}</a>
313+
</button>
314+
)
315+
})}
316+
</div>
267317
<ActionList
268318
items={results.map(({ url, breadcrumbs, title, content, score, popularity }) => {
269319
return {

components/context/MainContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export type ProductGroupT = {
2323
type VersionItem = {
2424
version: string
2525
versionTitle: string
26+
currentRelease: string
27+
latestVersion: string
2628
}
2729

2830
export type ProductTreeNode = {

0 commit comments

Comments
 (0)