import { root } from '@repo/core' import { type CheckFavoritedInput, type CheckFavoritedResponse, type FavoriteTemplateInput, TemplateSocialController, type UnfavoriteTemplateInput, } from '@repo/sdk' 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, initialFavorited = false, initialFavoriteCount?: number, ) => { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const favorite = useCallback(async () => { if (!templateId) { return { data: null, error: null } } setLoading(true) setError(null) // 乐观更新:立即更新状态 const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? initialFavoriteCount ?? 0 templateSocialStore.setFavorited(templateId, true) templateSocialStore.setFavoriteCount(templateId, currentCount + 1) const controller = root.get(TemplateSocialController) const params: FavoriteTemplateInput = { templateId, } const { data, error } = await handleError( async () => await controller.favorite(params), ) setLoading(false) if (error) { // 回滚乐观更新 templateSocialStore.setFavorited(templateId, false) templateSocialStore.setFavoriteCount(templateId, currentCount) setError(error) return { data: null, error } } // 使用服务器返回的准确数量 if (data?.favoriteCount !== undefined) { templateSocialStore.setFavoriteCount(templateId, data.favoriteCount) } return { data, error: null } }, [templateId, initialFavoriteCount]) const unfavorite = useCallback(async () => { if (!templateId) { return { data: null, error: null } } setLoading(true) setError(null) // 乐观更新:立即更新状态 const currentCount = templateSocialStore.getFavoriteCount(templateId) ?? initialFavoriteCount ?? 0 templateSocialStore.setFavorited(templateId, false) templateSocialStore.setFavoriteCount(templateId, Math.max(0, currentCount - 1)) const controller = root.get(TemplateSocialController) const params: UnfavoriteTemplateInput = { templateId, } const { data, error } = await handleError( async () => await controller.unfavorite(params), ) setLoading(false) if (error) { // 回滚乐观更新 templateSocialStore.setFavorited(templateId, true) templateSocialStore.setFavoriteCount(templateId, currentCount) setError(error) return { data: null, error } } // 使用服务器返回的准确数量 if (data?.favoriteCount !== undefined) { templateSocialStore.setFavoriteCount(templateId, data.favoriteCount) } return { data, error: null } }, [templateId, initialFavoriteCount]) const checkFavorited = useCallback(async () => { if (!templateId) { return { data: null, error: null } } setLoading(true) setError(null) const controller = root.get(TemplateSocialController) const params: CheckFavoritedInput = { templateId, } const { data, error } = await handleError( async () => await controller.checkFavorited(params), ) setLoading(false) if (error) { setError(error) return { data: null, error } } if (data?.favorited !== undefined) { templateSocialStore.setFavorited(templateId, data.favorited) } return { data, error: null } }, [templateId, initialFavoriteCount]) return { loading, error, favorite, unfavorite, checkFavorited, } }