@@ -17,7 +17,11 @@ import {
17
17
} from "@mui/material" ;
18
18
import { Autocomplete } from "@mui/material" ;
19
19
import { GenomeSearchProps , Result } from "./types" ;
20
- import { QueryClient , QueryClientProvider , useQuery } from "@tanstack/react-query" ;
20
+ import {
21
+ QueryClient ,
22
+ QueryClientProvider ,
23
+ useQuery ,
24
+ } from "@tanstack/react-query" ;
21
25
22
26
/**
23
27
* An autocomplete search component for genomic landmarks such as genes, SNPs, ICRs, and CCRs.
@@ -43,13 +47,6 @@ const Search: React.FC<GenomeSearchProps> = ({
43
47
slotProps,
44
48
...autocompleteProps
45
49
} ) => {
46
- // Boolean flags for each query
47
- const searchGene = queries . includes ( "Gene" ) ;
48
- const searchSnp = queries . includes ( "SNP" ) ;
49
- const searchICRE = queries . includes ( "iCRE" ) ;
50
- const searchCCRE = queries . includes ( "cCRE" ) ;
51
- const searchCoordinate = queries . includes ( "Coordinate" ) ;
52
-
53
50
// State variables
54
51
const [ inputValue , setInputValue ] = useState ( "" ) ;
55
52
const [ selection , setSelection ] = useState < Result > ( { } as Result ) ;
@@ -58,6 +55,12 @@ const Search: React.FC<GenomeSearchProps> = ({
58
55
) ;
59
56
const [ isLoading , setIsLoading ] = useState ( false ) ;
60
57
58
+ const searchGene = queries . includes ( "Gene" ) ;
59
+ const searchSnp = queries . includes ( "SNP" ) ;
60
+ const searchICRE = queries . includes ( "iCRE" ) ;
61
+ const searchCCRE = queries . includes ( "cCRE" ) ;
62
+ const searchCoordinate = queries . includes ( "Coordinate" ) ;
63
+
61
64
const {
62
65
data : icreData ,
63
66
refetch : refetchICREs ,
@@ -130,31 +133,43 @@ const Search: React.FC<GenomeSearchProps> = ({
130
133
useEffect ( ( ) => {
131
134
if ( isLoading ) return ;
132
135
const resultsList = [ ] ;
133
- if ( geneData ) {
136
+ if ( geneData && searchGene ) {
134
137
resultsList . push ( ...geneResultList ( geneData . data . gene , geneLimit || 3 ) ) ;
135
138
}
136
- if ( icreData && inputValue . toLowerCase ( ) . startsWith ( "eh" ) ) {
139
+ if ( icreData && searchICRE && inputValue . toLowerCase ( ) . startsWith ( "eh" ) ) {
137
140
resultsList . push (
138
141
...icreResultList ( icreData . data . iCREQuery , icreLimit || 3 )
139
142
) ;
140
143
}
141
- if ( ccreData && inputValue . toLowerCase ( ) . startsWith ( "eh" ) ) {
144
+ if ( ccreData && searchCCRE && inputValue . toLowerCase ( ) . startsWith ( "eh" ) ) {
142
145
resultsList . push (
143
146
...ccreResultList ( ccreData . data . cCREQuery , ccreLimit || 3 )
144
147
) ;
145
148
}
146
- if ( snpData && inputValue . toLowerCase ( ) . startsWith ( "rs" ) ) {
149
+ if ( snpData && searchSnp && inputValue . toLowerCase ( ) . startsWith ( "rs" ) ) {
147
150
resultsList . push (
148
151
...snpResultList ( snpData . data . snpAutocompleteQuery , snpLimit || 3 )
149
152
) ;
150
153
}
151
- if ( searchCoordinate && isDomain ( inputValue ) ) {
154
+ if ( isDomain ( inputValue ) && searchCoordinate ) {
152
155
resultsList . push ( ...getCoordinates ( inputValue , assembly ) ) ;
153
156
}
154
157
155
158
if ( resultsList . length === 0 ) setResults ( null ) ;
156
159
else setResults ( resultsList ) ;
157
- } , [ isLoading , icreData , ccreData , geneData , snpData ] ) ;
160
+ } , [
161
+ isLoading ,
162
+ icreData ,
163
+ ccreData ,
164
+ geneData ,
165
+ snpData ,
166
+ searchGene ,
167
+ searchICRE ,
168
+ searchCCRE ,
169
+ searchSnp ,
170
+ searchCoordinate ,
171
+ inputValue ,
172
+ ] ) ;
158
173
159
174
// Handle submit
160
175
const onSubmit = useCallback ( ( ) => {
@@ -184,10 +199,10 @@ const Search: React.FC<GenomeSearchProps> = ({
184
199
return (
185
200
< Box
186
201
display = "flex"
187
- flexDirection = "row"
202
+ flexDirection = "row"
188
203
gap = { 2 }
189
204
style = { { ...style } }
190
- sx = { { ...sx } }
205
+ sx = { { ...sx } }
191
206
{ ...slotProps ?. box }
192
207
>
193
208
< Autocomplete
@@ -256,31 +271,32 @@ const Search: React.FC<GenomeSearchProps> = ({
256
271
*/
257
272
function renderGroup ( params : any , inputValue : string ) {
258
273
// Sort items within each group by title match relevance
259
- const sortedOptions = Array . isArray ( params . children ) && ! isDomain ( inputValue )
260
- ? params . children . sort ( ( a : any , b : any ) => {
261
- const aTitle = (
262
- a . props ?. children ?. props ?. children ?. [ 0 ] ?. props ?. children || ""
263
- ) . toLowerCase ( ) ;
264
- const bTitle = (
265
- b . props ?. children ?. props ?. children ?. [ 0 ] ?. props ?. children || ""
266
- ) . toLowerCase ( ) ;
267
- const query = inputValue . toLowerCase ( ) ;
268
- // Exact matches first
269
- if ( aTitle === query && bTitle !== query ) return - 1 ;
270
- if ( bTitle === query && aTitle !== query ) return 1 ;
274
+ const sortedOptions =
275
+ Array . isArray ( params . children ) && ! isDomain ( inputValue )
276
+ ? params . children . sort ( ( a : any , b : any ) => {
277
+ const aTitle = (
278
+ a . props ?. children ?. props ?. children ?. [ 0 ] ?. props ?. children || ""
279
+ ) . toLowerCase ( ) ;
280
+ const bTitle = (
281
+ b . props ?. children ?. props ?. children ?. [ 0 ] ?. props ?. children || ""
282
+ ) . toLowerCase ( ) ;
283
+ const query = inputValue . toLowerCase ( ) ;
284
+ // Exact matches first
285
+ if ( aTitle === query && bTitle !== query ) return - 1 ;
286
+ if ( bTitle === query && aTitle !== query ) return 1 ;
271
287
272
- // Starts with query second
273
- if ( aTitle . startsWith ( query ) && ! bTitle . startsWith ( query ) ) return - 1 ;
274
- if ( bTitle . startsWith ( query ) && ! aTitle . startsWith ( query ) ) return 1 ;
288
+ // Starts with query second
289
+ if ( aTitle . startsWith ( query ) && ! bTitle . startsWith ( query ) ) return - 1 ;
290
+ if ( bTitle . startsWith ( query ) && ! aTitle . startsWith ( query ) ) return 1 ;
275
291
276
- // Contains query third
277
- if ( aTitle . includes ( query ) && ! bTitle . includes ( query ) ) return - 1 ;
278
- if ( bTitle . includes ( query ) && ! aTitle . includes ( query ) ) return 1 ;
292
+ // Contains query third
293
+ if ( aTitle . includes ( query ) && ! bTitle . includes ( query ) ) return - 1 ;
294
+ if ( bTitle . includes ( query ) && ! aTitle . includes ( query ) ) return 1 ;
279
295
280
- // Alphabetical order for equal relevance
281
- return aTitle . localeCompare ( bTitle ) ;
282
- } )
283
- : params . children ;
296
+ // Alphabetical order for equal relevance
297
+ return aTitle . localeCompare ( bTitle ) ;
298
+ } )
299
+ : params . children ;
284
300
285
301
return (
286
302
< div key = { params . key } >
0 commit comments