File tree Expand file tree Collapse file tree
js/components/useDocsSearch Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,11 +12,12 @@ function Search() {
1212
1313 // Initialize the useSqlcSearch custom hook
1414 const {
15- searchText , // Text to search for
16- searchRes, // Search results
17- searchError, // Error information if search fails
18- validSearchUrl,// Boolean indicating if the search URL is valid
19- handleSearch, // Function to handle search input changes
15+ searchText , // Text to search for
16+ searchRes, // Search results
17+ searchError, // Error information if search fails
18+ validSearchUrl, // Boolean indicating if the search URL is valid
19+ isLoading, // Boolean indicating if component is loading search result
20+ handleSearch, // Function to handle search input changes
2021 } = useDocsSearch (searchUrl);
2122
2223 return (
Original file line number Diff line number Diff line change @@ -22,5 +22,6 @@ export interface DocsSearchReturn {
2222 searchText : string ;
2323 searchError : SearchError ;
2424 validSearchUrl : boolean ;
25+ isLoading : boolean ;
2526 handleSearch : ( event : ChangeEvent < HTMLInputElement > ) => Promise < void > ;
2627}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ function UseSqlcSearchTester() {
1919 searchRes, // Search results
2020 searchError, // Error information if search fails
2121 validSearchUrl, // Boolean indicating if the search URL is valid
22+ isLoading, // Boolean indicating if component is loading search result
2223 handleSearch, // Function to handle search input changes
2324 } = useDocsSearch ( searchUrl ) ;
2425
@@ -77,7 +78,7 @@ function UseSqlcSearchTester() {
7778 < >
7879 { /* Display search results */ }
7980 < p className = "text-lg font-semibold mb-4 text-gray-700" >
80- Number of results: { searchRes . data . length }
81+ Number of results: { isLoading ? "loading ..." : searchRes . data . length }
8182 </ p >
8283 < div className = "bg-gray-100 p-4 rounded-lg max-h-64 overflow-auto text-sm text-gray-800 whitespace-pre-wrap" >
8384 < pre > { JSON . stringify ( searchRes , null , 2 ) } </ pre >
Original file line number Diff line number Diff line change @@ -31,6 +31,9 @@ function useDocsSearch(searchUrl: string): DocsSearchReturn {
3131 // State to store the parsed URL object
3232 const [ url , setUrl ] = useState < URL > ( undefined ) ;
3333
34+ // State to store the loading state
35+ const [ isLoading , setIsLoading ] = useState < boolean > ( false ) ;
36+
3437 // Effect to validate and parse the search URL whenever it changes
3538 useEffect ( ( ) => {
3639 if ( searchUrl ) {
@@ -54,7 +57,7 @@ function useDocsSearch(searchUrl: string): DocsSearchReturn {
5457 const search = async ( query : string ) => {
5558 lastSearchIdRef . current += 1 ;
5659 setLastSearchId ( lastSearchIdRef . current ) ;
57-
60+ setIsLoading ( true ) ;
5861 if ( query && url ) {
5962 // Perform search only if query is non-empty and URL is valid
6063 try {
@@ -100,6 +103,7 @@ function useDocsSearch(searchUrl: string): DocsSearchReturn {
100103 setSearchRes ( { data : [ ] } ) ;
101104 setSearchError ( undefined ) ;
102105 }
106+ setIsLoading ( false ) ;
103107 } ;
104108
105109 /**
@@ -117,6 +121,7 @@ function useDocsSearch(searchUrl: string): DocsSearchReturn {
117121 searchText,
118122 searchError,
119123 validSearchUrl,
124+ isLoading,
120125 handleSearch,
121126 } ;
122127}
You can’t perform that action at this time.
0 commit comments