@@ -10,6 +10,7 @@ import {
1010 Chip ,
1111 List ,
1212 ListItem ,
13+ LinearProgress ,
1314} from "@mui/material" ;
1415import { debounce } from 'lodash' ;
1516import PropTypes from 'prop-types' ;
@@ -91,6 +92,7 @@ const Search = () => {
9192 const [ terms , setTerms ] = useState ( [ ] ) ;
9293 const [ organizations , setOrganizations ] = useState ( [ ] ) ;
9394 const [ ontologies , setOntologies ] = useState ( [ ] ) ;
95+ const [ isSearching , setIsSearching ] = useState ( false ) ;
9496 const { storedSearchTerm, updateStoredSearchTerm, user } = useContext ( GlobalDataContext ) ;
9597
9698 // Get the group name based on user login status
@@ -155,6 +157,7 @@ const Search = () => {
155157 setTerms ( [ ] )
156158 setOntologies ( [ ] )
157159 setOrganizations ( [ ] )
160+ setIsSearching ( false ) ;
158161 } ;
159162
160163 const escapeSearch = useCallback ( ( ) => {
@@ -164,11 +167,14 @@ const Search = () => {
164167 setTerms ( [ ] )
165168 setOntologies ( [ ] )
166169 setOrganizations ( [ ] )
170+ setIsSearching ( false ) ;
167171 } , [ ] ) ;
168172
169173 const handleKeyDown = useCallback ( event => {
170- if ( event . ctrlKey && event . key === 'k' ) {
174+ if ( ( event . ctrlKey || event . metaKey ) && event . key . toLowerCase ( ) === 'k' ) {
175+ event . preventDefault ( ) ;
171176 setOpenList ( true ) ;
177+ document . getElementById ( 'interlex-search-input' ) ?. focus ( ) ;
172178 }
173179 if ( event . key === 'Escape' ) {
174180 escapeSearch ( ) ;
@@ -182,13 +188,18 @@ const Search = () => {
182188
183189 // eslint-disable-next-line react-hooks/exhaustive-deps
184190 const fetchTerms = useCallback ( debounce ( async ( searchTerm ) => {
185- const data = await elasticSearch ( searchTerm , 20 , 0 ) ;
186- const dataTerms = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . TERM ) ;
187- const dataOrganizations = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . ORGANIZATION ) ;
188- const dataOntologies = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . ONTOLOGY ) ;
189- setTerms ( dataTerms ) ;
190- setOrganizations ( dataOrganizations ) ;
191- setOntologies ( dataOntologies ) ;
191+ setIsSearching ( true ) ;
192+ try {
193+ const data = await elasticSearch ( searchTerm , 20 , 0 ) ;
194+ const dataTerms = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . TERM ) ;
195+ const dataOrganizations = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . ORGANIZATION ) ;
196+ const dataOntologies = data ?. results . results ?. filter ( result => result . type === SEARCH_TYPES . ONTOLOGY ) ;
197+ setTerms ( dataTerms ) ;
198+ setOrganizations ( dataOrganizations ) ;
199+ setOntologies ( dataOntologies ) ;
200+ } finally {
201+ setIsSearching ( false ) ;
202+ }
192203 } , 500 ) , [ searchAll ] ) ;
193204
194205 useEffect ( ( ) => {
@@ -208,6 +219,7 @@ const Search = () => {
208219 const ListboxComponent = forwardRef ( function ListboxComponent ( props , ref ) {
209220 return (
210221 < >
222+ { isSearching && < LinearProgress sx = { { height : '0.125rem' } } /> }
211223 { searchTerm && ( < > < Box p = "0.5rem" >
212224 < List sx = { {
213225 '& .MuiTypography-body1' : {
@@ -364,6 +376,10 @@ const Search = () => {
364376 placeholder = "Find something..."
365377 onChange = { handleInputChange }
366378 onKeyDown = { handleEnterKey }
379+ inputProps = { {
380+ ...params . inputProps ,
381+ id : 'interlex-search-input' ,
382+ } }
367383 InputProps = { {
368384 ...params . InputProps ,
369385 startAdornment : (
@@ -373,15 +389,15 @@ const Search = () => {
373389 ) ,
374390 endAdornment : (
375391 < InputAdornment position = "end" >
376- { openList ? (
392+ { searchTerm ? (
377393 < Box display = "flex" alignItems = "center" gap = "0.75rem" >
378394 < IconButton
379395 sx = { styles . searchButton }
380396 onClick = { resetSearch }
381397 >
382398 < CloseIcon />
383399 </ IconButton >
384- < Box sx = { styles . keyBoardInfo } > Esc</ Box >
400+ { openList && < Box sx = { styles . keyBoardInfo } > Esc</ Box > }
385401 </ Box >
386402 ) : (
387403 < Box sx = { styles . keyBoardInfo } > Ctrl + K</ Box >
0 commit comments