11import { Picker } from '@react-native-picker/picker' ;
22import { useRouter } from 'next/router' ;
3- import { useContext , useEffect , useState } from 'react' ;
3+ import { useContext , useState } from 'react' ;
44import { Pressable , StyleSheet , View } from 'react-native' ;
55
66import { colors , darkColors , P } from '~/common/styleguide' ;
@@ -15,75 +15,23 @@ type SortButtonProps = {
1515 query : Query ;
1616} ;
1717
18- const sorts : { param : QueryOrder ; label : string } [ ] = [
19- {
20- param : 'relevance' ,
21- label : 'Relevance' ,
22- } ,
23- {
24- param : 'updated' ,
25- label : 'Last Updated' ,
26- } ,
27- {
28- param : 'added' ,
29- label : 'Recently Added' ,
30- } ,
31- {
32- param : 'released' ,
33- label : 'Recently Released' ,
34- } ,
35- {
36- param : 'quality' ,
37- label : 'Quality' ,
38- } ,
39- {
40- param : 'popularity' ,
41- label : 'Popularity Gain' ,
42- } ,
43- {
44- param : 'downloads' ,
45- label : 'Downloads' ,
46- } ,
47- {
48- param : 'stars' ,
49- label : 'Stars' ,
50- } ,
51- {
52- param : 'issues' ,
53- label : 'Issues' ,
54- } ,
55- {
56- param : 'dependencies' ,
57- label : 'Dependencies' ,
58- } ,
59- {
60- param : 'size' ,
61- label : 'Bundle Size' ,
62- } ,
63- ] ;
64-
65- export const SortButton = ( { query : { order, direction, offset } , query } : SortButtonProps ) => {
66- const [ sortValue , setSortValue ] = useState < QueryOrder | undefined > ( order ) ;
67- const [ sortDirection , setSortDirection ] = useState < QueryOrderDirection | undefined > ( direction ) ;
68- const [ paginationOffset , setPaginationOffset ] = useState < string | null | undefined > (
69- typeof offset === 'string' ? offset : offset
70- ) ;
18+ export const SortButton = ( {
19+ query : { order, direction, offset, search } ,
20+ query,
21+ } : SortButtonProps ) => {
7122 const [ isSortIconHovered , setIsSortIconHovered ] = useState ( false ) ;
7223
73- const { pathname , push } = useRouter ( ) ;
24+ const { asPath , push } = useRouter ( ) ;
7425 const { isDark } = useContext ( CustomAppearanceContext ) ;
7526
76- useEffect ( ( ) => {
77- const url = urlWithQuery ( '/' , {
78- ...query ,
79- order : sortValue ,
80- direction : sortDirection ,
81- offset : paginationOffset ,
82- } ) ;
83- if ( url !== pathname ) {
27+ const currentSortValue : QueryOrder | undefined = order ;
28+ const currentDirection : QueryOrderDirection | undefined = direction ;
29+
30+ function updatePath ( url : string ) {
31+ if ( url !== asPath ) {
8432 void push ( url ) ;
8533 }
86- } , [ sortValue , sortDirection , paginationOffset ] ) ;
34+ }
8735
8836 return (
8937 < View
@@ -99,16 +47,16 @@ export const SortButton = ({ query: { order, direction, offset }, query }: SortB
9947 < Pressable
10048 onHoverIn = { ( ) => setIsSortIconHovered ( true ) }
10149 onHoverOut = { ( ) => setIsSortIconHovered ( false ) }
102- style = { sortDirection === 'ascending' && styles . flippedIcon }
50+ style = { currentDirection === 'ascending' && styles . flippedIcon }
10351 aria-label = "Toggle sort direction"
10452 role = "button"
10553 onPress = { ( ) => {
106- setSortDirection ( previousOrder =>
107- previousOrder === 'ascending' ? 'descending' : 'ascending'
54+ updatePath (
55+ urlWithQuery ( '/' , {
56+ ...query ,
57+ direction : currentDirection === 'ascending' ? 'descending' : 'ascending' ,
58+ } )
10859 ) ;
109- if ( ! sortValue ) {
110- setSortValue ( 'relevance' ) ;
111- }
11260 } } >
11361 < SortIcon fill = { isSortIconHovered ? colors . primary : colors . white } />
11462 </ Pressable >
@@ -121,18 +69,23 @@ export const SortButton = ({ query: { order, direction, offset }, query }: SortB
12169 < Picker
12270 id = "sort-order"
12371 aria-label = "Sort order"
124- selectedValue = { sortValue }
72+ selectedValue = { currentSortValue ?? ( search ? 'relevance' : 'updated' ) }
12573 style = { [
12674 styles . picker ,
12775 {
12876 backgroundColor : isDark ? darkColors . border : 'transparent' ,
12977 } ,
13078 ] }
13179 onValueChange = { value => {
132- setPaginationOffset ( null ) ;
133- setSortValue ( value ) ;
80+ updatePath (
81+ urlWithQuery ( '/' , {
82+ ...query ,
83+ order : value ,
84+ offset : null ,
85+ } )
86+ ) ;
13487 } } >
135- { sorts . map ( sort => (
88+ { SORTS . map ( sort => (
13689 < Picker . Item
13790 key = { sort . param }
13891 value = { sort . param }
@@ -146,6 +99,53 @@ export const SortButton = ({ query: { order, direction, offset }, query }: SortB
14699 ) ;
147100} ;
148101
102+ const SORTS : { param : QueryOrder ; label : string } [ ] = [
103+ {
104+ param : 'relevance' ,
105+ label : 'Relevance' ,
106+ } ,
107+ {
108+ param : 'updated' ,
109+ label : 'Last Updated' ,
110+ } ,
111+ {
112+ param : 'added' ,
113+ label : 'Recently Added' ,
114+ } ,
115+ {
116+ param : 'released' ,
117+ label : 'Recently Released' ,
118+ } ,
119+ {
120+ param : 'quality' ,
121+ label : 'Quality' ,
122+ } ,
123+ {
124+ param : 'popularity' ,
125+ label : 'Popularity Gain' ,
126+ } ,
127+ {
128+ param : 'downloads' ,
129+ label : 'Downloads' ,
130+ } ,
131+ {
132+ param : 'stars' ,
133+ label : 'Stars' ,
134+ } ,
135+ {
136+ param : 'issues' ,
137+ label : 'Issues' ,
138+ } ,
139+ {
140+ param : 'dependencies' ,
141+ label : 'Dependencies' ,
142+ } ,
143+ {
144+ param : 'size' ,
145+ label : 'Bundle Size' ,
146+ } ,
147+ ] ;
148+
149149const styles = StyleSheet . create ( {
150150 container : {
151151 backgroundColor : colors . gray5 ,
0 commit comments