Skip to content

Commit

Permalink
fix: loading svg cannot show
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Oct 6, 2022
1 parent 476f534 commit 35563b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/theme-default/components/Search/icons/loading.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 12 additions & 16 deletions src/theme-default/components/Search/index.tsx
@@ -1,9 +1,8 @@
import { ChangeEvent, useCallback, useRef, useState } from 'react';
import { MatchResultItem, PageSearcher } from '../../logic/search';
import { ComponentPropsWithIsland } from '../../../shared/types/index';
import SearchSvg from './icons/search.svg';
import LoadingSvg from './icons/loading.svg';
import { ComponentPropsWithIsland } from '../../../shared/types/index';
import { throttle } from 'lodash-es';

function SuggestionContent(props: {
suggestion: MatchResultItem;
Expand Down Expand Up @@ -69,21 +68,18 @@ export function Search(
props: ComponentPropsWithIsland & { langRoutePrefix: string }
) {
const [suggestions, setSuggestions] = useState<MatchResultItem[]>([]);
const [query, setQuery] = useState('');
const [initialized, setInitialized] = useState(false);
const [searching, setSearching] = useState(false);
const [focused, setFocused] = useState(false);
const [query, setQuery] = useState('');
const psRef = useRef<PageSearcher>();
const initPageSearcherPromiseRef = useRef<Promise<void>>();
const [initialized, setInitialized] = useState(false);
const [searching, setSearching] = useState(false);
// initializing or searching
const showLoading = query.length > 0 && (!initialized || searching);
// 1. user input query
// 2. page searcher has been initialized and finish searching
// 4. result is empty
const showNotFound =
query.length > 0 && !showLoading && suggestions.length === 0;
console.log(showLoading);

// initializing or searching
const showLoading = !initialized || searching;
// 1. page searcher has been initialized and finish searching
// 2. result is empty
const showNotFound = !showLoading && suggestions.length === 0;
const initPageSearcher = useCallback(async () => {
if (!psRef.current) {
const { PageSearcher } = await import('../../logic/search');
Expand All @@ -97,7 +93,7 @@ export function Search(

// eslint-disable-next-line react-hooks/exhaustive-deps
const onQueryChanged = useCallback(
throttle(async (e: ChangeEvent<HTMLInputElement>) => {
async (e: ChangeEvent<HTMLInputElement>) => {
const newQuery = e.target.value;
setQuery(newQuery);
initPageSearcherPromiseRef.current =
Expand All @@ -107,7 +103,7 @@ export function Search(
const matched = await psRef.current!.match(newQuery);
setSearching(false);
setSuggestions(matched);
}, 200),
},
[initPageSearcher]
);
return (
Expand All @@ -133,7 +129,7 @@ export function Search(
initPageSearcherPromiseRef.current = initPageSearcher();
}}
/>
{focused && query.length > 0 && (
{focused && query && (
<ul
absolute=""
z="60"
Expand Down

0 comments on commit 35563b3

Please sign in to comment.