import { useQuery } from '@tanstack/react-query'; import { Activity, Coins, RefreshCw, Rows3, Zap } from 'lucide-react-native'; import { Pressable, Text, View } from 'react-native'; import { LineTrendChart } from '@/src/components/line-trend-chart'; import { ScreenShell } from '@/src/components/screen-shell'; import { StatCard } from '@/src/components/stat-card'; import { getAdminSettings, getDashboardStats, getDashboardTrend } from '@/src/services/admin'; function getDateRange() { const end = new Date(); const start = new Date(); start.setDate(end.getDate() - 6); const toDate = (value: Date) => value.toISOString().slice(0, 10); return { start_date: toDate(start), end_date: toDate(end), }; } export default function DashboardScreen() { const range = getDateRange(); const statsQuery = useQuery({ queryKey: ['dashboard-stats'], queryFn: getDashboardStats, }); const trendQuery = useQuery({ queryKey: ['dashboard-trend', range.start_date, range.end_date], queryFn: () => getDashboardTrend({ ...range, granularity: 'day' }), }); const settingsQuery = useQuery({ queryKey: ['admin-settings'], queryFn: getAdminSettings, }); const stats = statsQuery.data; const errorMessage = statsQuery.error instanceof Error ? statsQuery.error.message : ''; const siteName = settingsQuery.data?.site_name?.trim() || '管理控制台'; const trendPoints = (trendQuery.data?.trend ?? []).map((item) => ({ label: item.date.slice(5), value: item.total_tokens, })); return ( { statsQuery.refetch(); trendQuery.refetch(); }} > } > {trendPoints.length > 1 ? ( `${Math.round(value / 1000)}k`} /> ) : null} {stats ? `今日请求 ${stats.today_requests} · 活跃用户 ${stats.active_users} · 总账号 ${stats.total_accounts}` : errorMessage || '正在等待代理或后台连接。'} ); }