@@ -24,7 +24,7 @@ import LoadingRing from '../LoadingRing/LoadingRing';
24
24
import styles from './SearchBar.module.css' ;
25
25
import { normalizeContextByPath } from '../../utils/normalizeContextByPath' ;
26
26
import useIsDocPage from '@site/src/hooks/use-is-doc' ;
27
- import { VERSIONS } from '@site/src/constant/common' ;
27
+ import { VERSIONS , DEFAULT_VERSION } from '@site/src/constant/common' ;
28
28
async function fetchAutoCompleteJS ( ) {
29
29
const autoCompleteModule = await import ( '@easyops-cn/autocomplete.js' ) ;
30
30
const autoComplete = autoCompleteModule . default ;
@@ -41,6 +41,8 @@ const SEARCH_PARAM_HIGHLIGHT = '_highlight';
41
41
export default function SearchBar ( { handleSearchBarToggle } ) {
42
42
const isBrowser = useIsBrowser ( ) ;
43
43
const location = useLocation ( ) ;
44
+ // const curVersion = useRef(DEFAULT_VERSION);
45
+ const [ curVersion , setCurVersion ] = useState ( DEFAULT_VERSION ) ;
44
46
const {
45
47
siteConfig : { baseUrl } ,
46
48
i18n : { currentLocale } ,
@@ -125,8 +127,8 @@ export default function SearchBar({ handleSearchBarToggle }) {
125
127
setSearchContext ( nextSearchContext ) ;
126
128
} , [ location . pathname , versionUrl ] ) ;
127
129
const hidden = ! ! hideSearchBarWithNoSearchContext && Array . isArray ( searchContextByPaths ) && searchContext === '' ;
128
- const loadIndex = useCallback ( async ( ) => {
129
- if ( hidden || indexStateMap . current . get ( searchContext ) ) {
130
+ const loadIndex = useCallback ( async ( forceLoad = false ) => {
131
+ if ( ( hidden || indexStateMap . current . get ( searchContext ) ) && ! forceLoad ) {
130
132
// Do not load the index (again) if its already loaded or in the process of being loaded.
131
133
return ;
132
134
}
@@ -339,10 +341,21 @@ export default function SearchBar({ handleSearchBarToggle }) {
339
341
} , [ isMac , onInputFocus ] ) ;
340
342
341
343
useEffect ( ( ) => {
342
- if ( isDocsPage ) {
343
- loadIndex ( ) ;
344
+ const pathHaveVer = VERSIONS . some ( item => location . pathname . includes ( item ) ) ;
345
+ if ( ! pathHaveVer && curVersion !== '2.1' ) {
346
+ setCurVersion ( '2.1' ) ;
347
+ } else {
348
+ VERSIONS . forEach ( item => {
349
+ if ( location . pathname . includes ( item ) && item !== curVersion ) {
350
+ setCurVersion ( item ) ;
351
+ }
352
+ } ) ;
344
353
}
345
- } , [ location . pathname , isDocsPage ] ) ;
354
+ } , [ location . pathname ] ) ;
355
+
356
+ useEffect ( ( ) => {
357
+ loadIndex ( true ) ;
358
+ } , [ curVersion ] ) ;
346
359
347
360
const onClearSearch = useCallback ( ( ) => {
348
361
const params = new URLSearchParams ( location . search ) ;
@@ -356,7 +369,7 @@ export default function SearchBar({ handleSearchBarToggle }) {
356
369
setInputValue ( '' ) ;
357
370
search . current ?. autocomplete . setVal ( '' ) ;
358
371
} , [ location . pathname , location . search , location . hash , history ] ) ;
359
-
372
+
360
373
return (
361
374
< div
362
375
className = { clsx ( 'navbar__search' , styles . searchBarContainer , {
0 commit comments