From ea11367ea436aa0d8ac56e09c0e4c9bb382b67f5 Mon Sep 17 00:00:00 2001 From: Djordje Vlaisavljevic Date: Mon, 11 Nov 2024 11:36:04 +0000 Subject: [PATCH] Stopped loading spinner flash when you first open Search page (#21569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close https://linear.app/ghost/issue/AP-531/search-spinner-flashes-for-a-split-second-when-switching-tab-for-the - Fixed the logic for displaying “loading” and “no results found” states - Refactored the component to avoid nested ternaries --- apps/admin-x-activitypub/package.json | 2 +- .../src/components/Search.tsx | 93 ++++++++++++------- 2 files changed, 60 insertions(+), 35 deletions(-) diff --git a/apps/admin-x-activitypub/package.json b/apps/admin-x-activitypub/package.json index 829fbf9992..29ce99717b 100644 --- a/apps/admin-x-activitypub/package.json +++ b/apps/admin-x-activitypub/package.json @@ -1,6 +1,6 @@ { "name": "@tryghost/admin-x-activitypub", - "version": "0.3.13", + "version": "0.3.14", "license": "MIT", "repository": { "type": "git", diff --git a/apps/admin-x-activitypub/src/components/Search.tsx b/apps/admin-x-activitypub/src/components/Search.tsx index 0985397f48..0d3598a273 100644 --- a/apps/admin-x-activitypub/src/components/Search.tsx +++ b/apps/admin-x-activitypub/src/components/Search.tsx @@ -71,6 +71,49 @@ const SearchResult: React.FC = ({result, update}) => { ); }; +const SearchResults: React.FC<{ + results: SearchResultItem[]; + onUpdate: (id: string, updated: Partial) => void; +}> = ({results, onUpdate}) => { + return ( + <> + {results.map(result => ( + + ))} + + ); +}; + +const SuggestedAccounts: React.FC<{ + profiles: SearchResultItem[]; + isLoading: boolean; + onUpdate: (id: string, updated: Partial) => void; +}> = ({profiles, isLoading, onUpdate}) => { + return ( + <> + + Suggested accounts + + {isLoading && ( +
+ +
+ )} + {profiles.map(profile => ( + + ))} + + ); +}; + const Search: React.FC = ({}) => { // Initialise suggested profiles const {suggestedProfilesQuery, updateSuggestedProfile} = useSuggestedProfiles('index', ['@index@activitypub.ghost.org', '@index@john.onolan.org', '@index@coffeecomplex.ghost.io', '@index@codename-jimmy.ghost.io', '@index@syphoncontinuity.ghost.io']); @@ -80,23 +123,14 @@ const Search: React.FC = ({}) => { const queryInputRef = useRef(null); const [query, setQuery] = useState(''); const [debouncedQuery] = useDebounce(query, 300); - const [isQuerying, setIsQuerying] = useState(false); const {searchQuery, updateProfileSearchResult: updateResult} = useSearchForUser('index', query !== '' ? debouncedQuery : query); const {data, isFetching, isFetched} = searchQuery; const results = data?.profiles || []; - const showLoading = (isFetching || isQuerying) && !isFetched; - const showNoResults = isFetched && results.length === 0 && (query.length > 0); + const showLoading = isFetching && query.length > 0; + const showNoResults = !isFetching && isFetched && results.length === 0 && query.length > 0 && debouncedQuery === query; const showSuggested = query === '' || (isFetched && results.length === 0); - useEffect(() => { - if (query !== '') { - setIsQuerying(true); - } else { - setIsQuerying(false); - } - }, [query]); - // Focus the query input on initial render useEffect(() => { if (queryInputRef.current) { @@ -132,41 +166,32 @@ const Search: React.FC = ({}) => { unstyled onClick={() => { setQuery(''); - queryInputRef.current?.focus(); }} /> )} - {showLoading && ( - - )} + {showLoading && } + {showNoResults && ( No users matching this username )} - {results.map(result => ( - - ))} + )} + {showSuggested && ( - <> - Suggested accounts - {isLoadingSuggested && ( - - )} - {suggested.map(profile => ( - - ))} - + )}