From 0d8302092601eba6e0a284a35781e1bdf0af806e Mon Sep 17 00:00:00 2001 From: imeepos Date: Wed, 28 Jan 2026 20:14:45 +0800 Subject: [PATCH] fix: pass initial count to hooks for correct optimistic update - Add initialLikeCount and initialFavoriteCount parameters - Use item.likeCount/item.favoriteCount as fallback - Fixes issue where count resets to 0 on first click --- app/(tabs)/video.tsx | 4 ++-- hooks/use-template-favorite.ts | 11 ++++++----- hooks/use-template-like.ts | 12 ++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/(tabs)/video.tsx b/app/(tabs)/video.tsx index 4ce38f5..6966836 100644 --- a/app/(tabs)/video.tsx +++ b/app/(tabs)/video.tsx @@ -92,8 +92,8 @@ export const VideoItem = memo(({ item, videoHeight }: { item: TemplateDetail; vi const favoriteCount = useTemplateFavoriteCount(item.id) ?? item.favoriteCount // 使用 hooks 获取操作函数 - const { like: onLike, unlike: onUnlike, loading: likeLoading } = useTemplateLike(item.id) - const { favorite: onFavorite, unfavorite: onUnfavorite, loading: favoriteLoading } = useTemplateFavorite(item.id) + const { like: onLike, unlike: onUnlike, loading: likeLoading } = useTemplateLike(item.id, item.likeCount) + const { favorite: onFavorite, unfavorite: onUnfavorite, loading: favoriteLoading } = useTemplateFavorite(item.id, false, item.favoriteCount) const handleImageLoad = useCallback((event: ImageLoadEventData) => { const { source } = event diff --git a/hooks/use-template-favorite.ts b/hooks/use-template-favorite.ts index e34e701..95f8487 100644 --- a/hooks/use-template-favorite.ts +++ b/hooks/use-template-favorite.ts @@ -16,6 +16,7 @@ import { templateSocialStore } from '@/stores/templateSocialStore' export const useTemplateFavorite = ( templateId?: string, initialFavorited = false, + initialFavoriteCount?: number, ) => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) @@ -29,7 +30,7 @@ export const useTemplateFavorite = ( setError(null) // 乐观更新:立即更新状态 - const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? 0 + const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? initialFavoriteCount ?? 0 templateSocialStore.setFavorited(templateId, true) templateSocialStore.setFavoriteCount(templateId, currentCount + 1) @@ -58,7 +59,7 @@ export const useTemplateFavorite = ( } return { data, error: null } - }, [templateId]) + }, [templateId, initialFavoriteCount]) const unfavorite = useCallback(async () => { if (!templateId) { @@ -69,7 +70,7 @@ export const useTemplateFavorite = ( setError(null) // 乐观更新:立即更新状态 - const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? 0 + const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? initialFavoriteCount ?? 0 templateSocialStore.setFavorited(templateId, false) templateSocialStore.setFavoriteCount(templateId, Math.max(0, currentCount - 1)) @@ -98,7 +99,7 @@ export const useTemplateFavorite = ( } return { data, error: null } - }, [templateId]) + }, [templateId, initialFavoriteCount]) const checkFavorited = useCallback(async () => { if (!templateId) { @@ -129,7 +130,7 @@ export const useTemplateFavorite = ( } return { data, error: null } - }, [templateId]) + }, [templateId, initialFavoriteCount]) return { loading, diff --git a/hooks/use-template-like.ts b/hooks/use-template-like.ts index a6fb3d1..cdb4ac7 100644 --- a/hooks/use-template-like.ts +++ b/hooks/use-template-like.ts @@ -6,7 +6,7 @@ import { type ApiError } from '@/lib/types' import { handleError } from './use-error' import { templateSocialStore } from '@/stores/templateSocialStore' -export const useTemplateLike = (templateId?: string) => { +export const useTemplateLike = (templateId?: string, initialLikeCount?: number) => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) @@ -19,7 +19,7 @@ export const useTemplateLike = (templateId?: string) => { setError(null) // 乐观更新:立即更新状态 - const currentCount = templateSocialStore.getLikeCount(templateId) ?? 0 + const currentCount = templateSocialStore.getLikeCount(templateId) ?? initialLikeCount ?? 0 templateSocialStore.setLiked(templateId, true) templateSocialStore.setLikeCount(templateId, currentCount + 1) @@ -44,7 +44,7 @@ export const useTemplateLike = (templateId?: string) => { } return {} - }, [templateId]) + }, [templateId, initialLikeCount]) const unlike = useCallback(async (): Promise<{ error?: ApiError }> => { if (!templateId) { @@ -55,7 +55,7 @@ export const useTemplateLike = (templateId?: string) => { setError(null) // 乐观更新:立即更新状态 - const currentCount = templateSocialStore.getLikeCount(templateId) ?? 0 + const currentCount = templateSocialStore.getLikeCount(templateId) ?? initialLikeCount ?? 0 templateSocialStore.setLiked(templateId, false) templateSocialStore.setLikeCount(templateId, Math.max(0, currentCount - 1)) @@ -80,7 +80,7 @@ export const useTemplateLike = (templateId?: string) => { } return {} - }, [templateId]) + }, [templateId, initialLikeCount]) const checkLiked = useCallback(async (): Promise<{ error?: ApiError }> => { if (!templateId) { @@ -106,7 +106,7 @@ export const useTemplateLike = (templateId?: string) => { templateSocialStore.setLiked(templateId, data.liked) } return {} - }, [templateId]) + }, [templateId, initialLikeCount]) return { loading,