|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { useRouter } from 'next/navigation'; |
| 5 | +import { useProjectStore } from '@/lib/stores/project-store'; |
| 6 | +import { ProjectCard } from '@/components/dashboard/project-card'; |
| 7 | +import { CreateProjectDialog } from '@/components/dashboard/create-project-dialog'; |
| 8 | +import { RenameProjectDialog } from '@/components/dashboard/rename-project-dialog'; |
| 9 | +import { Button } from '@/components/ui/button'; |
| 10 | +import { Plus, Film } from 'lucide-react'; |
| 11 | + |
| 12 | +export default function DashboardPage() { |
| 13 | + const router = useRouter(); |
| 14 | + const { projects, loadProjects, createProject, updateProject, deleteProject, setActiveProject } = |
| 15 | + useProjectStore(); |
| 16 | + const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); |
| 17 | + const [renameProjectId, setRenameProjectId] = useState<string | null>(null); |
| 18 | + const [isLoading, setIsLoading] = useState(true); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + const loadData = async () => { |
| 22 | + try { |
| 23 | + await loadProjects(); |
| 24 | + } finally { |
| 25 | + setIsLoading(false); |
| 26 | + } |
| 27 | + }; |
| 28 | + loadData(); |
| 29 | + }, [loadProjects]); |
| 30 | + |
| 31 | + const handleCreateProject = async (name: string) => { |
| 32 | + const project = await createProject(name); |
| 33 | + setActiveProject(project.id); |
| 34 | + router.push(`/editor/${project.id}`); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleDeleteProject = async (id: string) => { |
| 38 | + if (confirm('Are you sure you want to delete this project? This cannot be undone.')) { |
| 39 | + await deleteProject(id); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const handleRenameProject = async (name: string) => { |
| 44 | + if (renameProjectId) { |
| 45 | + await updateProject(renameProjectId, { name }); |
| 46 | + setRenameProjectId(null); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + const renameProject = projects.find((p) => p.id === renameProjectId); |
| 51 | + |
| 52 | + if (isLoading) { |
| 53 | + return ( |
| 54 | + <div className="min-h-screen flex items-center justify-center"> |
| 55 | + <div className="text-center"> |
| 56 | + <Film className="w-16 h-16 mx-auto mb-4 animate-pulse" /> |
| 57 | + <p className="text-lg text-muted-foreground">Loading projects...</p> |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="min-h-screen bg-background"> |
| 65 | + <div className="container mx-auto px-4 py-8"> |
| 66 | + <div className="flex items-center justify-between mb-8"> |
| 67 | + <div> |
| 68 | + <h1 className="text-4xl font-bold mb-2">My Projects</h1> |
| 69 | + <p className="text-muted-foreground"> |
| 70 | + Create and manage your video projects |
| 71 | + </p> |
| 72 | + </div> |
| 73 | + <Button size="lg" onClick={() => setIsCreateDialogOpen(true)}> |
| 74 | + <Plus className="w-5 h-5 mr-2" /> |
| 75 | + New Project |
| 76 | + </Button> |
| 77 | + </div> |
| 78 | + |
| 79 | + {projects.length === 0 ? ( |
| 80 | + <div className="text-center py-16"> |
| 81 | + <Film className="w-24 h-24 mx-auto mb-6 text-muted-foreground" /> |
| 82 | + <h2 className="text-2xl font-semibold mb-2">No projects yet</h2> |
| 83 | + <p className="text-muted-foreground mb-6"> |
| 84 | + Create your first video project to get started |
| 85 | + </p> |
| 86 | + <Button size="lg" onClick={() => setIsCreateDialogOpen(true)}> |
| 87 | + <Plus className="w-5 h-5 mr-2" /> |
| 88 | + Create Your First Project |
| 89 | + </Button> |
| 90 | + </div> |
| 91 | + ) : ( |
| 92 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6"> |
| 93 | + {projects.map((project) => ( |
| 94 | + <ProjectCard |
| 95 | + key={project.id} |
| 96 | + project={project} |
| 97 | + onDelete={handleDeleteProject} |
| 98 | + onRename={(id) => setRenameProjectId(id)} |
| 99 | + /> |
| 100 | + ))} |
| 101 | + </div> |
| 102 | + )} |
| 103 | + </div> |
| 104 | + |
| 105 | + <CreateProjectDialog |
| 106 | + open={isCreateDialogOpen} |
| 107 | + onOpenChange={setIsCreateDialogOpen} |
| 108 | + onSubmit={handleCreateProject} |
| 109 | + /> |
| 110 | + |
| 111 | + {renameProject && ( |
| 112 | + <RenameProjectDialog |
| 113 | + open={!!renameProjectId} |
| 114 | + currentName={renameProject.name} |
| 115 | + onOpenChange={(open) => !open && setRenameProjectId(null)} |
| 116 | + onSubmit={handleRenameProject} |
| 117 | + /> |
| 118 | + )} |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments