fix: bug
This commit is contained in:
@@ -49,6 +49,8 @@ function groupWorksByDate(works: WorkItem[]): Record<string, WorkItem[]> {
|
||||
}
|
||||
|
||||
export default function SearchWorksResultsScreen() {
|
||||
console.log('========== SearchWorksResultsScreen 组件渲染 ==========')
|
||||
|
||||
const { t } = useTranslation()
|
||||
const router = useRouter()
|
||||
const params = useLocalSearchParams()
|
||||
@@ -57,6 +59,8 @@ export default function SearchWorksResultsScreen() {
|
||||
const [allWorks, setAllWorks] = useState<WorksSearchResult[]>([])
|
||||
const [isLoadingMore, setIsLoadingMore] = useState(false)
|
||||
|
||||
console.log('========== 当前状态:', { searchText, page, allWorksLength: allWorks.length, isLoadingMore })
|
||||
|
||||
const categories: Category[] = [
|
||||
t('worksList.all') as Category,
|
||||
t('worksList.pets') as Category,
|
||||
@@ -76,29 +80,60 @@ export default function SearchWorksResultsScreen() {
|
||||
|
||||
// 当搜索关键词或分类变化时,重置页码和累积数据
|
||||
useEffect(() => {
|
||||
console.log('[SearchResults] 重置: searchText=', searchText, 'category=', selectedCategory)
|
||||
setPage(1)
|
||||
setAllWorks([])
|
||||
}, [searchText, selectedCategory])
|
||||
|
||||
// 累积新数据
|
||||
useEffect(() => {
|
||||
if (works && works.length > 0) {
|
||||
if (page === 1) {
|
||||
setAllWorks(works)
|
||||
} else {
|
||||
setAllWorks((prev) => [...prev, ...works])
|
||||
console.log('[SearchResults] works变化:', {
|
||||
worksLength: works?.length,
|
||||
page,
|
||||
allWorksLength: allWorks.length,
|
||||
isLoading,
|
||||
data: data ? { total: data.total, totalPages: data.totalPages } : null
|
||||
})
|
||||
|
||||
if (works) {
|
||||
if (works.length > 0) {
|
||||
if (page === 1) {
|
||||
console.log('[SearchResults] 设置第1页数据')
|
||||
setAllWorks(works)
|
||||
} else {
|
||||
console.log('[SearchResults] 追加第', page, '页数据')
|
||||
setAllWorks((prev) => [...prev, ...works])
|
||||
}
|
||||
} else if (page === 1) {
|
||||
console.log('[SearchResults] 第1页无结果,清空数据')
|
||||
setAllWorks([])
|
||||
}
|
||||
setIsLoadingMore(false)
|
||||
}
|
||||
}, [works, page])
|
||||
}, [works])
|
||||
|
||||
// 加载更多函数
|
||||
const handleLoadMore = () => {
|
||||
if (isLoading || isLoadingMore || !data) return
|
||||
console.log('[SearchResults] handleLoadMore调用:', {
|
||||
isLoading,
|
||||
isLoadingMore,
|
||||
hasData: !!data,
|
||||
page,
|
||||
totalPages: data?.totalPages
|
||||
})
|
||||
|
||||
if (isLoading || isLoadingMore || !data) {
|
||||
console.log('[SearchResults] 跳过加载: 正在加载或无数据')
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否还有更多数据
|
||||
if (page >= data.totalPages) return
|
||||
if (page >= data.totalPages) {
|
||||
console.log('[SearchResults] 跳过加载: 已到最后一页')
|
||||
return
|
||||
}
|
||||
|
||||
console.log('[SearchResults] 开始加载第', page + 1, '页')
|
||||
setIsLoadingMore(true)
|
||||
setPage((prev) => prev + 1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user