Skip to content

Commit

Permalink
feat: adjust removeUnnecessaryPages logic
Browse files Browse the repository at this point in the history
  • Loading branch information
liaoliao666 committed Sep 14, 2023
1 parent 4f36196 commit a39caae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ios": {
"supportsTablet": true,
"bundleIdentifier": "com.liaoliao666.v2ex",
"buildNumber": "1.4.6.1"
"buildNumber": "1.4.6.2"
},
"android": {
"adaptiveIcon": {
Expand Down
4 changes: 3 additions & 1 deletion components/RadioButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default function RadioButtonGroup<
<Pressable
key={item.value ?? '$k$'}
onPress={() => {
onChange(item.value)
if (item.value !== value) {
onChange(item.value)
}
}}
style={tw.style(
`px-1.5 flex-row items-center rounded-lg`,
Expand Down
2 changes: 1 addition & 1 deletion screens/LoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export default function LoginScreen() {
)}
/>

{(Platform.OS === 'android' || dayjs().isAfter('2023-9-16')) && (
{(Platform.OS === 'android' || dayjs().isAfter('2023-9-17')) && (
<TouchableOpacity
style={tw`w-full mt-4 flex-row justify-center items-center h-[52px] px-8`}
onPress={() => {
Expand Down
23 changes: 19 additions & 4 deletions screens/TopicDetailScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
View,
} from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import Toast from 'react-native-toast-message'
import { inferData } from 'react-query-kit'

import IconButton from '@/components/IconButton'
Expand Down Expand Up @@ -70,10 +71,16 @@ type OrderBy = 'asc' | 'desc'
function TopicDetailScreen() {
const { params } = useRoute<RouteProp<RootStackParamList, 'TopicDetail'>>()

const { data, refetch, hasNextPage, fetchNextPage, isFetchingNextPage } =
useTopicDetail({
variables: { id: params.id },
})
const {
data,
refetch,
hasNextPage,
fetchNextPage,
isFetchingNextPage,
isFetching,
} = useTopicDetail({
variables: { id: params.id },
})

const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch)

Expand Down Expand Up @@ -178,6 +185,14 @@ function TopicDetailScreen() {
}
value={orderBy}
onChange={async v => {
if (v === 'desc' && isFetching) {
Toast.show({
type: 'error',
text1: '请等待当前请求完成后再切换排序方式',
})
return
}

setOrderBy(v)

if (v === 'desc' && hasNextPage) {
Expand Down
29 changes: 23 additions & 6 deletions utils/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'
import { QueryClient, focusManager } from '@tanstack/react-query'
import { isUndefined, pick } from 'lodash-es'
import { isArray, isObjectLike, isUndefined } from 'lodash-es'
import { useMemo } from 'react'
import { AppState, Platform } from 'react-native'
import { InfiniteQueryHook, Middleware, getKey } from 'react-query-kit'
Expand All @@ -13,12 +13,29 @@ const removeUnnecessaryPages: Middleware<InfiniteQueryHook> =
!isUndefined(options.getNextPageParam) && options.enabled !== false

if (isValidInfiniteQuery) {
queryClient.prefetchInfiniteQuery({
queryKey: getKey(options.primaryKey, options.variables),
pages: 1,
...pick(options, ['queryFn', 'initialPageParam', 'getNextPageParam']),
})
queryClient
.getQueryCache()
.findAll({ queryKey: getKey(options.primaryKey, options.variables) })
.forEach(query => {
const data: any = query.state.data
const isInfiniteQuery =
isObjectLike(data) &&
isArray(data.pages) &&
isArray(data.pageParams)
if (
isInfiniteQuery &&
query.state.status === 'success' &&
data.pages.length >= 2
) {
// only keep one page before mount
query.setData({
pages: [data.pages[0]],
pageParams: [data.pageParams[0]],
})
}
})
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

Expand Down

0 comments on commit a39caae

Please sign in to comment.