/** * 插件统计组件 * 显示点赞、点踩、评分和下载量 */ import { useState, useEffect } from 'react' import { ThumbsUp, ThumbsDown, Star, Download } from 'lucide-react' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Textarea } from '@/components/ui/textarea' import { useToast } from '@/hooks/use-toast' import { getPluginStats, likePlugin, dislikePlugin, ratePlugin, type PluginStatsData, } from '@/lib/plugin-stats' interface PluginStatsProps { pluginId: string compact?: boolean // 紧凑模式(只显示数字) } export function PluginStats({ pluginId, compact = false }: PluginStatsProps) { const [stats, setStats] = useState(null) const [loading, setLoading] = useState(true) const [userRating, setUserRating] = useState(0) const [userComment, setUserComment] = useState('') const [isRatingDialogOpen, setIsRatingDialogOpen] = useState(false) const { toast } = useToast() // 加载统计数据 const loadStats = async () => { setLoading(true) const data = await getPluginStats(pluginId) if (data) { setStats(data) } setLoading(false) } useEffect(() => { loadStats() // eslint-disable-next-line react-hooks/exhaustive-deps }, [pluginId]) // 处理点赞 const handleLike = async () => { const result = await likePlugin(pluginId) if (result.success) { toast({ title: '已点赞', description: '感谢你的支持!' }) loadStats() // 重新加载统计数据 } else { toast({ title: '点赞失败', description: result.error || '未知错误', variant: 'destructive', }) } } // 处理点踩 const handleDislike = async () => { const result = await dislikePlugin(pluginId) if (result.success) { toast({ title: '已反馈', description: '感谢你的反馈!' }) loadStats() } else { toast({ title: '操作失败', description: result.error || '未知错误', variant: 'destructive', }) } } // 提交评分 const handleSubmitRating = async () => { if (userRating === 0) { toast({ title: '请选择评分', description: '至少选择 1 颗星', variant: 'destructive', }) return } const result = await ratePlugin(pluginId, userRating, userComment || undefined) if (result.success) { toast({ title: '评分成功', description: '感谢你的评价!' }) setIsRatingDialogOpen(false) setUserRating(0) setUserComment('') loadStats() } else { toast({ title: '评分失败', description: result.error || '未知错误', variant: 'destructive', }) } } if (loading) { return (
-
-
) } if (!stats) { return null } // 紧凑模式 if (compact) { return (
{stats.downloads.toLocaleString()}
{stats.rating.toFixed(1)}
{stats.likes}
) } // 完整模式 return (
{/* 统计数字 */}
{stats.downloads.toLocaleString()} 下载量
{stats.rating.toFixed(1)} {stats.rating_count} 条评价
{stats.likes} 点赞
{stats.dislikes} 点踩
{/* 操作按钮 */}
为插件评分 分享你的使用体验,帮助其他用户
{/* 星级评分 */}
{[1, 2, 3, 4, 5].map((star) => ( ))}
{userRating === 0 && '点击星星进行评分'} {userRating === 1 && '很差'} {userRating === 2 && '一般'} {userRating === 3 && '还行'} {userRating === 4 && '不错'} {userRating === 5 && '非常好'}
{/* 评论 */}