Skip to content

Commit a31ae20

Browse files
yang1666204liyang
and
liyang
authored
fix:search load (apache#1462)
Co-authored-by: liyang <[email protected]>
1 parent 74de2f7 commit a31ae20

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/theme/SearchBar/SearchBar.jsx

+20-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import LoadingRing from '../LoadingRing/LoadingRing';
2424
import styles from './SearchBar.module.css';
2525
import { normalizeContextByPath } from '../../utils/normalizeContextByPath';
2626
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';
2828
async function fetchAutoCompleteJS() {
2929
const autoCompleteModule = await import('@easyops-cn/autocomplete.js');
3030
const autoComplete = autoCompleteModule.default;
@@ -41,6 +41,8 @@ const SEARCH_PARAM_HIGHLIGHT = '_highlight';
4141
export default function SearchBar({ handleSearchBarToggle }) {
4242
const isBrowser = useIsBrowser();
4343
const location = useLocation();
44+
// const curVersion = useRef(DEFAULT_VERSION);
45+
const [curVersion, setCurVersion] = useState(DEFAULT_VERSION);
4446
const {
4547
siteConfig: { baseUrl },
4648
i18n: { currentLocale },
@@ -125,8 +127,8 @@ export default function SearchBar({ handleSearchBarToggle }) {
125127
setSearchContext(nextSearchContext);
126128
}, [location.pathname, versionUrl]);
127129
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) {
130132
// Do not load the index (again) if its already loaded or in the process of being loaded.
131133
return;
132134
}
@@ -339,10 +341,21 @@ export default function SearchBar({ handleSearchBarToggle }) {
339341
}, [isMac, onInputFocus]);
340342

341343
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+
});
344353
}
345-
}, [location.pathname, isDocsPage]);
354+
}, [location.pathname]);
355+
356+
useEffect(() => {
357+
loadIndex(true);
358+
}, [curVersion]);
346359

347360
const onClearSearch = useCallback(() => {
348361
const params = new URLSearchParams(location.search);
@@ -356,7 +369,7 @@ export default function SearchBar({ handleSearchBarToggle }) {
356369
setInputValue('');
357370
search.current?.autocomplete.setVal('');
358371
}, [location.pathname, location.search, location.hash, history]);
359-
372+
360373
return (
361374
<div
362375
className={clsx('navbar__search', styles.searchBarContainer, {

0 commit comments

Comments
 (0)