-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubSetupModal.tsx
More file actions
932 lines (865 loc) · 34.7 KB
/
Copy pathGitHubSetupModal.tsx
File metadata and controls
932 lines (865 loc) · 34.7 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import {
GitBranch as Github,
GitBranch,
Key,
Loader2,
CheckCircle2,
ChevronRight,
Sparkles,
Plus,
Link,
Lock,
Globe,
Building,
User
} from 'lucide-react';
import { Button } from './ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from './ui/dialog';
import { Label } from './ui/label';
import { Input } from './ui/input';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from './ui/select';
import { GitHubOAuthFlow } from './project-settings/GitHubOAuthFlow';
import { ClaudeOAuthFlow } from './project-settings/ClaudeOAuthFlow';
import type { Project, } from '../../shared/types';
interface GitHubSetupModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
project: Project;
onComplete: (settings: { githubToken: string; githubRepo: string; mainBranch: string; githubAuthMethod?: 'oauth' | 'pat' }) => void;
onSkip?: () => void;
}
type SetupStep = 'github-auth' | 'claude-auth' | 'repo-confirm' | 'repo' | 'branch' | 'complete';
/**
* Setup Modal - Required setup flow after Auto Code initialization
*
* Flow:
* 1. Authenticate with GitHub (via gh CLI OAuth) - for repo operations
* 2. Authenticate with Claude (via claude CLI OAuth) - for AI features
* 3. Detect/confirm repository
* 4. Select base branch for tasks (with recommended default)
*/
export function GitHubSetupModal({
open,
onOpenChange,
project,
onComplete,
onSkip
}: GitHubSetupModalProps) {
const { t } = useTranslation('dialogs');
const [step, setStep] = useState<SetupStep>('github-auth');
const [githubToken, setGithubToken] = useState<string | null>(null);
const [githubRepo, setGithubRepo] = useState<string | null>(null);
const [detectedRepo, setDetectedRepo] = useState<string | null>(null);
const [branches, setBranches] = useState<string[]>([]);
const [selectedBranch, setSelectedBranch] = useState<string | null>(null);
const [recommendedBranch, setRecommendedBranch] = useState<string | null>(null);
const [isLoadingBranches, setIsLoadingBranches] = useState(false);
const [isLoadingRepo, setIsLoadingRepo] = useState(false);
const [error, setError] = useState<string | null>(null);
// Repo setup state (for when no remote is detected)
const [repoAction, setRepoAction] = useState<'create' | 'link' | null>(null);
const [newRepoName, setNewRepoName] = useState('');
const [isPrivateRepo, setIsPrivateRepo] = useState(true);
const [existingRepoName, setExistingRepoName] = useState('');
const [isCreatingRepo, setIsCreatingRepo] = useState(false);
// Organization selection state
const [githubUsername, setGithubUsername] = useState<string | null>(null);
const [organizations, setOrganizations] = useState<Array<{ login: string; avatarUrl?: string }>>([]);
const [selectedOwner, setSelectedOwner] = useState<string | null>(null);
const [isLoadingOrgs, setIsLoadingOrgs] = useState(false);
// Load user info and organizations
const loadUserAndOrgs = async () => {
setIsLoadingOrgs(true);
try {
// Get current user
const userResult = await window.electronAPI.getGitHubUser();
if (userResult.success && userResult.data) {
setGithubUsername(userResult.data.username);
setSelectedOwner(userResult.data.username); // Default to personal account
}
// Get organizations
const orgsResult = await window.electronAPI.listGitHubOrgs();
if (orgsResult.success && orgsResult.data) {
setOrganizations(orgsResult.data.orgs);
}
} catch (err) {
console.error('Failed to load user/orgs:', err);
} finally {
setIsLoadingOrgs(false);
}
};
// Detect repository from git remote when auth succeeds
const detectRepository = async () => {
setIsLoadingRepo(true);
setError(null);
try {
// Try to detect repo from git remote
const result = await window.electronAPI.detectGitHubRepo(project.path);
if (result.success && result.data) {
setDetectedRepo(result.data);
setGithubRepo(result.data);
// Go to confirmation step instead of directly to branch
setStep('repo-confirm');
} else {
// No remote detected, load orgs and show repo setup step
await loadUserAndOrgs();
setStep('repo');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to detect repository');
await loadUserAndOrgs();
setStep('repo');
} finally {
setIsLoadingRepo(false);
}
};
// Reset state and check existing auth when modal opens
useEffect(() => {
if (open) {
// Reset all state first
setGithubToken(null);
setGithubRepo(null);
setDetectedRepo(null);
setBranches([]);
setSelectedBranch(null);
setRecommendedBranch(null);
setError(null);
// Reset repo setup state
setRepoAction(null);
setNewRepoName(project.name.replace(/[^A-Za-z0-9_.-]/g, '-'));
setIsPrivateRepo(true);
setExistingRepoName('');
setIsCreatingRepo(false);
// Reset organization state
setGithubUsername(null);
setOrganizations([]);
setSelectedOwner(null);
setIsLoadingOrgs(false);
// Check for existing authentication and skip to appropriate step
const checkExistingAuth = async () => {
try {
// Check for existing GitHub token
const ghTokenResult = await window.electronAPI.getGitHubToken();
const hasGitHubAuth = ghTokenResult.success && ghTokenResult.data?.token;
// Check for existing Claude authentication
const profilesResult = await window.electronAPI.getClaudeProfiles();
let hasClaudeAuth = false;
if (profilesResult.success && profilesResult.data) {
const activeProfile = profilesResult.data.profiles.find(
(p) => p.id === profilesResult.data?.activeProfileId
);
hasClaudeAuth = !!(activeProfile?.oauthToken || (activeProfile?.isDefault && activeProfile?.configDir));
}
// Determine starting step based on existing auth
if (hasGitHubAuth && hasClaudeAuth) {
// Both authenticated, go directly to repo detection
setGithubToken(ghTokenResult.data?.token ?? null);
// detectRepository will be called and set the step
setStep('repo'); // Temporary, detectRepository will update
await detectRepository();
} else if (hasGitHubAuth) {
// Only GitHub authenticated, go to Claude auth
setGithubToken(ghTokenResult.data?.token ?? null);
setStep('claude-auth');
} else {
// No auth, start from beginning
setStep('github-auth');
}
} catch (err) {
console.error('Failed to check existing auth:', err);
// On error, start from beginning
setStep('github-auth');
}
};
checkExistingAuth();
}
// biome-ignore lint/correctness/useExhaustiveDependencies: detectRepository is intentionally not wrapped in useCallback
}, [open, detectRepository, project.name.replace]);
// Load branches from GitHub
const loadBranches = async (repo: string) => {
setIsLoadingBranches(true);
setError(null);
try {
// Get branches from GitHub API
const token = githubToken ?? '';
const result = await window.electronAPI.getGitHubBranches(repo, token);
if (result.success && result.data) {
setBranches(result.data);
// Detect recommended branch (main > master > develop > first)
const recommended = detectRecommendedBranch(result.data);
setRecommendedBranch(recommended);
setSelectedBranch(recommended);
} else {
setError(result.error || 'Failed to load branches');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load branches');
} finally {
setIsLoadingBranches(false);
}
};
// Detect recommended branch from list
const detectRecommendedBranch = (branchList: string[]): string | null => {
const priorities = ['main', 'master', 'develop', 'dev'];
for (const priority of priorities) {
if (branchList.includes(priority)) {
return priority;
}
}
return branchList[0] || null;
};
// Handle GitHub OAuth success
const handleGitHubAuthSuccess = async (token: string) => {
setGithubToken(token);
// Check if Claude is already authenticated before showing auth step
try {
const profilesResult = await window.electronAPI.getClaudeProfiles();
if (profilesResult.success && profilesResult.data) {
const activeProfile = profilesResult.data.profiles.find(
(p) => p.id === profilesResult.data?.activeProfileId
);
// Check if active profile has authentication (oauthToken or default with configDir)
if (activeProfile?.oauthToken || (activeProfile?.isDefault && activeProfile?.configDir)) {
// Already authenticated, skip Claude auth and go directly to repo detection
await detectRepository();
return;
}
}
} catch (err) {
console.error('Failed to check Claude profiles:', err);
// On error, fall through to show Claude auth step
}
// Not authenticated, show Claude auth step
setStep('claude-auth');
};
// Handle Claude OAuth success
const handleClaudeAuthSuccess = async () => {
// Claude token is already saved to active profile by the OAuth flow
// Move to repo detection
await detectRepository();
};
// Handle creating a new GitHub repository
const handleCreateRepo = async () => {
if (!newRepoName.trim()) {
setError('Please enter a repository name');
return;
}
if (!selectedOwner) {
setError('Please select an owner for the repository');
return;
}
setIsCreatingRepo(true);
setError(null);
try {
const result = await window.electronAPI.createGitHubRepo(newRepoName.trim(), {
isPrivate: isPrivateRepo,
projectPath: project.path,
owner: selectedOwner !== githubUsername ? selectedOwner : undefined // Only pass owner if it's an org
});
if (result.success && result.data) {
// Repo created and remote added automatically by gh CLI
setGithubRepo(result.data.fullName);
setDetectedRepo(result.data.fullName);
setStep('branch');
await loadBranches(result.data.fullName);
} else {
setError(result.error || 'Failed to create repository');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create repository');
} finally {
setIsCreatingRepo(false);
}
};
// Handle confirming the detected repository
const handleConfirmRepo = async () => {
if (detectedRepo) {
setStep('branch');
await loadBranches(detectedRepo);
}
};
// Handle changing the repository (go to repo setup)
const handleChangeRepo = async () => {
await loadUserAndOrgs();
setStep('repo');
};
// Handle linking to an existing GitHub repository
const handleLinkRepo = async () => {
if (!existingRepoName.trim()) {
setError('Please enter a repository name (owner/repo format)');
return;
}
// Validate format
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(existingRepoName.trim())) {
setError('Invalid format. Use owner/repo (e.g., username/my-project)');
return;
}
setIsCreatingRepo(true);
setError(null);
try {
const result = await window.electronAPI.addGitRemote(project.path, existingRepoName.trim());
if (result.success) {
setGithubRepo(existingRepoName.trim());
setDetectedRepo(existingRepoName.trim());
setStep('branch');
await loadBranches(existingRepoName.trim());
} else {
setError(result.error || 'Failed to add remote');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to add remote');
} finally {
setIsCreatingRepo(false);
}
};
// Handle branch selection complete
const handleComplete = () => {
if (githubToken && githubRepo && selectedBranch) {
onComplete({
githubToken,
githubRepo,
mainBranch: selectedBranch,
githubAuthMethod: 'oauth' // Setup modal always uses OAuth flow
});
}
};
// Render step content
const renderStepContent = () => {
switch (step) {
case 'github-auth':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Github className="h-5 w-5" />
{t('githubSetup.connectTitle')}
</DialogTitle>
<DialogDescription>
{t('githubSetup.connectDescription')}
</DialogDescription>
</DialogHeader>
<div className="py-4">
<GitHubOAuthFlow
onSuccess={handleGitHubAuthSuccess}
onCancel={onSkip}
/>
</div>
</>
);
case 'claude-auth':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Key className="h-5 w-5" />
{t('githubSetup.claudeTitle')}
</DialogTitle>
<DialogDescription>
{t('githubSetup.claudeDescription')}
</DialogDescription>
</DialogHeader>
<div className="py-4">
<ClaudeOAuthFlow
onSuccess={handleClaudeAuthSuccess}
onCancel={onSkip}
/>
</div>
</>
);
case 'repo-confirm':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Github className="h-5 w-5" />
Confirm Repository
</DialogTitle>
<DialogDescription>
We detected a GitHub repository for this project. Please confirm or change it.
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-4">
<div className="rounded-lg border bg-muted/50 p-4">
<div className="flex items-center gap-3">
<CheckCircle2 className="h-6 w-6 text-green-500" />
<div>
<p className="font-medium">Repository Detected</p>
<p className="text-sm text-muted-foreground font-mono">
{detectedRepo}
</p>
</div>
</div>
</div>
<p className="text-sm text-muted-foreground">
{t('githubSetup.repoDescription')}
</p>
{error && (
<div className="rounded-lg bg-destructive/10 border border-destructive/30 p-3 text-sm text-destructive">
{error}
</div>
)}
</div>
<DialogFooter>
<Button variant="outline" onClick={handleChangeRepo}>
Use Different Repository
</Button>
<Button onClick={handleConfirmRepo}>
<CheckCircle2 className="mr-2 h-4 w-4" />
Confirm & Continue
</Button>
</DialogFooter>
</>
);
case 'repo':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Github className="h-5 w-5" />
Connect to GitHub
</DialogTitle>
<DialogDescription>
Your project needs a GitHub repository. Create a new one or link to an existing repository.
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-4">
{/* Action selection */}
{!repoAction && (
<div className="grid grid-cols-2 gap-3">
<button
type="button"
onClick={() => setRepoAction('create')}
className="flex flex-col items-center gap-2 p-4 rounded-lg border-2 border-dashed hover:border-primary hover:bg-primary/5 transition-colors"
aria-label={t('githubSetup.createRepoAriaLabel')}
>
<Plus className="h-8 w-8 text-muted-foreground" />
<span className="text-sm font-medium">Create New Repo</span>
<span className="text-xs text-muted-foreground text-center">
Create a new repository on GitHub
</span>
</button>
<button
type="button"
onClick={() => setRepoAction('link')}
className="flex flex-col items-center gap-2 p-4 rounded-lg border-2 border-dashed hover:border-primary hover:bg-primary/5 transition-colors"
aria-label={t('githubSetup.linkRepoAriaLabel')}
>
<Link className="h-8 w-8 text-muted-foreground" />
<span className="text-sm font-medium">Link Existing</span>
<span className="text-xs text-muted-foreground text-center">
Connect to an existing repository
</span>
</button>
</div>
)}
{/* Create new repo form */}
{repoAction === 'create' && (
<div className="space-y-4">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<button
type="button"
onClick={() => setRepoAction(null)}
className="text-primary hover:underline"
aria-label={t('githubSetup.goBackAriaLabel')}
>
← Back
</button>
<span>Create a new repository</span>
</div>
{/* Owner selection */}
<div className="space-y-2">
<Label>Owner</Label>
{isLoadingOrgs ? (
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Loader2 className="h-4 w-4 animate-spin" />
Loading accounts...
</div>
) : (
<div className="flex flex-wrap gap-2" role="radiogroup" aria-label={t('common:accessibility.repositoryOwnerAriaLabel')}>
{/* Personal account */}
{githubUsername && (
/* biome-ignore lint/a11y/useSemanticElements: Using button with role="radio" for styled radio-like selection */
<button
type="button"
onClick={() => setSelectedOwner(githubUsername)}
className={`flex items-center gap-2 px-3 py-2 rounded-md border ${
selectedOwner === githubUsername
? 'border-primary bg-primary/10 text-primary'
: 'border-muted hover:border-primary/50'
}`}
disabled={isCreatingRepo}
role="radio"
aria-checked={selectedOwner === githubUsername}
aria-label={t('githubSetup.selectOwnerAriaLabel', { owner: githubUsername })}
>
<User className="h-4 w-4" />
<span className="text-sm">{githubUsername}</span>
</button>
)}
{/* Organizations */}
{organizations.map((org) => (
/* biome-ignore lint/a11y/useSemanticElements: Using button with role="radio" for styled radio-like selection */
<button
type="button"
key={org.login}
onClick={() => setSelectedOwner(org.login)}
className={`flex items-center gap-2 px-3 py-2 rounded-md border ${
selectedOwner === org.login
? 'border-primary bg-primary/10 text-primary'
: 'border-muted hover:border-primary/50'
}`}
disabled={isCreatingRepo}
role="radio"
aria-checked={selectedOwner === org.login}
aria-label={t('githubSetup.selectOrgAriaLabel', { org: org.login })}
>
<Building className="h-4 w-4" />
<span className="text-sm">{org.login}</span>
</button>
))}
</div>
)}
{organizations.length > 0 && (
<p className="text-xs text-muted-foreground">
Select your personal account or an organization
</p>
)}
</div>
<div className="space-y-2">
<Label htmlFor="repo-name">Repository Name</Label>
<div className="flex items-center gap-2">
<span className="text-sm text-muted-foreground">
{selectedOwner || '...'} /
</span>
<Input
id="repo-name"
value={newRepoName}
onChange={(e) => setNewRepoName(e.target.value)}
placeholder="my-project"
disabled={isCreatingRepo}
className="flex-1"
/>
</div>
</div>
<div className="space-y-2">
<Label>Visibility</Label>
<div className="flex gap-2" role="radiogroup" aria-label={t('common:accessibility.repositoryVisibilityAriaLabel')}>
{/* biome-ignore lint/a11y/useSemanticElements: Using button with role="radio" for styled radio-like selection */}
<button
type="button"
onClick={() => setIsPrivateRepo(true)}
className={`flex items-center gap-2 px-3 py-2 rounded-md border ${
isPrivateRepo
? 'border-primary bg-primary/10 text-primary'
: 'border-muted hover:border-primary/50'
}`}
disabled={isCreatingRepo}
role="radio"
aria-checked={isPrivateRepo}
aria-label={t('githubSetup.selectVisibilityAriaLabel', { visibility: 'private' })}
>
<Lock className="h-4 w-4" />
<span className="text-sm">Private</span>
</button>
{/* biome-ignore lint/a11y/useSemanticElements: Using button with role="radio" for styled radio-like selection */}
<button
type="button"
onClick={() => setIsPrivateRepo(false)}
className={`flex items-center gap-2 px-3 py-2 rounded-md border ${
!isPrivateRepo
? 'border-primary bg-primary/10 text-primary'
: 'border-muted hover:border-primary/50'
}`}
disabled={isCreatingRepo}
role="radio"
aria-checked={!isPrivateRepo}
aria-label={t('githubSetup.selectVisibilityAriaLabel', { visibility: 'public' })}
>
<Globe className="h-4 w-4" />
<span className="text-sm">Public</span>
</button>
</div>
</div>
</div>
)}
{/* Link existing repo form */}
{repoAction === 'link' && (
<div className="space-y-4">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<button
type="button"
onClick={() => setRepoAction(null)}
className="text-primary hover:underline"
aria-label={t('githubSetup.goBackAriaLabel')}
>
← Back
</button>
<span>Link to existing repository</span>
</div>
<div className="space-y-2">
<Label htmlFor="existing-repo">Repository</Label>
<Input
id="existing-repo"
value={existingRepoName}
onChange={(e) => setExistingRepoName(e.target.value)}
placeholder="username/repository"
disabled={isCreatingRepo}
/>
<p className="text-xs text-muted-foreground">
Enter the full repository path (e.g., octocat/hello-world)
</p>
</div>
</div>
)}
{error && (
<div className="rounded-lg bg-destructive/10 border border-destructive/30 p-3 text-sm text-destructive">
{error}
</div>
)}
</div>
<DialogFooter>
{onSkip && (
<Button variant="outline" onClick={onSkip} disabled={isCreatingRepo}>
Skip for now
</Button>
)}
{repoAction === 'create' && (
<Button onClick={handleCreateRepo} disabled={isCreatingRepo || !newRepoName.trim()}>
{isCreatingRepo ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Creating...
</>
) : (
<>
<Plus className="mr-2 h-4 w-4" />
Create Repository
</>
)}
</Button>
)}
{repoAction === 'link' && (
<Button onClick={handleLinkRepo} disabled={isCreatingRepo || !existingRepoName.trim()}>
{isCreatingRepo ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Linking...
</>
) : (
<>
<Link className="mr-2 h-4 w-4" />
Link Repository
</>
)}
</Button>
)}
{!repoAction && (
<Button variant="outline" onClick={detectRepository} disabled={isLoadingRepo}>
{isLoadingRepo ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Checking...
</>
) : (
'Retry Detection'
)}
</Button>
)}
</DialogFooter>
</>
);
case 'branch':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<GitBranch className="h-5 w-5" />
Select Base Branch
</DialogTitle>
<DialogDescription>
Choose which branch Auto Code should use as the base for creating task branches.
</DialogDescription>
</DialogHeader>
<div className="py-4 space-y-4">
{/* Show detected repo */}
{detectedRepo && (
<div className="flex items-center gap-2 text-sm">
<Github className="h-4 w-4 text-muted-foreground" />
<span className="text-muted-foreground">Repository:</span>
<code className="px-2 py-0.5 bg-muted rounded font-mono text-xs">
{detectedRepo}
</code>
<CheckCircle2 className="h-4 w-4 text-success" />
</div>
)}
{/* Branch selector */}
<div className="space-y-2">
<Label>Base Branch</Label>
<Select
value={selectedBranch || ''}
onValueChange={setSelectedBranch}
disabled={isLoadingBranches || branches.length === 0}
>
<SelectTrigger>
{isLoadingBranches ? (
<div className="flex items-center gap-2">
<Loader2 className="h-3 w-3 animate-spin" />
<span>Loading branches...</span>
</div>
) : (
<SelectValue placeholder="Select a branch" />
)}
</SelectTrigger>
<SelectContent>
{branches.map((branch) => (
<SelectItem key={branch} value={branch}>
<div className="flex items-center gap-2">
<span>{branch}</span>
{branch === recommendedBranch && (
<span className="flex items-center gap-1 text-xs text-success">
<Sparkles className="h-3 w-3" />
Recommended
</span>
)}
</div>
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
All tasks will be created from branches like{' '}
<code className="px-1 bg-muted rounded">auto-claude/task-name</code>
{selectedBranch && (
<> based on <code className="px-1 bg-muted rounded">{selectedBranch}</code></>
)}
</p>
</div>
{/* Info about branch selection */}
<div className="rounded-lg border border-info/30 bg-info/5 p-3">
<div className="flex items-start gap-2">
<Sparkles className="h-4 w-4 text-info mt-0.5" />
<div className="text-xs text-muted-foreground">
<p className="font-medium text-foreground">Why select a branch?</p>
<p className="mt-1">
Auto Code creates isolated workspaces for each task. Selecting the right base branch ensures
your tasks start with the latest code from your main development line.
</p>
</div>
</div>
</div>
{error && (
<div className="rounded-lg bg-destructive/10 border border-destructive/30 p-3 text-sm text-destructive">
{error}
</div>
)}
</div>
<DialogFooter>
{onSkip && (
<Button variant="outline" onClick={onSkip}>
Skip for now
</Button>
)}
<Button
onClick={handleComplete}
disabled={!selectedBranch || isLoadingBranches}
>
<CheckCircle2 className="mr-2 h-4 w-4" />
Complete Setup
</Button>
</DialogFooter>
</>
);
case 'complete':
return (
<>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<CheckCircle2 className="h-5 w-5 text-success" />
Setup Complete
</DialogTitle>
</DialogHeader>
<div className="py-8 flex flex-col items-center justify-center">
<div className="h-16 w-16 rounded-full bg-success/10 flex items-center justify-center mb-4">
<CheckCircle2 className="h-8 w-8 text-success" />
</div>
<p className="text-sm text-muted-foreground text-center">
Auto Code is ready to use! You can now create tasks that will be
automatically based on <code className="px-1 bg-muted rounded">{selectedBranch}</code>.
</p>
</div>
</>
);
}
};
// Progress indicator
const renderProgress = () => {
const steps: { label: string }[] = [
{ label: 'Authenticate' },
{ label: 'Configure' },
];
// Don't show progress on complete step
if (step === 'complete') return null;
// Map steps to progress indices
// Auth steps (github-auth, claude-auth, repo) = 0
// Config steps (branch) = 1
const currentIndex =
step === 'github-auth' ? 0 :
step === 'claude-auth' ? 0 :
step === 'repo' ? 0 :
1;
return (
<div className="flex items-center justify-center gap-2 mb-4">
{steps.map((s, index) => (
<div key={s.label} className="flex items-center">
<div
className={`flex items-center justify-center w-6 h-6 rounded-full text-xs font-medium ${
index < currentIndex
? 'bg-success text-success-foreground'
: index === currentIndex
? 'bg-primary text-primary-foreground'
: 'bg-muted text-muted-foreground'
}`}
>
{index < currentIndex ? (
<CheckCircle2 className="h-4 w-4" />
) : (
index + 1
)}
</div>
<span className={`ml-2 text-xs ${
index === currentIndex ? 'text-foreground font-medium' : 'text-muted-foreground'
}`}>
{s.label}
</span>
{index < steps.length - 1 && (
<ChevronRight className="h-4 w-4 mx-2 text-muted-foreground" />
)}
</div>
))}
</div>
);
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
{renderProgress()}
{renderStepContent()}
</DialogContent>
</Dialog>
);
}