Skip to content

Commit

Permalink
perf: loadSearchData on focus & show loading only once (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc authored Sep 26, 2023
1 parent e0cac83 commit d72e8fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/client/theme-api/useSiteSearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ if (typeof window !== 'undefined') {

export const useSiteSearch = () => {
const debounceTimer = useRef<number>();
// const routes = useLocaleDocRoutes();
// const { demos } = useSiteData();
const [loading, setLoading] = useState(false);
const [loading, setLoading] = useState(true);
const [keywords, setKeywords] = useState('');
const [enabled, setEnabled] = useState(false);
const navData = useNavData();
Expand Down
1 change: 1 addition & 0 deletions src/client/theme-default/slots/SearchBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const SearchBar: FC = () => {
<Input
onFocus={() => {
setFocusing(true);
loadSearchData();
}}
onMouseEnter={() => {
loadSearchData();
Expand Down
9 changes: 8 additions & 1 deletion src/client/theme-default/slots/SearchResult/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ const SearchResult: FC<{
}> = (props) => {
const [data, histsCount] = useFlatSearchData(props.data);
const [activeIndex, setActiveIndex] = useState(-1);
const [loading, setLoading] = useState(props.loading);

useEffect(() => {
if (!props.loading) {
setLoading(false);
}
}, [props.loading]);

useEffect(() => {
const handler = (ev: KeyboardEvent) => {
Expand Down Expand Up @@ -149,7 +156,7 @@ const SearchResult: FC<{

let returnNode: React.ReactNode = null;

if (props.loading) {
if (loading) {
returnNode = (
<div className="dumi-default-search-empty">
<IconInbox />
Expand Down

0 comments on commit d72e8fe

Please sign in to comment.