diff --git a/src/components/DeleteProjectDialog.tsx b/src/components/DeleteProjectDialog.tsx new file mode 100644 index 00000000..c8f7c64c --- /dev/null +++ b/src/components/DeleteProjectDialog.tsx @@ -0,0 +1,62 @@ +import { + AlertDialog, + AlertDialogAction, + AlertDialogCancel, + AlertDialogContent, + AlertDialogDescription, + AlertDialogFooter, + AlertDialogHeader, + AlertDialogTitle, + AlertDialogTrigger, +} from '@/components/ui/alert-dialog'; +import { Loader2 } from 'lucide-react'; +import type { ReactNode } from 'react'; + +interface DeleteProjectDialogProps { + projectName: string; + onDelete: () => void | Promise; + isDeleting: boolean; + open?: boolean; + onOpenChange?: (open: boolean) => void; + trigger?: ReactNode; +} + +export function DeleteProjectDialog({ + projectName, + onDelete, + isDeleting, + open, + onOpenChange, + trigger, +}: DeleteProjectDialogProps) { + return ( + + {trigger && {trigger}} + + + Delete Project + + Are you sure you want to delete "{projectName}"? This action cannot be undone and will permanently delete all project data including files. + + + + Cancel + + {isDeleting ? ( + <> + + Deleting... + + ) : ( + 'Delete Project' + )} + + + + + ); +} diff --git a/src/components/ProjectDetailsDialog.tsx b/src/components/ProjectDetailsDialog.tsx index 0eccac7e..46482350 100644 --- a/src/components/ProjectDetailsDialog.tsx +++ b/src/components/ProjectDetailsDialog.tsx @@ -23,20 +23,28 @@ import { DollarSign, ExternalLink, FileCode, + Download, + Trash2, } from 'lucide-react'; import { Skeleton } from './ui/skeleton'; +import { Separator } from './ui/separator'; import { DotAI } from '@/lib/DotAI'; import { LabelSelector } from '@/components/labels/LabelSelector'; +import { DeleteProjectDialog } from '@/components/DeleteProjectDialog'; import { cn } from '@/lib/utils'; +import JSZip from 'jszip'; interface ProjectDetailsDialogProps { project: Project; open: boolean; onOpenChange: (open: boolean) => void; + onProjectDeleted?: () => void; } -export function ProjectDetailsDialog({ project, open, onOpenChange }: ProjectDetailsDialogProps) { +export function ProjectDetailsDialog({ project, open, onOpenChange, onProjectDeleted }: ProjectDetailsDialogProps) { const [isRenaming, setIsRenaming] = useState(false); + const [isExporting, setIsExporting] = useState(false); + const [isDeleting, setIsDeleting] = useState(false); const [newProjectName, setNewProjectName] = useState(project.id); const [totalCost, setTotalCost] = useState(null); const [isLoading, setIsLoading] = useState(false); @@ -129,6 +137,105 @@ export function ProjectDetailsDialog({ project, open, onOpenChange }: ProjectDet } }; + const handleDeleteProject = async () => { + setIsDeleting(true); + try { + await projectsManager.deleteProject(project.id); + + // Invalidate the projects query to update the sidebar + await queryClient.invalidateQueries({ queryKey: ['projects'] }); + + toast({ + title: "Project deleted", + description: `"${project.name}" has been permanently deleted.`, + }); + + // Close the dialog first + onOpenChange(false); + + // Navigate back to home and notify parent + navigate('/'); + if (onProjectDeleted) { + onProjectDeleted(); + } + } catch (error) { + toast({ + title: "Failed to delete project", + description: error instanceof Error ? error.message : "An unexpected error occurred", + variant: "destructive", + }); + } finally { + setIsDeleting(false); + } + }; + + const handleExportProject = async () => { + setIsExporting(true); + try { + const zip = new JSZip(); + const projectPath = `${projectsPath}/${project.id}`; + + // Recursive function to add files and directories to zip from a specific project + const addFolderToZip = async (dirPath: string, zipFolder: JSZip) => { + try { + const entries = await fs.readdir(dirPath, { withFileTypes: true }); + + for (const entry of entries) { + const fullPath = `${dirPath}/${entry.name}`; + + if (entry.isDirectory()) { + // Create folder in zip and recursively add its contents + const folder = zipFolder.folder(entry.name); + if (folder) { + await addFolderToZip(fullPath, folder); + } + } else if (entry.isFile()) { + // Add file to zip + try { + const fileContent = await fs.readFile(fullPath); + zipFolder.file(entry.name, fileContent); + } catch (error) { + console.warn(`Failed to read file ${fullPath}:`, error); + } + } + } + } catch (error) { + console.warn(`Failed to read directory ${dirPath}:`, error); + } + }; + + // Start from the project directory + await addFolderToZip(projectPath, zip); + + // Generate zip file + const content = await zip.generateAsync({ type: 'blob' }); + + // Create download link + const url = URL.createObjectURL(content); + const link = document.createElement('a'); + link.href = url; + link.download = `${project.id}.zip`; + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + URL.revokeObjectURL(url); + + toast({ + title: "Project exported successfully", + description: `"${project.name}" has been downloaded as a zip file.`, + }); + } catch (error) { + console.error('Failed to export project:', error); + toast({ + title: "Failed to export project", + description: error instanceof Error ? error.message : "An unexpected error occurred", + variant: "destructive", + }); + } finally { + setIsExporting(false); + } + }; + return ( e.preventDefault()}> @@ -223,6 +330,40 @@ export function ProjectDetailsDialog({ project, open, onOpenChange }: ProjectDet {/* Labels */} + + + + {/* Export Project Button */} + + + {/* Delete Project Button */} + + + Delete Project + + } + /> diff --git a/src/components/ProjectTitleMenu.tsx b/src/components/ProjectTitleMenu.tsx index 4b5c8ad3..4ba8d067 100644 --- a/src/components/ProjectTitleMenu.tsx +++ b/src/components/ProjectTitleMenu.tsx @@ -12,7 +12,6 @@ import { Folder, Copy, ChevronDown, - Download, Trash2, Star, } from 'lucide-react'; @@ -26,7 +25,6 @@ interface ProjectTitleMenuProps { onNewChat: () => void; onGitHistory: () => void; onDuplicate: () => void; - onExport: () => void; onDelete: () => void; onProjectDetails: () => void; isAILoading: boolean; @@ -40,7 +38,6 @@ export function ProjectTitleMenu({ onNewChat, onGitHistory, onDuplicate, - onExport, onDelete, onProjectDetails, isAILoading, @@ -130,15 +127,6 @@ export function ProjectTitleMenu({ {t('duplicateProject')} - - - Export Project - - { - if (!project) return; - - setIsExporting(true); - try { - const zip = new JSZip(); - const projectPath = `${projectsPath}/${project.id}`; - - // Recursive function to add files and directories to zip from a specific project - const addFolderToZip = async (dirPath: string, zipFolder: JSZip) => { - try { - const entries = await fs.readdir(dirPath, { withFileTypes: true }); - - for (const entry of entries) { - const fullPath = `${dirPath}/${entry.name}`; - - if (entry.isDirectory()) { - // Create folder in zip and recursively add its contents - const folder = zipFolder.folder(entry.name); - if (folder) { - await addFolderToZip(fullPath, folder); - } - } else if (entry.isFile()) { - // Add file to zip - try { - const fileContent = await fs.readFile(fullPath); - zipFolder.file(entry.name, fileContent); - } catch (error) { - console.warn(`Failed to read file ${fullPath}:`, error); - } - } - } - } catch (error) { - console.warn(`Failed to read directory ${dirPath}:`, error); - } - }; - - // Start from the project directory - await addFolderToZip(projectPath, zip); - - // Generate zip file - const content = await zip.generateAsync({ type: 'blob' }); - - // Create download link - const url = URL.createObjectURL(content); - const link = document.createElement('a'); - link.href = url; - link.download = `${project.id}.zip`; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - URL.revokeObjectURL(url); - - toast({ - title: "Project exported successfully", - description: `"${project.name}" has been downloaded as a zip file.`, - }); - } catch (error) { - console.error('Failed to export project:', error); - toast({ - title: "Failed to export project", - description: error instanceof Error ? error.message : "An unexpected error occurred", - variant: "destructive", - }); - } finally { - setIsExporting(false); - } - }; - const handleDeleteProject = async () => { if (!project) return; @@ -293,11 +211,10 @@ export function ProjectView() { onNewChat={handleNewChat} onGitHistory={() => setGitHistoryOpen(true)} onDuplicate={() => setDuplicateDialogOpen(true)} - onExport={handleExportProject} onDelete={() => setDeleteDialogOpen(true)} onProjectDetails={() => setIsProjectDetailsOpen(true)} isAILoading={isAILoading} - isAnyLoading={isAnyLoading || isExporting || isDeleting} + isAnyLoading={isAnyLoading || isDeleting} className="text-base" /> ) : ( @@ -446,6 +363,7 @@ export function ProjectView() { project={project} open={isProjectDetailsOpen} onOpenChange={setIsProjectDetailsOpen} + onProjectDeleted={handleProjectDeleted} /> - - - - Delete Project - - Are you sure you want to delete "{project.name}"? This action cannot be undone and will permanently delete all project data including files. - - - - Cancel - - {isDeleting ? ( - <> - - Deleting... - - ) : ( - 'Delete Project' - )} - - - - + )} @@ -543,11 +441,10 @@ export function ProjectView() { onNewChat={handleNewChat} onGitHistory={() => setGitHistoryOpen(true)} onDuplicate={() => setDuplicateDialogOpen(true)} - onExport={handleExportProject} onDelete={() => setDeleteDialogOpen(true)} onProjectDetails={() => setIsProjectDetailsOpen(true)} isAILoading={isAILoading} - isAnyLoading={isAnyLoading || isExporting || isDeleting} + isAnyLoading={isAnyLoading || isDeleting} className="text-lg" /> ) : ( @@ -638,6 +535,7 @@ export function ProjectView() { project={project} open={isProjectDetailsOpen} onOpenChange={setIsProjectDetailsOpen} + onProjectDeleted={handleProjectDeleted} /> - - - - Delete Project - - Are you sure you want to delete "{project.name}"? This action cannot be undone and will permanently delete all project data including files. - - - - Cancel - - {isDeleting ? ( - <> - - Deleting... - - ) : ( - 'Delete Project' - )} - - - - + )}