diff --git a/components/Search.module.scss b/components/Search.module.scss index ccaa155ba0c7..45f9e19640db 100644 --- a/components/Search.module.scss +++ b/components/Search.module.scss @@ -23,7 +23,7 @@ top: 0; right: 0; width: 0; - border-radius: 3px; + border-radius: 0 0 0 3px; background: var(--color-canvas-default); box-shadow: 0 1px 15px rgba(0, 0, 0, 0.15); transition: width 0.3s ease-in-out; diff --git a/components/Search.tsx b/components/Search.tsx index d27d4fcfb461..03e5548174c3 100644 --- a/components/Search.tsx +++ b/components/Search.tsx @@ -43,6 +43,7 @@ export function Search({ const { t } = useTranslation('search') const { currentVersion } = useVersion() const { languages } = useLanguages() + const initialRender = useRef(true) // Figure out language and version for index const { searchVersions, nonEnterpriseDefaultVersion } = useMainContext() @@ -61,6 +62,18 @@ export function Search({ } }, []) + // If the version changed from the dropdown version or language picker + // close the search pane and clear the query + // If the version changed from the search result window, keep the query + // and results but reset the versionFromSearchPane value + useEffect(() => { + if (initialRender.current) { + initialRender.current = false + } else { + closeSearch() + } + }, [currentVersion, language]) + // When the user finishes typing, update the results async function onSearch(e: React.ChangeEvent) { const xquery = e.target?.value?.trim() @@ -116,6 +129,9 @@ export function Search({ function closeSearch() { setQuery('') setResults(null) + if (inputRef.current) { + inputRef.current.value = '' + } } // Prevent the page from refreshing when you "submit" the form @@ -143,6 +159,7 @@ export function Search({ results={results} closeSearch={closeSearch} debug={'debug' in router.query} + query={query} /> {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */} @@ -212,6 +229,7 @@ function ShowSearchResults({ results, closeSearch, debug, + query, }: { anchorRef: RefObject isHeaderSearch: boolean @@ -219,8 +237,26 @@ function ShowSearchResults({ results: SearchResult[] | null closeSearch: () => void debug: boolean + query: string | string[] }) { const { t } = useTranslation('search') + const router = useRouter() + const { currentVersion } = useVersion() + const { allVersions } = useMainContext() + const searchVersion = allVersions[currentVersion].versionTitle + const latestVersions = new Set( + Object.keys(allVersions) + .map((version) => allVersions[version].latestVersion) + .filter((version) => version !== currentVersion) + ) + + const versions = Array.from(latestVersions).map((version) => { + return { + title: allVersions[version].versionTitle, + version: version, + } + }) + const redirectQuery = query ? `?query=${query}` : '' if (results !== null) { if (results.length === 0) { @@ -263,76 +299,88 @@ function ShowSearchResults({ } } > -
+
+
+

+ You're searching the {searchVersion}{' '} + version. Didn't find what you're looking for? Click a different version to try + again. +

+ {versions.map(({ title, version }) => { + return ( + + ) + })} +
{ - return { - key: url, - text: title, - renderItem: () => ( - - -
  • -
    - {/* Breadcrumbs in search records don't include the page title. These fields may contain elements that we need to render */} - - {debug && ( - - score: {score.toFixed(4)} popularity: {popularity.toFixed(4)} - + items={results.map(({ url, breadcrumbs, title, content, score, popularity }) => { + return { + key: url, + text: title, + renderItem: () => ( + + +
  • +
    + {/* Breadcrumbs in search records don't include the page title. These fields may contain elements that we need to render */} + + {debug && ( + + score: {score.toFixed(4)} popularity: {popularity.toFixed(4)} + + )} +
    +
    -
    -
    ]+(>|$)|(\/)/g, '') } - : { - __html: breadcrumbs - .split(' / ') - .slice(0, breadcrumbs.length - 1) - .join(' / ') - .replace(/<\/?[^>]+(>|$)/g, ''), - } - } - /> -
    -
  • - -
    - ), - } - })} + style={{ maxHeight: '2.5rem' }} + dangerouslySetInnerHTML={{ __html: content }} + /> +
    ]+(>|$)|(\/)/g, '') } + : { + __html: breadcrumbs + .split(' / ') + .slice(0, breadcrumbs.length - 1) + .join(' / ') + .replace(/<\/?[^>]+(>|$)/g, ''), + } + } + /> +
    + + + + ), + } + })} />
    diff --git a/components/context/MainContext.tsx b/components/context/MainContext.tsx index 5f29e3941cc1..299f8a88fdd5 100644 --- a/components/context/MainContext.tsx +++ b/components/context/MainContext.tsx @@ -23,6 +23,8 @@ export type ProductGroupT = { type VersionItem = { version: string versionTitle: string + currentRelease: string + latestVersion: string } export type ProductTreeNode = {