mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add download zip
This commit is contained in:
1 parent
f6488523f3
commit
a3fdf5f4bd
3 files changed
+174
-1
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Plus, Loader2, Info, MoreVertical, Trash2, Eye, Star, Bug, Folder } from 'lucide-react';
|
||||
import { Plus, Loader2, Info, MoreVertical, Trash2, Eye, Star, Bug, Folder, Download } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Label } from '@/components/ui/label';
|
||||
@@ -12,8 +12,10 @@ import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import type { Project } from '@/lib/ProjectsManager';
|
||||
import { cn } from '@/lib/utils';
|
||||
import JSZip from 'jszip';
|
||||
|
||||
interface ProjectSidebarProps {
|
||||
selectedProject: Project | null;
|
||||
@@ -29,10 +31,12 @@ export function ProjectSidebar({
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
const [favorites, setFavorites] = useLocalStorage<string[]>('project-favorites', []);
|
||||
const projectsManager = useProjectsManager();
|
||||
const navigate = useNavigate();
|
||||
const { toast } = useToast();
|
||||
const { fs } = useFS();
|
||||
|
||||
// Load projects on mount
|
||||
React.useEffect(() => {
|
||||
@@ -101,6 +105,72 @@ export function ProjectSidebar({
|
||||
|
||||
const isFavorite = (projectId: string) => favorites.includes(projectId);
|
||||
|
||||
const handleExportFiles = async () => {
|
||||
setIsExporting(true);
|
||||
try {
|
||||
const zip = new JSZip();
|
||||
|
||||
// Recursive function to add files and directories to zip
|
||||
const addToZip = async (dirPath: string, zipFolder: JSZip) => {
|
||||
try {
|
||||
const entries = await fs.readdir(dirPath, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = dirPath === '/' ? `/${entry.name}` : `${dirPath}/${entry.name}`;
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
// Create folder in zip and recursively add its contents
|
||||
const folder = zipFolder.folder(entry.name);
|
||||
if (folder) {
|
||||
await addToZip(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 root directory
|
||||
await addToZip('/', 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 = selectedProject ? `${selectedProject.name}-files.zip` : 'project-files.zip';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
toast({
|
||||
title: "Files exported successfully",
|
||||
description: "Your project files have been downloaded as a zip file.",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to export files:', error);
|
||||
toast({
|
||||
title: "Failed to export files",
|
||||
description: error instanceof Error ? error.message : "An unexpected error occurred",
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setIsExporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Sort projects: favorites first, then by lastModified (newest first)
|
||||
const sortedProjects = projects.sort((a, b) => {
|
||||
const aIsFavorite = isFavorite(a.id);
|
||||
@@ -141,6 +211,14 @@ export function ProjectSidebar({
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="w-48">
|
||||
<DropdownMenuItem
|
||||
onClick={handleExportFiles}
|
||||
disabled={isExporting || !selectedProject}
|
||||
className="flex items-center gap-2 w-full"
|
||||
>
|
||||
<Download className="h-4 w-4" />
|
||||
{isExporting ? 'Exporting...' : 'Export Files'}
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem asChild>
|
||||
<a href="https://soapbox.pub/mkstack" target="_blank" rel="noopener noreferrer" className="flex items-center gap-2 w-full">
|
||||
<Info className="h-4 w-4" />
|
||||
|
||||
Reference in new issue
Block a user