Skip to content

Commit 16c5e33

Browse files
authored
fix elements desync with query params, remove unused assets (#1991)
1 parent 728c976 commit 16c5e33

File tree

5 files changed

+83
-77
lines changed

5 files changed

+83
-77
lines changed

app.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"expo": {
33
"name": "React Native Directory",
44
"slug": "react-native-directory",
5-
"privacy": "public",
65
"platforms": [
76
"web"
87
],
98
"version": "1.0.0",
10-
"icon": "./assets/icon.png",
9+
"icon": "./assets/logo.png",
1110
"updates": {
1211
"fallbackToCacheTimeout": 0
1312
},

assets/icon.png

-1.07 KB
Binary file not shown.

assets/share-bg.png

-5.01 KB
Binary file not shown.

components/Search.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const Search = ({ query, total }: Props) => {
2323
const { search, order, direction, offset, ...filterParams } = query;
2424
const [isInputFocused, setInputFocused] = useState(false);
2525
const [isFilterVisible, setFilterVisible] = useState(Object.keys(filterParams).length > 0);
26+
2627
const isApple = useMemo<boolean>(() => isAppleDevice(), []);
2728
const inputRef = useRef<TextInput>(null);
2829

@@ -43,12 +44,18 @@ const Search = ({ query, total }: Props) => {
4344
}
4445
}, [isApple]);
4546

47+
useEffect(() => {
48+
if (inputRef?.current && !search) {
49+
inputRef.current.clear();
50+
}
51+
}, [search]);
52+
4653
const typingCallback = useDebouncedCallback((text: string) => {
4754
void replace(urlWithQuery('/', { ...query, search: text, offset: null }));
4855
}, 200);
4956

5057
function handleClearAllPress() {
51-
void replace(urlWithQuery('/', { search: query.search, offset: undefined }));
58+
void replace(urlWithQuery('/', { search, offset: undefined }));
5259
}
5360

5461
return (

components/Sort.tsx

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Picker } from '@react-native-picker/picker';
22
import { useRouter } from 'next/router';
3-
import { useContext, useEffect, useState } from 'react';
3+
import { useContext, useState } from 'react';
44
import { Pressable, StyleSheet, View } from 'react-native';
55

66
import { 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+
149149
const styles = StyleSheet.create({
150150
container: {
151151
backgroundColor: colors.gray5,

0 commit comments

Comments
 (0)