feat: Implement Dashboard page displaying pool status, statistics, and recent batch runs.
This commit is contained in:
@@ -1,16 +1,62 @@
|
|||||||
import { Clock, CheckCircle, XCircle } from 'lucide-react'
|
import { useState, useEffect, useCallback } from 'react'
|
||||||
|
import { Clock, CheckCircle, RefreshCw, AlertCircle, XCircle } from 'lucide-react'
|
||||||
import { Link } from 'react-router-dom'
|
import { Link } from 'react-router-dom'
|
||||||
import type { AddRecord } from '../../types'
|
|
||||||
import { formatRelativeTime } from '../../utils/format'
|
|
||||||
import { Card, CardHeader, CardTitle, Button } from '../common'
|
import { Card, CardHeader, CardTitle, Button } from '../common'
|
||||||
|
|
||||||
interface RecentRecordsProps {
|
interface BatchRun {
|
||||||
records: AddRecord[]
|
id: number
|
||||||
loading?: boolean
|
started_at: string
|
||||||
|
finished_at: string
|
||||||
|
total_owners: number
|
||||||
|
total_registered: number
|
||||||
|
total_added_to_s2a: number
|
||||||
|
success_rate: number
|
||||||
|
duration_seconds: number
|
||||||
|
status: string
|
||||||
|
errors: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RecentRecords({ records, loading = false }: RecentRecordsProps) {
|
// 格式化相对时间
|
||||||
const recentRecords = records.slice(0, 5)
|
function formatRelativeTime(dateStr: string): string {
|
||||||
|
if (!dateStr) return '-'
|
||||||
|
const date = new Date(dateStr)
|
||||||
|
const now = new Date()
|
||||||
|
const diffMs = now.getTime() - date.getTime()
|
||||||
|
const diffMins = Math.floor(diffMs / 60000)
|
||||||
|
const diffHours = Math.floor(diffMins / 60)
|
||||||
|
const diffDays = Math.floor(diffHours / 24)
|
||||||
|
|
||||||
|
if (diffMins < 1) return '刚刚'
|
||||||
|
if (diffMins < 60) return `${diffMins}分钟前`
|
||||||
|
if (diffHours < 24) return `${diffHours}小时前`
|
||||||
|
if (diffDays < 7) return `${diffDays}天前`
|
||||||
|
return date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' })
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function RecentRecords() {
|
||||||
|
const [runs, setRuns] = useState<BatchRun[]>([])
|
||||||
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
|
const fetchRuns = useCallback(async () => {
|
||||||
|
setLoading(true)
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/batch/runs')
|
||||||
|
if (res.ok) {
|
||||||
|
const data = await res.json()
|
||||||
|
if (data.code === 0) {
|
||||||
|
// 只取最近5条
|
||||||
|
setRuns((data.data || []).slice(0, 5))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('获取批次记录失败:', e)
|
||||||
|
}
|
||||||
|
setLoading(false)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
fetchRuns()
|
||||||
|
}, [fetchRuns])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card hoverable>
|
<Card hoverable>
|
||||||
@@ -24,12 +70,12 @@ export default function RecentRecords({ records, loading = false }: RecentRecord
|
|||||||
</CardHeader>
|
</CardHeader>
|
||||||
|
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3 p-4">
|
||||||
{[1, 2, 3].map((i) => (
|
{[1, 2, 3].map((i) => (
|
||||||
<div key={i} className="h-16 bg-slate-100 dark:bg-slate-700 rounded-lg animate-pulse" />
|
<div key={i} className="h-16 bg-slate-100 dark:bg-slate-700 rounded-lg animate-pulse" />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
) : recentRecords.length === 0 ? (
|
) : runs.length === 0 ? (
|
||||||
<div className="text-center py-8">
|
<div className="text-center py-8">
|
||||||
<Clock className="h-12 w-12 mx-auto text-slate-300 dark:text-slate-600" />
|
<Clock className="h-12 w-12 mx-auto text-slate-300 dark:text-slate-600" />
|
||||||
<p className="mt-2 text-sm text-slate-500 dark:text-slate-400">暂无加号记录</p>
|
<p className="mt-2 text-sm text-slate-500 dark:text-slate-400">暂无加号记录</p>
|
||||||
@@ -38,40 +84,54 @@ export default function RecentRecords({ records, loading = false }: RecentRecord
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="space-y-3">
|
<div className="space-y-3 p-4">
|
||||||
{recentRecords.map((record) => (
|
{runs.map((run) => (
|
||||||
<div
|
<div
|
||||||
key={record.id}
|
key={run.id}
|
||||||
className="flex items-center justify-between p-3 bg-slate-50 dark:bg-slate-700/50 rounded-lg transition-colors duration-200 hover:bg-slate-100 dark:hover:bg-slate-700"
|
className="flex items-center justify-between p-3 bg-slate-50 dark:bg-slate-700/50 rounded-lg transition-colors duration-200 hover:bg-slate-100 dark:hover:bg-slate-700"
|
||||||
>
|
>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<div
|
<div
|
||||||
className={`p-2 rounded-lg ${
|
className={`p-2 rounded-lg ${run.status === 'completed'
|
||||||
record.failed === 0
|
? run.success_rate >= 80
|
||||||
? 'bg-green-100 dark:bg-green-900/30'
|
? 'bg-green-100 dark:bg-green-900/30'
|
||||||
: 'bg-yellow-100 dark:bg-yellow-900/30'
|
: 'bg-yellow-100 dark:bg-yellow-900/30'
|
||||||
|
: run.status === 'running'
|
||||||
|
? 'bg-blue-100 dark:bg-blue-900/30'
|
||||||
|
: 'bg-red-100 dark:bg-red-900/30'
|
||||||
}`}
|
}`}
|
||||||
>
|
>
|
||||||
{record.failed === 0 ? (
|
{run.status === 'completed' ? (
|
||||||
|
run.success_rate >= 80 ? (
|
||||||
<CheckCircle className="h-4 w-4 text-green-600 dark:text-green-400" />
|
<CheckCircle className="h-4 w-4 text-green-600 dark:text-green-400" />
|
||||||
) : (
|
) : (
|
||||||
<XCircle className="h-4 w-4 text-yellow-600 dark:text-yellow-400" />
|
<XCircle className="h-4 w-4 text-yellow-600 dark:text-yellow-400" />
|
||||||
|
)
|
||||||
|
) : run.status === 'running' ? (
|
||||||
|
<RefreshCw className="h-4 w-4 text-blue-600 dark:text-blue-400 animate-spin" />
|
||||||
|
) : (
|
||||||
|
<AlertCircle className="h-4 w-4 text-red-600 dark:text-red-400" />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p className="text-sm font-medium text-slate-900 dark:text-slate-100">
|
<p className="text-sm font-medium text-slate-900 dark:text-slate-100">
|
||||||
{record.source === 'manual' ? '手动上传' : '自动补号'}
|
批次 #{run.id}
|
||||||
|
<span className="ml-2 text-xs text-slate-500">
|
||||||
|
{run.total_owners} 个母号
|
||||||
|
</span>
|
||||||
</p>
|
</p>
|
||||||
<p className="text-xs text-slate-500 dark:text-slate-400">
|
<p className="text-xs text-slate-500 dark:text-slate-400">
|
||||||
{formatRelativeTime(record.timestamp)}
|
{formatRelativeTime(run.started_at)}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="text-right">
|
<div className="text-right">
|
||||||
<p className="text-sm font-medium text-slate-900 dark:text-slate-100">
|
<p className="text-sm font-medium text-green-600 dark:text-green-400">
|
||||||
+{record.success}
|
+{run.total_added_to_s2a}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-slate-500">
|
||||||
|
{run.status === 'running' ? '运行中' : `${run.success_rate.toFixed(0)}%`}
|
||||||
</p>
|
</p>
|
||||||
{record.failed > 0 && <p className="text-xs text-red-500">失败 {record.failed}</p>}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ import { Upload, RefreshCw, Settings, Trash2 } from 'lucide-react'
|
|||||||
import { PoolStatus, RecentRecords } from '../components/dashboard'
|
import { PoolStatus, RecentRecords } from '../components/dashboard'
|
||||||
import { Card, CardHeader, CardTitle, CardContent, Button } from '../components/common'
|
import { Card, CardHeader, CardTitle, CardContent, Button } from '../components/common'
|
||||||
import { useS2AApi } from '../hooks/useS2AApi'
|
import { useS2AApi } from '../hooks/useS2AApi'
|
||||||
import { useRecords } from '../hooks/useRecords'
|
|
||||||
import { useConfig } from '../hooks/useConfig'
|
import { useConfig } from '../hooks/useConfig'
|
||||||
import type { DashboardStats } from '../types'
|
import type { DashboardStats } from '../types'
|
||||||
import { formatNumber, formatCurrency } from '../utils/format'
|
import { formatNumber, formatCurrency } from '../utils/format'
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const { getDashboardStats, loading, error, isConnected } = useS2AApi()
|
const { getDashboardStats, loading, error, isConnected } = useS2AApi()
|
||||||
const { records } = useRecords()
|
|
||||||
const { config } = useConfig()
|
const { config } = useConfig()
|
||||||
const [stats, setStats] = useState<DashboardStats | null>(null)
|
const [stats, setStats] = useState<DashboardStats | null>(null)
|
||||||
const [refreshing, setRefreshing] = useState(false)
|
const [refreshing, setRefreshing] = useState(false)
|
||||||
@@ -202,7 +200,7 @@ export default function Dashboard() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Recent Records */}
|
{/* Recent Records */}
|
||||||
<RecentRecords records={records} loading={loading} />
|
<RecentRecords />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user