CodeReview/src/pages/AdminDashboard.tsx

387 lines
15 KiB
TypeScript
Raw Normal View History

import { useState, useEffect } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import {
Users,
Settings,
Shield,
Activity,
AlertTriangle,
Search,
Edit,
Trash2,
UserPlus,
Database,
Server,
BarChart3
} from "lucide-react";
import { api } from "@/shared/config/database";
import type { Profile } from "@/shared/types";
import { toast } from "sonner";
export default function AdminDashboard() {
const user = null as any;
const [profiles, setProfiles] = useState<Profile[]>([]);
const [loading, setLoading] = useState(true);
const [searchTerm, setSearchTerm] = useState("");
const [editingUser, setEditingUser] = useState<Profile | null>(null);
const [showEditDialog, setShowEditDialog] = useState(false);
useEffect(() => {
loadProfiles();
}, []);
const loadProfiles = async () => {
try {
setLoading(true);
const data = await api.getAllProfiles();
setProfiles(data);
} catch (error) {
console.error('Failed to load profiles:', error);
toast.error("加载用户数据失败");
} finally {
setLoading(false);
}
};
const handleUpdateUserRole = async (userId: string, newRole: 'admin' | 'member') => {
try {
await api.updateProfile(userId, { role: newRole });
toast.success("用户角色更新成功");
loadProfiles();
} catch (error) {
console.error('Failed to update user role:', error);
toast.error("更新用户角色失败");
}
};
const formatDate = (dateString: string) => {
return new Date(dateString).toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit'
});
};
const filteredProfiles = profiles.filter(profile =>
profile.full_name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
profile.phone?.toLowerCase().includes(searchTerm.toLowerCase()) ||
profile.email?.toLowerCase().includes(searchTerm.toLowerCase())
);
// 检查当前用户是否为管理员
const isAdmin = true;
if (!isAdmin) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="text-center">
<Shield className="w-16 h-16 text-red-500 mx-auto mb-4" />
<h2 className="text-2xl font-bold text-gray-900 mb-2">访</h2>
<p className="text-gray-600 mb-4">访</p>
</div>
</div>
);
}
if (loading) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="animate-spin rounded-full h-32 w-32 border-b-2 border-primary"></div>
</div>
);
}
return (
<div className="space-y-6">
{/* 页面标题 */}
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold text-gray-900"></h1>
<p className="text-gray-600 mt-2">
</p>
</div>
<Badge variant="outline" className="bg-red-50 text-red-700">
<Shield className="w-4 h-4 mr-2" />
</Badge>
</div>
{/* 统计卡片 */}
<div className="grid grid-cols-1 md:grid-cols-4 gap-6">
<Card>
<CardContent className="p-6">
<div className="flex items-center">
<Users className="h-8 w-8 text-primary" />
<div className="ml-4">
<p className="text-sm font-medium text-muted-foreground"></p>
<p className="text-2xl font-bold">{profiles.length}</p>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-6">
<div className="flex items-center">
<Shield className="h-8 w-8 text-green-600" />
<div className="ml-4">
<p className="text-sm font-medium text-muted-foreground"></p>
<p className="text-2xl font-bold">
{profiles.filter(p => p.role === 'admin').length}
</p>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-6">
<div className="flex items-center">
<Activity className="h-8 w-8 text-orange-600" />
<div className="ml-4">
<p className="text-sm font-medium text-muted-foreground"></p>
<p className="text-2xl font-bold">
{profiles.filter(p => p.role === 'member').length}
</p>
</div>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-6">
<div className="flex items-center">
<Server className="h-8 w-8 text-purple-600" />
<div className="ml-4">
<p className="text-sm font-medium text-muted-foreground"></p>
<p className="text-2xl font-bold text-green-600"></p>
</div>
</div>
</CardContent>
</Card>
</div>
{/* 主要内容 */}
<Tabs defaultValue="users" className="w-full">
<TabsList className="grid w-full grid-cols-4">
<TabsTrigger value="users"></TabsTrigger>
<TabsTrigger value="system"></TabsTrigger>
<TabsTrigger value="settings"></TabsTrigger>
<TabsTrigger value="logs"></TabsTrigger>
</TabsList>
<TabsContent value="users" className="space-y-6">
{/* 搜索和操作 */}
<Card>
<CardContent className="p-4">
<div className="flex items-center justify-between">
<div className="flex-1 relative mr-4">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-4 h-4" />
<Input
placeholder="搜索用户姓名、手机号或邮箱..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10"
/>
</div>
<Button>
<UserPlus className="w-4 h-4 mr-2" />
</Button>
</div>
</CardContent>
</Card>
{/* 用户列表 */}
<Card>
<CardHeader>
<CardTitle> ({filteredProfiles.length})</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-4">
{filteredProfiles.map((profile) => (
<div key={profile.id} className="flex items-center justify-between p-4 border rounded-lg">
<div className="flex items-center space-x-4">
<div className="w-10 h-10 bg-gradient-to-r from-primary to-accent rounded-full flex items-center justify-center text-white font-medium">
{profile.full_name?.charAt(0) || profile.phone?.charAt(0) || 'U'}
</div>
<div>
<h4 className="font-medium">
{profile.full_name || '未设置姓名'}
</h4>
<div className="flex items-center space-x-4 text-sm text-muted-foreground">
{profile.phone && (
<span>📱 {profile.phone}</span>
)}
{profile.email && (
<span>📧 {profile.email}</span>
)}
</div>
<p className="text-xs text-muted-foreground">
{formatDate(profile.created_at)}
</p>
</div>
</div>
<div className="flex items-center space-x-3">
<Badge variant={profile.role === 'admin' ? 'default' : 'secondary'}>
{profile.role === 'admin' ? '管理员' : '普通用户'}
</Badge>
{profile.id !== user?.id && (
<Select
value={profile.role}
onValueChange={(value: 'admin' | 'member') =>
handleUpdateUserRole(profile.id, value)
}
>
<SelectTrigger className="w-32">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="member"></SelectItem>
<SelectItem value="admin"></SelectItem>
</SelectContent>
</Select>
)}
<Button variant="outline" size="sm">
<Edit className="w-4 h-4" />
</Button>
</div>
</div>
))}
</div>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="system" className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
{/* 系统性能 */}
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<BarChart3 className="w-5 h-5 mr-2" />
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-3">
<div className="flex items-center justify-between">
<span className="text-sm font-medium">CPU 使</span>
<span className="text-sm text-muted-foreground">15%</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-2">
<div className="bg-primary h-2 rounded-full" style={{ width: '15%' }}></div>
</div>
<div className="flex items-center justify-between">
<span className="text-sm font-medium">使</span>
<span className="text-sm text-muted-foreground">32%</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-2">
<div className="bg-green-600 h-2 rounded-full" style={{ width: '32%' }}></div>
</div>
<div className="flex items-center justify-between">
<span className="text-sm font-medium">使</span>
<span className="text-sm text-muted-foreground">58%</span>
</div>
<div className="w-full bg-gray-200 rounded-full h-2">
<div className="bg-orange-600 h-2 rounded-full" style={{ width: '58%' }}></div>
</div>
</div>
</CardContent>
</Card>
{/* 数据库状态 */}
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<Database className="w-5 h-5 mr-2" />
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="flex items-center justify-between">
<span className="text-sm font-medium"></span>
<Badge className="bg-green-100 text-green-800"></Badge>
</div>
<div className="flex items-center justify-between">
<span className="text-sm font-medium"></span>
<span className="text-sm text-muted-foreground">12</span>
</div>
<div className="flex items-center justify-between">
<span className="text-sm font-medium"></span>
<span className="text-sm text-muted-foreground">45ms</span>
</div>
<div className="flex items-center justify-between">
<span className="text-sm font-medium">使</span>
<span className="text-sm text-muted-foreground">2.3GB</span>
</div>
</CardContent>
</Card>
</div>
{/* 系统日志 */}
<Card>
<CardHeader>
<CardTitle></CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
<div className="flex items-center space-x-3 p-3 bg-green-50 rounded-lg">
<div className="w-2 h-2 bg-green-500 rounded-full"></div>
<div className="flex-1">
<p className="text-sm font-medium"></p>
<p className="text-xs text-muted-foreground">2024-01-15 09:00:00</p>
</div>
</div>
<div className="flex items-center space-x-3 p-3 bg-red-50 rounded-lg">
<div className="w-2 h-2 bg-primary rounded-full"></div>
<div className="flex-1">
<p className="text-sm font-medium"></p>
<p className="text-xs text-muted-foreground">2024-01-15 08:45:00</p>
</div>
</div>
<div className="flex items-center space-x-3 p-3 bg-yellow-50 rounded-lg">
<div className="w-2 h-2 bg-yellow-500 rounded-full"></div>
<div className="flex-1">
<p className="text-sm font-medium"></p>
<p className="text-xs text-muted-foreground">2024-01-15 08:30:00</p>
</div>
</div>
</div>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="settings" className="space-y-6">
<div className="text-center py-12">
<Settings className="w-16 h-16 text-muted-foreground mx-auto mb-4" />
<h3 className="text-lg font-medium text-muted-foreground mb-2"></h3>
<p className="text-sm text-muted-foreground"></p>
</div>
</TabsContent>
<TabsContent value="logs" className="space-y-6">
<div className="text-center py-12">
<Activity className="w-16 h-16 text-muted-foreground mx-auto mb-4" />
<h3 className="text-lg font-medium text-muted-foreground mb-2"></h3>
<p className="text-sm text-muted-foreground"></p>
</div>
</TabsContent>
</Tabs>
</div>
);
}