Files
ok_r/app/projects/page.tsx
2026-01-25 21:55:29 +08:00

130 lines
4.6 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import Link from 'next/link';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { StatusBadge, StatusDot } from '@/components/status-badge';
import { useUser } from '@/components/user-context';
import type { Project, User } from '@/types/database';
import { Plus, Users, Target, ArrowRight } from 'lucide-react';
interface ProjectWithRelations extends Project {
owner: User | null;
pm: User | null;
}
export default function ProjectsPage() {
const { currentUser } = useUser();
const [projects, setProjects] = useState<ProjectWithRelations[]>([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
async function fetchProjects() {
try {
const res = await fetch('/api/projects');
if (res.ok) {
const data = await res.json();
setProjects(data);
}
} catch (error) {
console.error('Failed to fetch projects:', error);
} finally {
setIsLoading(false);
}
}
fetchProjects();
}, []);
const canCreate = currentUser?.role === 'owner' || currentUser?.role === 'pm';
if (isLoading) {
return (
<div className="max-w-7xl mx-auto px-4 py-8">
<div className="animate-pulse space-y-6">
<div className="h-8 bg-slate-200 rounded w-32" />
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{[1, 2, 3].map(i => (
<div key={i} className="h-48 bg-slate-200 rounded-lg" />
))}
</div>
</div>
</div>
);
}
return (
<div className="max-w-7xl mx-auto px-4 py-8">
<div className="flex items-center justify-between mb-8">
<div>
<h1 className="text-2xl font-bold text-slate-800">项目列表</h1>
<p className="text-muted-foreground mt-1">共 {projects.length} 个项目</p>
</div>
{canCreate && (
<Button asChild className="bg-teal-600 hover:bg-teal-700">
<Link href="/projects/new">
<Plus className="h-4 w-4 mr-2" />
新建项目
</Link>
</Button>
)}
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{projects.map(project => (
<Link key={project.id} href={`/projects/${project.id}`}>
<Card className="h-full hover:shadow-md transition-shadow cursor-pointer">
<CardHeader className="pb-2">
<div className="flex items-start justify-between">
<CardTitle className="text-lg">{project.name}</CardTitle>
<StatusBadge status={project.status} size="sm" />
</div>
{project.project_type && (
<span className="text-xs text-muted-foreground bg-slate-100 px-2 py-0.5 rounded">
{project.project_type}
</span>
)}
</CardHeader>
<CardContent className="space-y-4">
{project.description && (
<p className="text-sm text-muted-foreground line-clamp-2">
{project.description}
</p>
)}
<div className="flex items-center gap-4 text-sm">
<div className="flex items-center gap-1.5 text-muted-foreground">
<Users className="h-4 w-4" />
<span>{project.pm?.name || '未指定PM'}</span>
</div>
</div>
<div className="flex items-center justify-between pt-2 border-t">
<span className="text-xs text-muted-foreground">
负责人: {project.owner?.name || '未指定'}
</span>
<ArrowRight className="h-4 w-4 text-muted-foreground" />
</div>
</CardContent>
</Card>
</Link>
))}
{projects.length === 0 && (
<div className="col-span-full text-center py-12">
<Target className="h-12 w-12 text-slate-300 mx-auto mb-4" />
<p className="text-muted-foreground">暂无项目</p>
{canCreate && (
<Button asChild className="mt-4" variant="outline">
<Link href="/projects/new">
<Plus className="h-4 w-4 mr-2" />
创建第一个项目
</Link>
</Button>
)}
</div>
)}
</div>
</div>
);
}