-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone-configuration-modal.tsx
More file actions
142 lines (123 loc) · 4.5 KB
/
Copy pathclone-configuration-modal.tsx
File metadata and controls
142 lines (123 loc) · 4.5 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
import { useState, useEffect } from 'react'
import DirectoryPicker from '~/components/directory-picker/directory-picker'
import Button from '~/components/inputs/button'
import CloseButton from '~/components/inputs/close-button'
import Input from '~/components/inputs/input'
import { filesystemService } from '~/services/filesystem-service'
import { joinPath, normalizePath } from '~/utils/path-utils'
interface CloneProjectModalProperties {
isOpen: boolean
isLocal: boolean
onClose: () => void
onClone: (repoUrl: string, localPath: string, token?: string) => void
initialPath?: string
}
export default function CloneConfigurationModal({
isOpen,
isLocal,
onClose,
onClone,
initialPath,
}: Readonly<CloneProjectModalProperties>) {
const [repoUrl, setRepoUrl] = useState('')
const [location, setLocation] = useState('')
const [token, setToken] = useState('')
const [showPicker, setShowPicker] = useState(false)
useEffect(() => {
if (!isOpen || !isLocal) {
if (isOpen) setLocation(initialPath ?? '')
return
}
filesystemService
.resolveNearestAccessiblePath(initialPath ?? '')
.then((resolved) => setLocation(normalizePath(resolved)))
.catch(() => setLocation(''))
}, [isOpen, isLocal, initialPath])
if (!isOpen) return null
const repoName = repoUrl
.split('/')
.pop()
?.replace(/\.git$/, '')
const targetName = repoName || 'cloned-project'
const targetPath = location ? joinPath(location, targetName) : targetName
const handleClone = () => {
if (!repoUrl.trim()) return
if (isLocal && !location) return
onClone(repoUrl.trim(), targetPath, token || undefined)
handleClose()
}
const handleClose = () => {
setRepoUrl('')
setLocation('')
setToken('')
setShowPicker(false)
onClose()
}
return (
<>
<div className="bg-background/50 absolute inset-0 z-50 flex items-center justify-center">
<div className="bg-background border-border relative w-[600px] rounded-lg border p-6 shadow-lg">
<h2 className="mb-4 text-lg font-semibold">Clone Repository</h2>
<p className="text-foreground-muted mb-4 text-sm">
{isLocal ? 'Clone a Git repository to a local folder' : 'Clone a Git repository into the workspace'}
</p>
<div className="mb-4">
<label className="mb-1 block text-sm font-medium">Clone into</label>
<div className="flex items-center gap-2">
<Input
value={location || (isLocal ? '' : 'Workspace root')}
readOnly
placeholder={isLocal ? 'Select a parent directory...' : 'Workspace root (or browse for subfolder)'}
onDoubleClick={() => setShowPicker(true)}
/>
<Button onClick={() => setShowPicker(true)} className="text-sm">
Browse...
</Button>
</div>
</div>
<div className="mb-4">
<label className="mb-1 block text-sm font-medium">Repository URL</label>
<Input
value={repoUrl}
onChange={(event) => setRepoUrl(event.target.value)}
placeholder="https://github.com/user/repo.git"
aria-label="repository url"
/>
</div>
{!isLocal && (
<div className="mb-4">
<label className="mb-1 block text-sm font-medium">Access Token</label>
<Input
type="password"
value={token}
onChange={(event) => setToken(event.target.value)}
placeholder="Personal access token for private repos"
aria-label="access token"
/>
</div>
)}
{repoName && <p className="text-foreground-muted mb-4 text-xs">Will clone to: {targetPath}</p>}
<div className="flex gap-2">
<Button
onClick={handleClone}
disabled={!repoUrl.trim() || (isLocal && !location)}
className="disabled:text-foreground-muted disabled:cursor-not-allowed disabled:opacity-50"
>
Clone
</Button>
<CloseButton onClick={handleClose} className="absolute top-3 right-3" />
</div>
</div>
</div>
<DirectoryPicker
isOpen={showPicker}
onSelect={(path) => {
setLocation(normalizePath(path))
setShowPicker(false)
}}
onCancel={() => setShowPicker(false)}
initialPath={location}
/>
</>
)
}