This commit is contained in:
imeepos
2026-01-29 16:20:01 +08:00
parent 7c3b3c066c
commit 1ccdc23355
6 changed files with 315 additions and 229 deletions

View File

@@ -21,8 +21,8 @@ export const useUserLikes = (initialParams?: GetUserLikesParams) => {
const [loadingMore, setLoadingMore] = useState(false)
const [error, setError] = useState<ApiError | null>(null)
const [data, setData] = useState<GetUserLikesResponse>()
const [hasMore, setHasMore] = useState(true)
const currentPageRef = useRef(1)
const hasMoreRef = useRef(true)
const execute = useCallback(async (params?: GetUserLikesParams) => {
setLoading(true)
@@ -52,14 +52,15 @@ export const useUserLikes = (initialParams?: GetUserLikesParams) => {
const total = data?.total || 0
const limit = requestParams.limit || 20
const totalPages = Math.ceil(total / limit)
hasMoreRef.current = currentPage < totalPages
const hasMoreValue = currentPage < totalPages
setHasMore(hasMoreValue)
setData(data)
setLoading(false)
return { data, error: null }
}, [initialParams])
const loadMore = useCallback(async () => {
if (loadingMore || loading || !hasMoreRef.current) return { data: undefined, error: null }
if (loadingMore || loading || !hasMore) return { data: undefined, error: null }
setLoadingMore(true)
const nextPage = currentPageRef.current + 1
@@ -84,7 +85,8 @@ export const useUserLikes = (initialParams?: GetUserLikesParams) => {
const total = newData?.total || 0
const limit = requestParams.limit || 20
const totalPages = Math.ceil(total / limit)
hasMoreRef.current = nextPage < totalPages
const hasMoreValue = nextPage < totalPages
setHasMore(hasMoreValue)
currentPageRef.current = nextPage
setData((prev) => ({
@@ -93,10 +95,10 @@ export const useUserLikes = (initialParams?: GetUserLikesParams) => {
}))
setLoadingMore(false)
return { data: newData, error: null }
}, [loading, loadingMore, initialParams])
}, [loading, loadingMore, hasMore, initialParams])
const refetch = useCallback(() => {
hasMoreRef.current = true
setHasMore(true)
return execute()
}, [execute])
@@ -105,10 +107,10 @@ export const useUserLikes = (initialParams?: GetUserLikesParams) => {
likes: data?.likes || [],
loading,
loadingMore,
hasMore,
error,
execute,
refetch,
loadMore,
hasMore: hasMoreRef.current,
}
}