fix: search bug
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import React, { useState, useEffect, useRef } from 'react'
|
||||
import {
|
||||
StyleSheet,
|
||||
StatusBar as RNStatusBar,
|
||||
@@ -53,6 +53,9 @@ export default function SearchWorksResultsScreen() {
|
||||
const router = useRouter()
|
||||
const params = useLocalSearchParams()
|
||||
const [searchText, setSearchText] = useState((params.q as string) || '')
|
||||
const [page, setPage] = useState(1)
|
||||
const [allWorks, setAllWorks] = useState<WorksSearchResult[]>([])
|
||||
const [isLoadingMore, setIsLoadingMore] = useState(false)
|
||||
|
||||
const categories: Category[] = [
|
||||
t('worksList.all') as Category,
|
||||
@@ -67,15 +70,44 @@ export default function SearchWorksResultsScreen() {
|
||||
const { data, works, isLoading, error } = useWorksSearch({
|
||||
keyword: searchText,
|
||||
category: selectedCategory,
|
||||
page: 1,
|
||||
page,
|
||||
limit: 20,
|
||||
})
|
||||
|
||||
// 当搜索关键词或分类变化时,重置页码和累积数据
|
||||
useEffect(() => {
|
||||
setPage(1)
|
||||
setAllWorks([])
|
||||
}, [searchText, selectedCategory])
|
||||
|
||||
// 累积新数据
|
||||
useEffect(() => {
|
||||
if (works && works.length > 0) {
|
||||
if (page === 1) {
|
||||
setAllWorks(works)
|
||||
} else {
|
||||
setAllWorks((prev) => [...prev, ...works])
|
||||
}
|
||||
setIsLoadingMore(false)
|
||||
}
|
||||
}, [works, page])
|
||||
|
||||
// 加载更多函数
|
||||
const handleLoadMore = () => {
|
||||
if (isLoading || isLoadingMore || !data) return
|
||||
|
||||
// 检查是否还有更多数据
|
||||
if (page >= data.totalPages) return
|
||||
|
||||
setIsLoadingMore(true)
|
||||
setPage((prev) => prev + 1)
|
||||
}
|
||||
|
||||
// 根据分类过滤结果(如果 API 不支持分类筛选,则在前端过滤)
|
||||
const filteredWorks =
|
||||
selectedCategory === categories[0]
|
||||
? works
|
||||
: works.filter((work) => {
|
||||
? allWorks
|
||||
: allWorks.filter((work) => {
|
||||
const convertedWork = convertToWorkItem(work)
|
||||
return convertedWork.category === selectedCategory
|
||||
})
|
||||
@@ -190,6 +222,15 @@ export default function SearchWorksResultsScreen() {
|
||||
params: { id: id.toString() },
|
||||
})
|
||||
}}
|
||||
onEndReached={handleLoadMore}
|
||||
ListFooterComponent={
|
||||
isLoadingMore ? (
|
||||
<View style={styles.loadingMoreContainer} testID="loading-more-indicator">
|
||||
<ActivityIndicator size="small" color="#FF6699" />
|
||||
<Text style={styles.loadingMoreText}>{t('search.loadingMore')}</Text>
|
||||
</View>
|
||||
) : undefined
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</SafeAreaView>
|
||||
@@ -234,4 +275,15 @@ const styles = StyleSheet.create({
|
||||
color: '#8A8A8A',
|
||||
fontSize: 14,
|
||||
},
|
||||
loadingMoreContainer: {
|
||||
flexDirection: 'row',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
paddingVertical: 20,
|
||||
gap: 8,
|
||||
},
|
||||
loadingMoreText: {
|
||||
color: '#F5F5F5',
|
||||
fontSize: 12,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user