1818 * SPDX-License-Identifier: Apache-2.0
1919 */
2020
21- import _ from "lodash" ;
2221import { Suspense , useCallback , useEffect , useMemo , useState } from "react" ;
2322import type { FallbackProps } from "react-error-boundary" ;
2423import { ErrorBoundary } from "react-error-boundary" ;
@@ -42,7 +41,8 @@ import type { FileSelect_getRepository_Query } from "@/api/__generated__/FileSel
4241import Button from "@/components/Button" ;
4342import Spinner from "@/components/Spinner" ;
4443import Stack from "@/components/Stack" ;
45- import { RECORDS_TO_LOAD_FIRST , RECORDS_TO_LOAD_NEXT } from "@/constants" ;
44+ import { RECORDS_TO_LOAD_FIRST } from "@/constants" ;
45+ import useRelayConnectionPagination from "@/hooks/useRelayConnectionPagination" ;
4646
4747const GET_REPOSITORY_QUERY = graphql `
4848 query FileSelect_getRepository_Query(
@@ -112,40 +112,24 @@ const FileSelect = ({ filesFragmentRef, controllerProps }: FileSelectProps) => {
112112
113113 const [ searchText , setSearchText ] = useState < string | null > ( null ) ;
114114
115- const debounceRefetch = useMemo (
116- ( ) =>
117- _ . debounce ( ( text : string ) => {
118- if ( text === "" ) {
119- refetch (
120- {
121- first : RECORDS_TO_LOAD_FIRST ,
122- } ,
123- { fetchPolicy : "network-only" } ,
124- ) ;
125- } else {
126- refetch (
127- {
128- first : RECORDS_TO_LOAD_FIRST ,
129- filter : { name : { ilike : `%${ text } %` } } ,
130- } ,
131- { fetchPolicy : "network-only" } ,
132- ) ;
133- }
134- } , 500 ) ,
135- [ refetch ] ,
136- ) ;
137-
138- useEffect ( ( ) => {
139- if ( searchText !== null ) {
140- debounceRefetch ( searchText ) ;
141- }
142- } , [ debounceRefetch , searchText ] ) ;
115+ const { onLoadMore } = useRelayConnectionPagination ( {
116+ hasNext,
117+ isLoadingNext,
118+ loadNext,
119+ refetch,
120+ searchText,
121+ buildFilter : ( text ) => {
122+ if ( text === "" ) {
123+ return undefined ;
124+ }
143125
144- const loadNextOptions = useCallback ( ( ) => {
145- if ( hasNext && ! isLoadingNext ) {
146- loadNext ( RECORDS_TO_LOAD_NEXT ) ;
147- }
148- } , [ hasNext , isLoadingNext , loadNext ] ) ;
126+ return {
127+ name : {
128+ ilike : `%${ text } %` ,
129+ } ,
130+ } ;
131+ } ,
132+ } ) ;
149133
150134 const files = useMemo ( ( ) => {
151135 return (
@@ -185,7 +169,7 @@ const FileSelect = ({ filesFragmentRef, controllerProps }: FileSelectProps) => {
185169 getOptionValue = { getFileValue }
186170 noOptionsMessage = { ( { inputValue } ) => noFileOptionsMessage ( inputValue ) }
187171 isLoading = { isLoadingNext }
188- onMenuScrollToBottom = { hasNext ? loadNextOptions : undefined }
172+ onMenuScrollToBottom = { onLoadMore }
189173 onInputChange = { ( text ) => setSearchText ( text ) }
190174 />
191175 ) ;
0 commit comments