|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useState, useEffect } from "react" |
| 4 | +import { AppStatisticsChart } from "@/components/app-statistics-chart" |
| 5 | +import { AppStatistics } from "@/lib/statistics-types" |
| 6 | + |
| 7 | +export default function HomeStatistics() { |
| 8 | + const [statistics, setStatistics] = useState<AppStatistics | null>(null) |
| 9 | + const [loading, setLoading] = useState(true) |
| 10 | + const [error, setError] = useState<string | null>(null) |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + fetchStatistics() |
| 14 | + }, []) |
| 15 | + |
| 16 | + const fetchStatistics = async () => { |
| 17 | + try { |
| 18 | + setLoading(true) |
| 19 | + setError(null) |
| 20 | + |
| 21 | + // Fetch from internal API (no authentication required) |
| 22 | + const response = await fetch('/api/internal/statistics') |
| 23 | + |
| 24 | + if (!response.ok) { |
| 25 | + throw new Error(`API request failed: ${response.status} ${response.statusText}`) |
| 26 | + } |
| 27 | + |
| 28 | + const result = await response.json() |
| 29 | + |
| 30 | + if (result.code !== 0) { |
| 31 | + throw new Error(`API error: ${result.msg}`) |
| 32 | + } |
| 33 | + |
| 34 | + setStatistics(result.data) |
| 35 | + |
| 36 | + } catch (err) { |
| 37 | + console.error('Error fetching statistics:', err); |
| 38 | + setError(err instanceof Error ? err.message : "获取统计数据失败") |
| 39 | + } finally { |
| 40 | + setLoading(false) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (loading) { |
| 45 | + return ( |
| 46 | + <section className="py-16"> |
| 47 | + <div className="container mx-auto"> |
| 48 | + <div className="flex items-center justify-center min-h-[200px]"> |
| 49 | + <div className="text-center"> |
| 50 | + <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div> |
| 51 | + <p className="text-muted-foreground">加载统计数据中...</p> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </section> |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + if (error) { |
| 60 | + return ( |
| 61 | + <section className="py-16"> |
| 62 | + <div className="container mx-auto"> |
| 63 | + <div className="flex items-center justify-center min-h-[200px]"> |
| 64 | + <div className="text-center"> |
| 65 | + <div className="text-destructive text-4xl mb-4">⚠️</div> |
| 66 | + <p className="text-destructive mb-4">{error}</p> |
| 67 | + <button |
| 68 | + onClick={fetchStatistics} |
| 69 | + className="px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90" |
| 70 | + > |
| 71 | + 重试 |
| 72 | + </button> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </section> |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + if (!statistics) { |
| 81 | + return null |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <section className="py-16"> |
| 86 | + <div className="container mx-auto space-y-8"> |
| 87 | + <div className="text-center space-y-4"> |
| 88 | + <h2 className="text-3xl font-bold">应用统计</h2> |
| 89 | + <p className="text-muted-foreground max-w-2xl mx-auto"> |
| 90 | + 查看应用的下载和启动统计数据,了解用户使用趋势 |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + |
| 94 | + {/* Summary Cards */} |
| 95 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> |
| 96 | + <div className="bg-card rounded-lg p-6 border"> |
| 97 | + <div className="text-sm font-medium text-muted-foreground mb-2">昨日下载</div> |
| 98 | + <div className="text-2xl font-bold">{statistics.yesterdayDownloadCount.toLocaleString()}</div> |
| 99 | + </div> |
| 100 | + <div className="bg-card rounded-lg p-6 border"> |
| 101 | + <div className="text-sm font-medium text-muted-foreground mb-2">总下载量</div> |
| 102 | + <div className="text-2xl font-bold">{statistics.totalDownloadCount.toLocaleString()}</div> |
| 103 | + {statistics.githubDownloads && ( |
| 104 | + <div className="text-xs text-muted-foreground mt-1"> |
| 105 | + 包含 GitHub: {statistics.githubDownloads.toLocaleString()} |
| 106 | + </div> |
| 107 | + )} |
| 108 | + </div> |
| 109 | + <div className="bg-card rounded-lg p-6 border"> |
| 110 | + <div className="text-sm font-medium text-muted-foreground mb-2">昨日启动</div> |
| 111 | + <div className="text-2xl font-bold">{statistics.yesterdayAppStartCount.toLocaleString()}</div> |
| 112 | + </div> |
| 113 | + <div className="bg-card rounded-lg p-6 border"> |
| 114 | + <div className="text-sm font-medium text-muted-foreground mb-2">总启动量</div> |
| 115 | + <div className="text-2xl font-bold">{statistics.totalAppStartCount.toLocaleString()}</div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + |
| 119 | + {/* GitHub Stats */} |
| 120 | + {statistics.githubStars !== undefined && ( |
| 121 | + <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> |
| 122 | + <div className="bg-card rounded-lg p-6 border"> |
| 123 | + <div className="text-sm font-medium text-muted-foreground mb-2">GitHub Stars</div> |
| 124 | + <div className="text-2xl font-bold">{statistics.githubStars.toLocaleString()}</div> |
| 125 | + </div> |
| 126 | + <div className="bg-card rounded-lg p-6 border"> |
| 127 | + <div className="text-sm font-medium text-muted-foreground mb-2">GitHub Forks</div> |
| 128 | + <div className="text-2xl font-bold">{statistics.githubForks?.toLocaleString() || 0}</div> |
| 129 | + </div> |
| 130 | + <div className="bg-card rounded-lg p-6 border"> |
| 131 | + <div className="text-sm font-medium text-muted-foreground mb-2">GitHub 下载</div> |
| 132 | + <div className="text-2xl font-bold">{statistics.githubDownloads?.toLocaleString() || 0}</div> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + )} |
| 136 | + |
| 137 | + {/* Chart */} |
| 138 | + <AppStatisticsChart |
| 139 | + downloadData={statistics.downloadCount7Day} |
| 140 | + startData={statistics.appStartCount7Day} |
| 141 | + /> |
| 142 | + </div> |
| 143 | + </section> |
| 144 | + ) |
| 145 | +} |
0 commit comments