1
1
import { IconButton , Input , InputGroup , InputRightElement , Spinner } from '@invoke-ai/ui-library' ;
2
- import { useAppDispatch , useAppSelector } from 'app/store/storeHooks' ;
2
+ import { useAppSelector } from 'app/store/storeHooks' ;
3
3
import { selectListImagesQueryArgs } from 'features/gallery/store/gallerySelectors' ;
4
- import { searchTermChanged } from 'features/gallery/store/gallerySlice' ;
5
- import { debounce } from 'lodash-es' ;
6
4
import type { ChangeEvent } from 'react' ;
7
- import { useCallback , useEffect , useMemo , useState } from 'react' ;
5
+ import { useCallback } from 'react' ;
8
6
import { useTranslation } from 'react-i18next' ;
9
7
import { PiXBold } from 'react-icons/pi' ;
10
8
import { useListImagesQuery } from 'services/api/endpoints/images' ;
11
9
12
- export const GallerySearch = ( ) => {
13
- const dispatch = useAppDispatch ( ) ;
14
- const searchTerm = useAppSelector ( ( s ) => s . gallery . searchTerm ) ;
10
+ type Props = {
11
+ searchTerm : string ;
12
+ onChangeSearchTerm : ( value : string ) => void ;
13
+ onResetSearchTerm : ( ) => void ;
14
+ } ;
15
+
16
+ export const GallerySearch = ( { searchTerm, onChangeSearchTerm, onResetSearchTerm } : Props ) => {
15
17
const { t } = useTranslation ( ) ;
16
- const [ searchTermInput , setSearchTermInput ] = useState ( searchTerm ) ;
17
18
const queryArgs = useAppSelector ( selectListImagesQueryArgs ) ;
18
19
const { isPending } = useListImagesQuery ( queryArgs , {
19
20
selectFromResult : ( { isLoading, isFetching } ) => ( { isPending : isLoading || isFetching } ) ,
20
21
} ) ;
21
- const debouncedSetSearchTerm = useMemo ( ( ) => {
22
- return debounce ( ( value : string ) => {
23
- dispatch ( searchTermChanged ( value ) ) ;
24
- } , 1000 ) ;
25
- } , [ dispatch ] ) ;
26
22
27
23
const handleChangeInput = useCallback (
28
24
( e : ChangeEvent < HTMLInputElement > ) => {
29
- setSearchTermInput ( e . target . value ) ;
30
- debouncedSetSearchTerm ( e . target . value ) ;
25
+ onChangeSearchTerm ( e . target . value ) ;
31
26
} ,
32
- [ debouncedSetSearchTerm ]
27
+ [ onChangeSearchTerm ]
33
28
) ;
34
29
35
- const handleClearInput = useCallback ( ( ) => {
36
- setSearchTermInput ( '' ) ;
37
- dispatch ( searchTermChanged ( '' ) ) ;
38
- } , [ dispatch ] ) ;
39
-
40
- useEffect ( ( ) => {
41
- setSearchTermInput ( searchTerm ) ;
42
- } , [ searchTerm ] ) ;
43
-
44
30
return (
45
31
< InputGroup >
46
32
< Input
47
33
placeholder = { t ( 'gallery.searchImages' ) }
48
- value = { searchTermInput }
34
+ value = { searchTerm }
49
35
onChange = { handleChangeInput }
50
36
data-testid = "image-search-input"
51
37
/>
@@ -54,10 +40,10 @@ export const GallerySearch = () => {
54
40
< Spinner size = "sm" opacity = { 0.5 } />
55
41
</ InputRightElement >
56
42
) }
57
- { ! isPending && searchTermInput . length && (
43
+ { ! isPending && searchTerm . length && (
58
44
< InputRightElement h = "full" pe = { 2 } >
59
45
< IconButton
60
- onClick = { handleClearInput }
46
+ onClick = { onResetSearchTerm }
61
47
size = "sm"
62
48
variant = "link"
63
49
aria-label = { t ( 'boards.clearSearch' ) }
0 commit comments