|
| 1 | +import React, { useState, useCallback } from "react"; |
| 2 | +import { Modal, ModalActions, CancelButton, PrimaryButton } from "./Modal"; |
| 3 | +import type { ProjectConfig } from "@/config"; |
| 4 | + |
| 5 | +interface ProjectCreateModalProps { |
| 6 | + isOpen: boolean; |
| 7 | + onClose: () => void; |
| 8 | + onSuccess: (normalizedPath: string, projectConfig: ProjectConfig) => void; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Project creation modal that handles the full flow from path input to backend validation. |
| 13 | + * |
| 14 | + * Displays a modal for path input, calls the backend to create the project, and shows |
| 15 | + * validation errors inline. Modal stays open until project is successfully created or user cancels. |
| 16 | + */ |
| 17 | +export const ProjectCreateModal: React.FC<ProjectCreateModalProps> = ({ |
| 18 | + isOpen, |
| 19 | + onClose, |
| 20 | + onSuccess, |
| 21 | +}) => { |
| 22 | + const [path, setPath] = useState(""); |
| 23 | + const [error, setError] = useState(""); |
| 24 | + const [isCreating, setIsCreating] = useState(false); |
| 25 | + |
| 26 | + const handleCancel = useCallback(() => { |
| 27 | + setPath(""); |
| 28 | + setError(""); |
| 29 | + onClose(); |
| 30 | + }, [onClose]); |
| 31 | + |
| 32 | + const handleSelect = useCallback(async () => { |
| 33 | + const trimmedPath = path.trim(); |
| 34 | + if (!trimmedPath) { |
| 35 | + setError("Please enter a directory path"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + setError(""); |
| 40 | + setIsCreating(true); |
| 41 | + |
| 42 | + try { |
| 43 | + // First check if project already exists |
| 44 | + const existingProjects = await window.api.projects.list(); |
| 45 | + const existingPaths = new Map(existingProjects); |
| 46 | + |
| 47 | + // Try to create the project |
| 48 | + const result = await window.api.projects.create(trimmedPath); |
| 49 | + |
| 50 | + if (result.success) { |
| 51 | + // Check if duplicate (backend may normalize the path) |
| 52 | + const { normalizedPath, projectConfig } = result.data as { |
| 53 | + normalizedPath: string; |
| 54 | + projectConfig: ProjectConfig; |
| 55 | + }; |
| 56 | + if (existingPaths.has(normalizedPath)) { |
| 57 | + setError("This project has already been added."); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Success - notify parent and close |
| 62 | + onSuccess(normalizedPath, projectConfig); |
| 63 | + setPath(""); |
| 64 | + setError(""); |
| 65 | + onClose(); |
| 66 | + } else { |
| 67 | + // Backend validation error - show inline, keep modal open |
| 68 | + const errorMessage = |
| 69 | + typeof result.error === "string" ? result.error : "Failed to add project"; |
| 70 | + setError(errorMessage); |
| 71 | + } |
| 72 | + } catch (err) { |
| 73 | + // Unexpected error |
| 74 | + const errorMessage = err instanceof Error ? err.message : "An unexpected error occurred"; |
| 75 | + setError(`Failed to add project: ${errorMessage}`); |
| 76 | + } finally { |
| 77 | + setIsCreating(false); |
| 78 | + } |
| 79 | + }, [path, onSuccess, onClose]); |
| 80 | + |
| 81 | + const handleKeyDown = useCallback( |
| 82 | + (e: React.KeyboardEvent) => { |
| 83 | + if (e.key === "Enter") { |
| 84 | + e.preventDefault(); |
| 85 | + void handleSelect(); |
| 86 | + } |
| 87 | + }, |
| 88 | + [handleSelect] |
| 89 | + ); |
| 90 | + |
| 91 | + return ( |
| 92 | + <Modal |
| 93 | + isOpen={isOpen} |
| 94 | + title="Add Project" |
| 95 | + subtitle="Enter the path to your project directory" |
| 96 | + onClose={handleCancel} |
| 97 | + isLoading={isCreating} |
| 98 | + > |
| 99 | + <input |
| 100 | + type="text" |
| 101 | + value={path} |
| 102 | + onChange={(e) => { |
| 103 | + setPath(e.target.value); |
| 104 | + setError(""); |
| 105 | + }} |
| 106 | + onKeyDown={handleKeyDown} |
| 107 | + placeholder="/home/user/projects/my-project" |
| 108 | + autoFocus |
| 109 | + disabled={isCreating} |
| 110 | + className="bg-modal-bg border-border-medium focus:border-accent placeholder:text-muted mb-5 w-full rounded border px-3 py-2 font-mono text-sm text-white focus:outline-none disabled:opacity-50" |
| 111 | + /> |
| 112 | + {error && <div className="text-error -mt-3 mb-3 text-xs">{error}</div>} |
| 113 | + <ModalActions> |
| 114 | + <CancelButton onClick={handleCancel} disabled={isCreating}> |
| 115 | + Cancel |
| 116 | + </CancelButton> |
| 117 | + <PrimaryButton onClick={() => void handleSelect()} disabled={isCreating}> |
| 118 | + {isCreating ? "Adding..." : "Add Project"} |
| 119 | + </PrimaryButton> |
| 120 | + </ModalActions> |
| 121 | + </Modal> |
| 122 | + ); |
| 123 | +}; |
0 commit comments