@@ -14,15 +14,16 @@ import {
1414 TableCell ,
1515} from 'components/shared/ui/Table' ;
1616import { Relay } from 'core/relay/type' ;
17- import useAllRelaysQuery , {
18- useGetAllRelaysCountQuery ,
19- } from 'pages/relay-manager/hooks/useGetAllRelaysQuery' ;
2017import { FaCircle } from 'react-icons/fa6' ;
2118import { cn } from 'utils/classnames' ;
2219import { Pagination } from 'components/shared/Pagination' ;
23- import { useState } from 'react' ;
20+ import { useMemo , useState } from 'react' ;
2421import { Button } from 'components/shared/ui/Button' ;
2522import { toast } from 'components/shared/ui/Toast/use-toast' ;
23+ import useAllRelaysQuery , {
24+ useGetAllRelaysCountQuery ,
25+ } from 'pages/relay-manager/hooks/useGetAllRelaysQuery' ;
26+ import useFindRelaysByKeywordQuery from 'pages/relay-manager/hooks/useFindRelayByKeywordQuery' ;
2627import CopyToGroupModal from '../CopyToGroupModal' ;
2728import useCopyRelaysMutation from 'pages/relay-manager/hooks/useCopyRelaysMutation' ;
2829
@@ -110,14 +111,39 @@ export const columns: ColumnDef<Relay>[] = [
110111 } ,
111112] ;
112113
113- export default function RelayPool ( ) {
114+ export interface RelayPoolProp {
115+ keyword ?: string ;
116+ }
117+
118+ export default function RelayPool ( { keyword } : RelayPoolProp ) {
114119 const [ pageIndex , setPageIndex ] = useState ( 1 ) ;
115- const { data : relays = [ ] } = useAllRelaysQuery ( pageIndex , 20 ) ;
116- const { data : totalCount = 0 } = useGetAllRelaysCountQuery ( ) ;
120+
121+ const limit = 20 ;
122+ const isSearching = useMemo ( ( ) => keyword && keyword . length > 0 , [ keyword ] ) ;
123+
124+ const { data : searchResult } = useFindRelaysByKeywordQuery (
125+ keyword ,
126+ pageIndex ,
127+ limit ,
128+ ) ;
129+ const { data : relays = [ ] } = useAllRelaysQuery ( pageIndex , limit ) ;
130+ const { data : getAllTotalCount = 0 } = useGetAllRelaysCountQuery ( ) ;
131+
132+ const pageCount = useMemo ( ( ) => {
133+ const totalCount = isSearching
134+ ? ( searchResult ?. [ 1 ] as number )
135+ : getAllTotalCount ;
136+ return Math . ceil ( totalCount / 20 ) ;
137+ } , [ isSearching , searchResult , getAllTotalCount ] ) ;
138+
139+ const tableData = useMemo ( ( ) => {
140+ return isSearching ? ( searchResult ?. [ 0 ] as Relay [ ] ) || [ ] : relays ;
141+ } , [ isSearching , searchResult , relays ] ) ;
142+
117143 const copyMutation = useCopyRelaysMutation ( ) ;
118144
119145 const table = useReactTable ( {
120- data : relays ,
146+ data : tableData ,
121147 columns,
122148 getCoreRowModel : getCoreRowModel ( ) ,
123149 } ) ;
@@ -190,7 +216,7 @@ export default function RelayPool() {
190216 < div className = "flex mt-8 mb-20 justify-center" >
191217 < Pagination
192218 pageIndex = { pageIndex }
193- pageCount = { Math . ceil ( totalCount / 20 ) }
219+ pageCount = { pageCount }
194220 onPageChange = { ( page : number ) => setPageIndex ( page ) }
195221 />
196222 </ div >
0 commit comments