diff --git a/hooks/use-template-favorite.ts b/hooks/use-template-favorite.ts index a03dbdb..cc7979c 100644 --- a/hooks/use-template-favorite.ts +++ b/hooks/use-template-favorite.ts @@ -11,6 +11,7 @@ import { useCallback, useState } from 'react' import { type ApiError } from '@/lib/types' import { handleError } from './use-error' +import { templateSocialStore } from '@/stores/templateSocialStore' export const useTemplateFavorite = ( templateId?: string, @@ -44,6 +45,13 @@ export const useTemplateFavorite = ( } setFavorited(true) + + // 同步更新 store + templateSocialStore.setFavorited(templateId, true) + if (data?.favoriteCount !== undefined) { + templateSocialStore.setFavoriteCount(templateId, data.favoriteCount) + } + setLoading(false) return { data, error: null } }, [templateId]) @@ -72,6 +80,13 @@ export const useTemplateFavorite = ( } setFavorited(false) + + // 同步更新 store + templateSocialStore.setFavorited(templateId, false) + if (data?.favoriteCount !== undefined) { + templateSocialStore.setFavoriteCount(templateId, data.favoriteCount) + } + setLoading(false) return { data, error: null } }, [templateId]) diff --git a/hooks/use-template-like.ts b/hooks/use-template-like.ts index 0475c97..25ec455 100644 --- a/hooks/use-template-like.ts +++ b/hooks/use-template-like.ts @@ -4,6 +4,7 @@ import { useCallback, useState } from 'react' import { type ApiError } from '@/lib/types' import { handleError } from './use-error' +import { templateSocialStore } from '@/stores/templateSocialStore' export const useTemplateLike = (templateId?: string) => { const [liked, setLiked] = useState(false) @@ -31,6 +32,13 @@ export const useTemplateLike = (templateId?: string) => { } setLiked(true) + + // 同步更新 store + templateSocialStore.setLiked(templateId, true) + if (data?.likeCount !== undefined) { + templateSocialStore.setLikeCount(templateId, data.likeCount) + } + return {} }, [templateId]) @@ -55,6 +63,13 @@ export const useTemplateLike = (templateId?: string) => { } setLiked(false) + + // 同步更新 store + templateSocialStore.setLiked(templateId, false) + if (data?.likeCount !== undefined) { + templateSocialStore.setLikeCount(templateId, data.likeCount) + } + return {} }, [templateId])