Files
vibe-kanban/frontend/src/components/projects/project-list.tsx

231 lines
7.3 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react';
2025-06-25 09:36:07 +01:00
import { useNavigate } from 'react-router-dom';
import { Button } from '@/components/ui/button';
2025-06-21 22:55:12 +01:00
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
2025-06-25 09:36:07 +01:00
} from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Project } from 'shared/types';
2025-06-25 09:36:07 +01:00
import { ProjectForm } from './project-form';
import { projectsApi } from '@/lib/api';
2025-06-21 22:55:12 +01:00
import {
AlertCircle,
Calendar,
Edit,
2025-06-21 22:55:12 +01:00
ExternalLink,
FolderOpen,
Loader2,
MoreHorizontal,
Plus,
Trash2,
2025-06-25 09:36:07 +01:00
} from 'lucide-react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
2025-06-25 09:36:07 +01:00
} from '@/components/ui/dropdown-menu';
2025-06-14 16:26:48 -04:00
export function ProjectList() {
2025-06-21 22:55:12 +01:00
const navigate = useNavigate();
const [projects, setProjects] = useState<Project[]>([]);
const [loading, setLoading] = useState(false);
const [showForm, setShowForm] = useState(false);
const [editingProject, setEditingProject] = useState<Project | null>(null);
2025-06-25 09:36:07 +01:00
const [error, setError] = useState('');
2025-06-14 16:26:48 -04:00
const fetchProjects = async () => {
2025-06-21 22:55:12 +01:00
setLoading(true);
2025-06-25 09:36:07 +01:00
setError('');
2025-06-14 16:26:48 -04:00
try {
const result = await projectsApi.getAll();
setProjects(result);
2025-06-14 16:26:48 -04:00
} catch (error) {
2025-06-25 09:36:07 +01:00
console.error('Failed to fetch projects:', error);
setError('Failed to fetch projects');
2025-06-14 16:26:48 -04:00
} finally {
2025-06-21 22:55:12 +01:00
setLoading(false);
2025-06-14 16:26:48 -04:00
}
2025-06-21 22:55:12 +01:00
};
2025-06-14 16:26:48 -04:00
const handleDelete = async (id: string, name: string) => {
2025-06-21 22:55:12 +01:00
if (
!confirm(
`Are you sure you want to delete "${name}"? This action cannot be undone.`
)
)
return;
2025-06-14 16:26:48 -04:00
try {
await projectsApi.delete(id);
fetchProjects();
2025-06-14 16:26:48 -04:00
} catch (error) {
2025-06-25 09:36:07 +01:00
console.error('Failed to delete project:', error);
setError('Failed to delete project');
2025-06-14 16:26:48 -04:00
}
2025-06-21 22:55:12 +01:00
};
2025-06-14 16:26:48 -04:00
const handleEdit = (project: Project) => {
2025-06-21 22:55:12 +01:00
setEditingProject(project);
setShowForm(true);
};
2025-06-14 16:26:48 -04:00
const handleOpenInIDE = async (projectId: string) => {
try {
await projectsApi.openEditor(projectId);
} catch (error) {
console.error('Failed to open project in IDE:', error);
setError('Failed to open project in IDE');
}
};
2025-06-14 16:26:48 -04:00
const handleFormSuccess = () => {
2025-06-21 22:55:12 +01:00
setShowForm(false);
setEditingProject(null);
fetchProjects();
};
2025-06-14 16:26:48 -04:00
useEffect(() => {
2025-06-21 22:55:12 +01:00
fetchProjects();
}, []);
2025-06-14 16:26:48 -04:00
return (
2025-06-21 22:55:12 +01:00
<div className="space-y-6 p-8">
2025-06-14 16:26:48 -04:00
<div className="flex justify-between items-center">
<div>
<h1 className="text-3xl font-bold tracking-tight">Projects</h1>
<p className="text-muted-foreground">
Manage your projects and track their progress
</p>
</div>
<Button onClick={() => setShowForm(true)}>
<Plus className="mr-2 h-4 w-4" />
Create Project
</Button>
</div>
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
2025-06-21 22:55:12 +01:00
<AlertDescription>{error}</AlertDescription>
2025-06-14 16:26:48 -04:00
</Alert>
)}
{loading ? (
<div className="flex items-center justify-center py-12">
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Loading projects...
</div>
) : projects.length === 0 ? (
<Card>
<CardContent className="py-12 text-center">
<div className="mx-auto flex h-12 w-12 items-center justify-center rounded-lg bg-muted">
<Plus className="h-6 w-6" />
</div>
<h3 className="mt-4 text-lg font-semibold">No projects yet</h3>
<p className="mt-2 text-sm text-muted-foreground">
Get started by creating your first project.
</p>
2025-06-21 22:55:12 +01:00
<Button className="mt-4" onClick={() => setShowForm(true)}>
2025-06-14 16:26:48 -04:00
<Plus className="mr-2 h-4 w-4" />
Create your first project
</Button>
</CardContent>
</Card>
) : (
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
{projects.map((project) => (
2025-06-21 22:55:12 +01:00
<Card
key={project.id}
className="hover:shadow-md transition-shadow cursor-pointer"
onClick={() => navigate(`/projects/${project.id}/tasks`)}
>
<CardHeader>
2025-06-14 16:26:48 -04:00
<div className="flex items-start justify-between">
2025-06-21 22:55:12 +01:00
<CardTitle className="text-lg">{project.name}</CardTitle>
<div className="flex items-center gap-2">
2025-06-21 22:55:12 +01:00
<Badge variant="secondary">Active</Badge>
<DropdownMenu>
2025-06-21 22:55:12 +01:00
<DropdownMenuTrigger
asChild
onClick={(e) => e.stopPropagation()}
>
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
>
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
2025-06-21 22:55:12 +01:00
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
navigate(`/projects/${project.id}`);
}}
>
<ExternalLink className="mr-2 h-4 w-4" />
View Project
</DropdownMenuItem>
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
handleOpenInIDE(project.id);
}}
>
<FolderOpen className="mr-2 h-4 w-4" />
Open in IDE
</DropdownMenuItem>
2025-06-21 22:55:12 +01:00
<DropdownMenuItem
onClick={(e) => {
e.stopPropagation();
handleEdit(project);
}}
>
<Edit className="mr-2 h-4 w-4" />
Edit
</DropdownMenuItem>
2025-06-21 22:55:12 +01:00
<DropdownMenuItem
onClick={(e) => {
2025-06-21 22:55:12 +01:00
e.stopPropagation();
handleDelete(project.id, project.name);
}}
className="text-destructive"
>
<Trash2 className="mr-2 h-4 w-4" />
Delete
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
2025-06-14 16:26:48 -04:00
</div>
<CardDescription className="flex items-center">
<Calendar className="mr-1 h-3 w-3" />
Created {new Date(project.created_at).toLocaleDateString()}
</CardDescription>
</CardHeader>
</Card>
))}
</div>
)}
<ProjectForm
open={showForm}
onClose={() => {
2025-06-21 22:55:12 +01:00
setShowForm(false);
setEditingProject(null);
2025-06-14 16:26:48 -04:00
}}
onSuccess={handleFormSuccess}
project={editingProject}
/>
</div>
2025-06-21 22:55:12 +01:00
);
2025-06-14 16:26:48 -04:00
}