11import { TABLE_LENGTH , TABLE_REFETCH_INTERVAL } from '@/config' ;
22import { execute } from '@/graphql/poco/execute' ;
3+ import { Workerpool_OrderBy , OrderDirection } from '@/graphql/poco/graphql' ;
34import { useQuery } from '@tanstack/react-query' ;
45import { createFileRoute } from '@tanstack/react-router' ;
56import { LoaderCircle } from 'lucide-react' ;
67import { DataTable } from '@/components/DataTable' ;
78import { PaginatedNavigation } from '@/components/PaginatedNavigation' ;
89import WorkerpoolIcon from '@/components/icons/WorkerpoolIcon' ;
910import { BackButton } from '@/components/ui/BackButton' ;
11+ import {
12+ Select ,
13+ SelectContent ,
14+ SelectItem ,
15+ SelectTrigger ,
16+ } from '@/components/ui/select' ;
17+ import { useFilterParam } from '@/hooks/useFilterParam' ;
1018import { usePageParam } from '@/hooks/usePageParam' ;
1119import { ErrorAlert } from '@/modules/ErrorAlert' ;
1220import { SearcherBar } from '@/modules/search/SearcherBar' ;
1321import { WorkerpoolBreadcrumbsList } from '@/modules/workerpools/WorkerpoolBreadcrumbs' ;
1422import { workerpoolsQuery } from '@/modules/workerpools/workerpoolsQuery' ;
1523import { columns } from '@/modules/workerpools/workerpoolsTable/columns' ;
1624import useUserStore from '@/stores/useUser.store' ;
17- import { createPlaceholderDataFnForQueryKey } from '@/utils/createPlaceholderDataFnForQueryKey' ;
25+ import { createPlaceholderDataFn } from '@/utils/createPlaceholderDataFnForQueryKey' ;
1826import { getAdditionalPages } from '@/utils/format' ;
1927
2028export const Route = createFileRoute ( '/$chainSlug/_layout/workerpools' ) ( {
2129 component : WorkerpoolsRoute ,
2230} ) ;
2331
24- function useWorkerpoolsData ( currentPage : number ) {
32+ function useWorkerpoolsData ( currentPage : number , orderFilter : string ) {
2533 const { chainId } = useUserStore ( ) ;
2634 const skip = currentPage * TABLE_LENGTH ;
2735 const nextSkip = skip + TABLE_LENGTH ;
2836 const nextNextSkip = skip + 2 * TABLE_LENGTH ;
37+ const orderBy = orderFilter === 'pertinent' ? 'usageCount' : 'timestamp' ;
38+ const orderDirection = orderFilter === 'oldest' ? 'asc' : 'desc' ;
39+ const recentFrom =
40+ orderFilter === 'pertinent'
41+ ? Math . floor ( Date . now ( ) / 1000 ) - 14 * 24 * 60 * 60
42+ : 0 ;
2943
30- const queryKey = [ chainId , 'workerpools' , currentPage ] ;
44+ const queryKey = [
45+ chainId ,
46+ 'workerpools' ,
47+ currentPage ,
48+ orderBy ,
49+ orderDirection ,
50+ recentFrom ,
51+ ] ;
3152 const { data, isLoading, isRefetching, isError, errorUpdateCount } = useQuery (
3253 {
3354 queryKey,
@@ -37,10 +58,13 @@ function useWorkerpoolsData(currentPage: number) {
3758 skip,
3859 nextSkip,
3960 nextNextSkip,
61+ orderBy : orderBy as Workerpool_OrderBy ,
62+ orderDirection : orderDirection as OrderDirection ,
63+ recentFrom,
4064 } ) ,
4165 refetchInterval : TABLE_REFETCH_INTERVAL ,
4266 enabled : ! ! chainId ,
43- placeholderData : createPlaceholderDataFnForQueryKey ( queryKey ) ,
67+ placeholderData : createPlaceholderDataFn ( ) ,
4468 }
4569 ) ;
4670
@@ -68,20 +92,54 @@ function useWorkerpoolsData(currentPage: number) {
6892}
6993
7094function WorkerpoolsRoute ( ) {
95+ const orders = [
96+ { id : 1 , value : 'recent' , name : 'Recently deployed' } ,
97+ { id : 2 , value : 'oldest' , name : 'Oldest deployed' } ,
98+ { id : 3 , value : 'pertinent' , name : 'Most pertinent' } ,
99+ ] ;
100+ const allowedOrderValues = orders . map ( ( o ) => o . value ) ;
71101 const [ currentPage , setCurrentPage ] = usePageParam ( 'workerpoolsPage' ) ;
102+ const [ orderByFilter , setOrderByFilter ] = useFilterParam (
103+ 'workerpoolsOrderBy' ,
104+ allowedOrderValues ,
105+ 'pertinent'
106+ ) ;
72107 const {
73108 data,
74109 isLoading,
75110 isRefetching,
76111 isError,
77112 hasPastError,
78113 additionalPages,
79- } = useWorkerpoolsData ( currentPage - 1 ) ;
114+ } = useWorkerpoolsData ( currentPage - 1 , orderByFilter ) ;
115+
116+ function handleOrderChange ( value : string ) {
117+ setOrderByFilter ( value ) ;
118+ setCurrentPage ( 1 ) ;
119+ }
80120
81121 return (
82122 < div className = "mt-8 grid gap-6" >
83123 < div className = "mt-6 flex flex-col justify-between lg:flex-row" >
84- < SearcherBar className = "py-6 lg:order-last lg:mr-0 lg:max-w-md lg:py-0 xl:max-w-xl" />
124+ < div className = "flex flex-col items-stretch gap-4 py-6 sm:flex-row lg:order-last lg:mr-0 lg:py-0" >
125+ < SearcherBar className = "lg:max-w-md xl:max-w-xl" />
126+ < Select
127+ value = { orderByFilter ?. toString ( ) }
128+ onValueChange = { handleOrderChange }
129+ defaultValue = "pertinent"
130+ >
131+ < SelectTrigger className = "m-auto box-content h-9! rounded-2xl" >
132+ Order by
133+ </ SelectTrigger >
134+ < SelectContent >
135+ { orders . map ( ( order ) => (
136+ < SelectItem key = { order . id } value = { order . value } >
137+ { order . name }
138+ </ SelectItem >
139+ ) ) }
140+ </ SelectContent >
141+ </ Select >
142+ </ div >
85143 < div className = "space-y-2" >
86144 < h1 className = "flex items-center gap-2 font-sans text-2xl font-extrabold" >
87145 < WorkerpoolIcon size = { 24 } />
@@ -116,6 +174,7 @@ function WorkerpoolsRoute() {
116174 currentPage = { currentPage }
117175 totalPages = { currentPage + additionalPages }
118176 onPageChange = { setCurrentPage }
177+ filterKey = { orderByFilter }
119178 />
120179 </ div >
121180 ) ;
0 commit comments