-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddProjectModal.tsx
More file actions
303 lines (280 loc) · 9.83 KB
/
Copy pathAddProjectModal.tsx
File metadata and controls
303 lines (280 loc) · 9.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { FolderOpen, FolderPlus, ChevronRight } from 'lucide-react';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Label } from './ui/label';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from './ui/dialog';
import { cn } from '../lib/utils';
import { addProject } from '../stores/project-store';
import type { Project } from '../../shared/types';
type ModalStep = 'choose' | 'create-form';
interface AddProjectModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onProjectAdded?: (project: Project, needsInit: boolean) => void;
}
export function AddProjectModal({ open, onOpenChange, onProjectAdded }: AddProjectModalProps) {
const { t } = useTranslation('dialogs');
const [step, setStep] = useState<ModalStep>('choose');
const [projectName, setProjectName] = useState('');
const [projectLocation, setProjectLocation] = useState('');
const [initGit, setInitGit] = useState(true);
const [isCreating, setIsCreating] = useState(false);
const [error, setError] = useState<string | null>(null);
// Reset state when modal opens
useEffect(() => {
if (open) {
setStep('choose');
setProjectName('');
setProjectLocation('');
setInitGit(true);
setError(null);
}
}, [open]);
// Load default location on mount
useEffect(() => {
const loadDefaultLocation = async () => {
try {
const defaultDir = await window.electronAPI.getDefaultProjectLocation();
if (defaultDir) {
setProjectLocation(defaultDir);
}
} catch {
// Ignore - will just be empty
}
};
loadDefaultLocation();
}, []);
const handleOpenExisting = async () => {
try {
const path = await window.electronAPI.selectDirectory();
if (path) {
const project = await addProject(path);
if (project) {
// Auto-detect and save the main branch for the project
try {
const mainBranchResult = await window.electronAPI.detectMainBranch(path);
if (mainBranchResult.success && mainBranchResult.data) {
await window.electronAPI.updateProjectSettings(project.id, {
mainBranch: mainBranchResult.data
});
}
} catch {
// Non-fatal - main branch can be set later in settings
}
onProjectAdded?.(project, !project.autoBuildPath);
onOpenChange(false);
}
}
} catch (err) {
setError(err instanceof Error ? err.message : t('addProject.failedToOpen'));
}
};
const handleSelectLocation = async () => {
try {
const path = await window.electronAPI.selectDirectory();
if (path) {
setProjectLocation(path);
}
} catch {
// User cancelled - ignore
}
};
const handleCreateProject = async () => {
if (!projectName.trim()) {
setError(t('addProject.nameRequired'));
return;
}
if (!projectLocation.trim()) {
setError(t('addProject.locationRequired'));
return;
}
setIsCreating(true);
setError(null);
try {
// Create the project folder
const result = await window.electronAPI.createProjectFolder(
projectLocation,
projectName.trim(),
initGit
);
if (!result.success || !result.data) {
setError(result.error || 'Failed to create project folder');
return;
}
// Add the project to our store
const project = await addProject(result.data.path);
if (project) {
// For new projects with git init, set main branch
// Git init creates 'main' branch by default on modern git
if (initGit) {
try {
const mainBranchResult = await window.electronAPI.detectMainBranch(result.data.path);
if (mainBranchResult.success && mainBranchResult.data) {
await window.electronAPI.updateProjectSettings(project.id, {
mainBranch: mainBranchResult.data
});
}
} catch {
// Non-fatal - main branch can be set later in settings
}
}
onProjectAdded?.(project, true); // New projects always need init
onOpenChange(false);
}
} catch (err) {
setError(err instanceof Error ? err.message : t('addProject.failedToCreate'));
} finally {
setIsCreating(false);
}
};
const renderChooseStep = () => (
<>
<DialogHeader>
<DialogTitle>{t('addProject.title')}</DialogTitle>
<DialogDescription>
{t('addProject.description')}
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-3">
{/* Open Existing Option */}
<button
type="button"
onClick={handleOpenExisting}
className={cn(
'w-full flex items-center gap-4 p-4 rounded-xl border border-border',
'bg-card hover:bg-accent hover:border-accent transition-all duration-200',
'text-left group'
)}
aria-label={t('addProject.openExistingAriaLabel')}
>
<div className="flex h-12 w-12 shrink-0 items-center justify-center rounded-lg bg-primary/10">
<FolderOpen className="h-6 w-6 text-primary" />
</div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-foreground">{t('addProject.openExisting')}</h3>
<p className="text-sm text-muted-foreground mt-0.5">
{t('addProject.openExistingDescription')}
</p>
</div>
<ChevronRight className="h-5 w-5 text-muted-foreground group-hover:text-foreground transition-colors" />
</button>
{/* Create New Option */}
<button
type="button"
onClick={() => setStep('create-form')}
className={cn(
'w-full flex items-center gap-4 p-4 rounded-xl border border-border',
'bg-card hover:bg-accent hover:border-accent transition-all duration-200',
'text-left group'
)}
aria-label={t('addProject.createNewAriaLabel')}
>
<div className="flex h-12 w-12 shrink-0 items-center justify-center rounded-lg bg-success/10">
<FolderPlus className="h-6 w-6 text-success" />
</div>
<div className="flex-1 min-w-0">
<h3 className="font-medium text-foreground">{t('addProject.createNew')}</h3>
<p className="text-sm text-muted-foreground mt-0.5">
{t('addProject.createNewDescription')}
</p>
</div>
<ChevronRight className="h-5 w-5 text-muted-foreground group-hover:text-foreground transition-colors" />
</button>
</div>
{error && (
<div className="text-sm text-destructive bg-destructive/10 rounded-lg p-3 mt-2" role="alert">
{error}
</div>
)}
</>
);
const renderCreateForm = () => (
<>
<DialogHeader>
<DialogTitle>{t('addProject.createNewTitle')}</DialogTitle>
<DialogDescription>
{t('addProject.createNewSubtitle')}
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-4">
{/* Project Name */}
<div className="space-y-2">
<Label htmlFor="project-name">{t('addProject.projectName')}</Label>
<Input
id="project-name"
placeholder={t('addProject.projectNamePlaceholder')}
value={projectName}
onChange={(e) => setProjectName(e.target.value)}
autoFocus
/>
<p className="text-xs text-muted-foreground">
{t('addProject.projectNameHelp')}
</p>
</div>
{/* Location */}
<div className="space-y-2">
<Label htmlFor="project-location">{t('addProject.location')}</Label>
<div className="flex gap-2">
<Input
id="project-location"
placeholder={t('addProject.locationPlaceholder')}
value={projectLocation}
onChange={(e) => setProjectLocation(e.target.value)}
className="flex-1"
/>
<Button variant="outline" onClick={handleSelectLocation}>
{t('addProject.browse')}
</Button>
</div>
{projectLocation && projectName && (
<p className="text-xs text-muted-foreground">
{t('addProject.willCreate')} <code className="bg-muted px-1 py-0.5 rounded">{projectLocation}/{projectName}</code>
</p>
)}
</div>
{/* Git Init Checkbox */}
<div className="flex items-center gap-2">
<input
type="checkbox"
id="init-git"
checked={initGit}
onChange={(e) => setInitGit(e.target.checked)}
className="h-4 w-4 rounded border-border bg-background"
/>
<Label htmlFor="init-git" className="text-sm font-normal cursor-pointer">
{t('addProject.initGit')}
</Label>
</div>
{error && (
<div className="text-sm text-destructive bg-destructive/10 rounded-lg p-3" role="alert">
{error}
</div>
)}
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setStep('choose')} disabled={isCreating}>
{t('addProject.back')}
</Button>
<Button onClick={handleCreateProject} disabled={isCreating}>
{isCreating ? t('addProject.creating') : t('addProject.createProject')}
</Button>
</DialogFooter>
</>
);
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
{step === 'choose' ? renderChooseStep() : renderCreateForm()}
</DialogContent>
</Dialog>
);
}