diff --git a/src/components/ProjectSidebar.tsx b/src/components/ProjectSidebar.tsx index 3d6782a0..03be8a67 100644 --- a/src/components/ProjectSidebar.tsx +++ b/src/components/ProjectSidebar.tsx @@ -123,10 +123,10 @@ export function ProjectSidebar({ } }; - const handleImportZip = async (file: File, overwrite = false, projectId?: string) => { + const handleImportZip = async (file: File, overwrite = false) => { try { // Import project from zip file - const project = await projectsManager.importProjectFromZip(file, projectId, overwrite); + const project = await projectsManager.importProjectFromZip(file, overwrite); // Show success toast toast({ diff --git a/src/lib/ProjectsManager.test.ts b/src/lib/ProjectsManager.test.ts index 3ae4deef..75b3494d 100644 --- a/src/lib/ProjectsManager.test.ts +++ b/src/lib/ProjectsManager.test.ts @@ -211,8 +211,8 @@ describe('ProjectsManager', () => { // Replace git instance Object.assign(projectsManager, { git: mockGit }); - // Create a mock ZIP file with different name - const zipFile = new File(['test content'], 'different-name.zip', { type: 'application/zip' }); + // Create a mock ZIP file with same name to trigger overwrite + const zipFile = new File(['test content'], 'my-project.zip', { type: 'application/zip' }); // Mock arrayBuffer method Object.defineProperty(zipFile, 'arrayBuffer', { @@ -221,7 +221,7 @@ describe('ProjectsManager', () => { }); // Import with overwrite - const project = await projectsManager.importProjectFromZip(zipFile, 'my-project', true); + const project = await projectsManager.importProjectFromZip(zipFile, true); // Should preserve original project name expect(project.name).toBe('my-project'); @@ -254,7 +254,7 @@ describe('ProjectsManager', () => { }); // Import without overwrite - const project = await projectsManager.importProjectFromZip(zipFile, undefined, false); + const project = await projectsManager.importProjectFromZip(zipFile, false); // Should use formatted name from ID expect(project.name).toBe('new-project'); diff --git a/src/lib/ProjectsManager.ts b/src/lib/ProjectsManager.ts index 79963a49..68e34f5f 100644 --- a/src/lib/ProjectsManager.ts +++ b/src/lib/ProjectsManager.ts @@ -38,8 +38,8 @@ export class ProjectsManager { } } - async createProject(name: string, customId?: string): Promise { - const project = await this.cloneProject(name, GIT_TEMPLATE_URL, customId, { depth: 1 }); + async createProject(name: string): Promise { + const project = await this.cloneProject(name, GIT_TEMPLATE_URL, { depth: 1 }); try { await this.fs.mkdir(this.dir + `/${project.id}/.ai/history`, { recursive: true }); @@ -92,21 +92,24 @@ export class ProjectsManager { return project; } - async importProjectFromZip(zipFile: File, customId?: string, overwrite = false): Promise { - // Generate a project ID based on the zip file name or use custom ID + async importProjectFromZip(zipFile: File, overwrite = false): Promise { + // Generate a project ID based on the zip file name const baseName = zipFile.name.replace(/\.zip$/i, ''); const generatedId = baseName .toLowerCase() .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, ''); + // Check if a project with the generated ID already exists + const existingProjectExists = await this.projectExists(generatedId); + // Determine the project ID to use let id: string; - if (customId) { - // Use the provided custom ID (for overwrite scenarios) - id = customId; + if (overwrite && existingProjectExists) { + // Use the existing project ID for overwrite + id = generatedId; } else { - // If no custom ID, generate a unique ID (for new projects) + // Generate a unique ID for new projects id = await this.generateUniqueProjectId(generatedId); } @@ -269,13 +272,8 @@ export class ProjectsManager { } } - async cloneProject(name: string, repoUrl: string, customId?: string, options?: { depth?: number }): Promise { - const id = customId || await this.generateUniqueProjectId(name); - - // Check if project with this ID already exists when using custom ID - if (customId && await this.projectExists(id)) { - throw new Error(`Project with ID "${id}" already exists`); - } + async cloneProject(name: string, repoUrl: string, options?: { depth?: number }): Promise { + const id = await this.generateUniqueProjectId(name); const projectPath = `${this.dir}/${id}`; diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index 0c99ee16..acb2e47a 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -26,7 +26,7 @@ export default function Index() { const navigate = useNavigate(); const projectsManager = useProjectsManager(); const { fs } = useFS(); - const { generateProjectId, isLoading: isGeneratingId, isConfigured: isAIConfigured } = useAIProjectId(); + const { isLoading: isGeneratingId, isConfigured: isAIConfigured } = useAIProjectId(); const { settings, addRecentlyUsedModel } = useAISettings(); const { toast } = useToast(); const [providerModel, setProviderModel] = useState(() => { @@ -91,14 +91,11 @@ export default function Index() { setIsCreating(true); try { - // Use AI to generate project ID - const projectId = await generateProjectId(providerModel, prompt.trim()); - // Add model to recently used when creating project with AI addRecentlyUsedModel(providerModel.trim()); - // Create project with AI-generated ID - const project = await projectsManager.createProject(prompt.trim(), projectId); + // Create project + const project = await projectsManager.createProject(prompt.trim()); // Store the initial message in chat history using DotAI const dotAI = new DotAI(fs, `/projects/${project.id}`);