-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrepository-selector.tsx
More file actions
516 lines (481 loc) · 19.6 KB
/
Copy pathrepository-selector.tsx
File metadata and controls
516 lines (481 loc) · 19.6 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
"use client"
import type React from "react"
import { useState } from "react"
import { motion, AnimatePresence } from "framer-motion"
import {
Github,
Folder,
Globe,
HardDrive,
CheckCircle,
AlertCircle,
Loader2,
GitBranch,
Calendar,
User,
} from "lucide-react"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
import { REPOSITORY, FILENAMES } from "@/lib/constants"
import { Input } from "@/components/ui/input"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Badge } from "@/components/ui/badge"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { RepositoryInfo } from "@/lib/repository-handler"
interface RepositorySelectorProps {
onRepositorySelect: (info: RepositoryInfo) => void
isLoading: boolean
error: string | null
currentRepository: RepositoryInfo | null
}
export default function RepositorySelector({
onRepositorySelect,
isLoading,
error,
currentRepository,
}: RepositorySelectorProps) {
interface ValidationDetails {
type: "remote" | "local"
platform?: string
url?: string
path?: string
isGitRepo?: boolean
}
const [activeTab, setActiveTab] = useState<"remote" | "local">("remote")
const [remoteUrl, setRemoteUrl] = useState("")
const [localPath, setLocalPath] = useState("")
const [validationStatus, setValidationStatus] = useState<{
isValid: boolean
message: string
details?: ValidationDetails
} | null>(null)
const handleRemoteSubmit = async (e: React.FormEvent) => {
e.preventDefault()
if (!remoteUrl.trim()) {
setValidationStatus({
isValid: false,
message: "Please enter a repository URL",
})
return
}
// Validate URL format
try {
const url = new URL(remoteUrl)
if (
!url.hostname.includes("github.com") &&
!url.hostname.includes("gitlab.com") &&
!url.hostname.includes("bitbucket.org")
) {
setValidationStatus({
isValid: false,
message: "Please enter a valid Git repository URL (GitHub, GitLab, or Bitbucket)",
})
return
}
} catch {
setValidationStatus({
isValid: false,
message: "Please enter a valid URL",
})
return
}
setValidationStatus({
isValid: true,
message: "Repository URL validated successfully",
details: {
type: "remote",
platform: remoteUrl.includes("github.com")
? "GitHub"
: remoteUrl.includes("gitlab.com")
? "GitLab"
: "Bitbucket",
url: remoteUrl,
},
})
const repoInfo: RepositoryInfo = {
type: "remote",
path: remoteUrl,
name: remoteUrl.split("/").pop()?.replace(".git", "") || "Unknown Repository",
}
onRepositorySelect(repoInfo)
}
const handleTryDemo = () => {
const demoRepo: RepositoryInfo = {
type: "remote",
path: REPOSITORY.GITTUF_URL,
name: "gittuf",
}
setRemoteUrl(demoRepo.path)
setValidationStatus({
isValid: true,
message: "Demo repository loaded",
details: {
type: "remote",
platform: "GitHub",
url: demoRepo.path,
},
})
onRepositorySelect(demoRepo)
}
return (
<Card className="bg-white/90 backdrop-blur-sm border-slate-200 shadow-lg" data-testid="repository-selector">
<CardHeader className="pb-4">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-3">
<div className="p-2 bg-blue-100 rounded-lg">
<GitBranch className="h-6 w-6 text-blue-600" />
</div>
<div>
<CardTitle className="text-xl text-slate-800">Repository Selection</CardTitle>
<p className="text-sm text-slate-600 mt-1">
Choose a repository to analyze security metadata and compare commits
</p>
</div>
</div>
{currentRepository && (
<div className="text-right" data-testid="selected-repository">
<Badge variant="outline" className="bg-green-50 text-green-700 border-green-200">
<CheckCircle className="h-3 w-3 mr-1" />
Connected
</Badge>
<p className="text-xs text-slate-500 mt-1">{currentRepository.name}</p>
</div>
)}
</div>
</CardHeader>
<CardContent className="space-y-6">
<Tabs value={activeTab} onValueChange={(value) => setActiveTab(value as "remote" | "local")}>
<TabsList className="grid w-full grid-cols-2">
<TabsTrigger value="remote" className="flex items-center" data-testid="repository-option">
<Globe className="h-4 w-4 mr-2" />
Remote Repository
</TabsTrigger>
<TabsTrigger value="local" className="flex items-center" data-testid="repository-option">
<HardDrive className="h-4 w-4 mr-2" />
Local Repository
</TabsTrigger>
</TabsList>
<TabsContent value="remote" className="space-y-4">
<div className="space-y-4">
<div>
<h3 className="font-medium text-slate-800 mb-2 flex items-center">
<Github className="h-4 w-4 mr-2" />
Remote Repository URL
</h3>
<p className="text-sm text-slate-600 mb-3">
Enter the URL of a Git repository (GitHub, GitLab, or Bitbucket) that contains gittuf security
metadata.
</p>
<form onSubmit={handleRemoteSubmit} className="space-y-3">
<div className="flex gap-2">
<Input
type="url"
placeholder="https://github.com/username/repository"
value={remoteUrl}
onChange={(e) => setRemoteUrl(e.target.value)}
className="flex-grow"
disabled={isLoading}
/>
<Button type="submit" disabled={isLoading || !remoteUrl.trim()} className="min-w-[100px]">
{isLoading ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<>
<Github className="h-4 w-4 mr-2" />
Connect
</>
)}
</Button>
</div>
</form>
<div className="flex items-center justify-between mt-3">
<div className="flex items-center space-x-2 text-xs text-slate-500">
<span>Supported platforms:</span>
<Badge variant="outline" className="text-xs">
GitHub
</Badge>
<Badge variant="outline" className="text-xs">
GitLab
</Badge>
<Badge variant="outline" className="text-xs">
Bitbucket
</Badge>
</div>
<Button
variant="outline"
size="sm"
onClick={handleTryDemo}
disabled={isLoading}
className="text-blue-600 border-blue-200 hover:bg-blue-50 bg-transparent"
data-testid="try-demo-button"
>
Try Demo
</Button>
</div>
</div>
{/* Remote Repository Features */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
<div className="p-3 bg-blue-50 rounded-lg border border-blue-200">
<div className="flex items-center space-x-2 mb-1">
<Globe className="h-4 w-4 text-blue-600" />
<span className="text-sm font-medium text-blue-800">Cloud Access</span>
</div>
<p className="text-xs text-blue-700">Access repositories from anywhere</p>
</div>
<div className="p-3 bg-green-50 rounded-lg border border-green-200">
<div className="flex items-center space-x-2 mb-1">
<GitBranch className="h-4 w-4 text-green-600" />
<span className="text-sm font-medium text-green-800">Full History</span>
</div>
<p className="text-xs text-green-700">Complete commit history available</p>
</div>
<div className="p-3 bg-purple-50 rounded-lg border border-purple-200">
<div className="flex items-center space-x-2 mb-1">
<User className="h-4 w-4 text-purple-600" />
<span className="text-sm font-medium text-purple-800">Collaboration</span>
</div>
<p className="text-xs text-purple-700">Team repository analysis</p>
</div>
</div>
</div>
</TabsContent>
<TabsContent value="local" className="space-y-4">
<div className="space-y-4">
<div>
<h3 className="font-medium text-slate-800 mb-2 flex items-center">
<Folder className="h-4 w-4 mr-2" />
Local Repository Folder
</h3>
<p className="text-sm text-slate-600 mb-3">
Select a local Git repository folder from your computer that contains gittuf security metadata files.
</p>
<div className="space-y-3">
<div className="flex items-center gap-3">
{/* <Button
onClick={handleLocalSelect}
disabled={isLoading}
className="min-w-[140px] bg-transparent"
variant="outline"
>
{isLoading ? (
<Loader2 className="h-4 w-4 animate-spin mr-2" />
) : (
<Folder className="h-4 w-4 mr-2" />
)}
Select Folder
</Button> */}
<form
onSubmit={(e) => {
e.preventDefault()
if (!localPath.trim()) {
setValidationStatus({
isValid: false,
message: "Please enter a local repository path",
})
return
}
// Very basic validation – you can expand this as needed
const isGitRepo = true // Optionally, check for .git folder existence via IPC/electron/native bridge in a real app
setValidationStatus({
isValid: true,
message: isGitRepo
? "Local repository path accepted"
: "Path entered (Git repo not verified, but accepted)",
details: {
type: "local",
path: localPath,
isGitRepo,
},
})
const repoInfo: RepositoryInfo = {
type: "local",
path: localPath,
name: localPath.split("/").pop() || "Local Repo",
}
onRepositorySelect(repoInfo)
}}
className="space-y-3 w-full"
>
<div className="flex gap-2">
<Input
type="text"
placeholder="/path/to/local/repo"
value={localPath}
onChange={(e) => setLocalPath(e.target.value)}
disabled={isLoading}
className="flex-grow"
/>
<Button type="submit" disabled={isLoading || !localPath.trim()} className="min-w-[100px]">
{isLoading ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<>
<HardDrive className="h-4 w-4 mr-2" />
Connect
</>
)}
</Button>
</div>
</form>
{localPath && (
<div className="flex-1">
<p className="text-sm font-medium text-slate-800">Selected:</p>
<p className="text-xs text-slate-600 font-mono bg-slate-100 px-2 py-1 rounded">{localPath}</p>
</div>
)}
</div>
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<Alert className="bg-amber-50 border-amber-200">
<AlertCircle className="h-4 w-4 text-amber-600" />
<AlertDescription className="text-amber-800">
<strong>Browser Compatibility:</strong> Local folder selection requires a modern browser
(Chrome 86+, Edge 86+) with File System Access API support.
</AlertDescription>
</Alert>
</TooltipTrigger>
<TooltipContent side="bottom" className="max-w-md">
<div className="space-y-2">
<p className="font-medium">Supported Browsers:</p>
<ul className="text-sm space-y-1">
<li>• Chrome 86+ ✅</li>
<li>• Microsoft Edge 86+ ✅</li>
<li>• Opera 72+ ✅</li>
<li>• Firefox ❌ (not supported)</li>
<li>• Safari ❌ (not supported)</li>
</ul>
</div>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
</div>
{/* Local Repository Features */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
<div className="p-3 bg-green-50 rounded-lg border border-green-200">
<div className="flex items-center space-x-2 mb-1">
<HardDrive className="h-4 w-4 text-green-600" />
<span className="text-sm font-medium text-green-800">Offline Access</span>
</div>
<p className="text-xs text-green-700">Work without internet connection</p>
</div>
<div className="p-3 bg-blue-50 rounded-lg border border-blue-200">
<div className="flex items-center space-x-2 mb-1">
<CheckCircle className="h-4 w-4 text-blue-600" />
<span className="text-sm font-medium text-blue-800">Privacy</span>
</div>
<p className="text-xs text-blue-700">Data stays on your computer</p>
</div>
<div className="p-3 bg-purple-50 rounded-lg border border-purple-200">
<div className="flex items-center space-x-2 mb-1">
<Calendar className="h-4 w-4 text-purple-600" />
<span className="text-sm font-medium text-purple-800">Real-time</span>
</div>
<p className="text-xs text-purple-700">Analyze latest changes instantly</p>
</div>
</div>
</div>
</TabsContent>
</Tabs>
{/* Validation Status */}
<AnimatePresence>
{validationStatus && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.3 }}
>
<Alert className={validationStatus.isValid ? "bg-green-50 border-green-200" : "bg-red-50 border-red-200"}>
{validationStatus.isValid ? (
<CheckCircle className="h-4 w-4 text-green-600" />
) : (
<AlertCircle className="h-4 w-4 text-red-600" />
)}
<AlertDescription className={validationStatus.isValid ? "text-green-800" : "text-red-800"}>
{validationStatus.message}
{validationStatus.details && (
<div className="mt-2 text-xs">
{validationStatus.details.type === "remote" && (
<div className="flex items-center space-x-2">
<Badge variant="outline" className="bg-white">
{validationStatus.details.platform}
</Badge>
<span className="font-mono">{validationStatus.details.url}</span>
</div>
)}
{validationStatus.details.type === "local" && (
<div className="space-y-1">
<div className="flex items-center space-x-2">
<span>Path:</span>
<span className="font-mono bg-white px-1 rounded">{validationStatus.details.path}</span>
</div>
{validationStatus.details.isGitRepo !== undefined && (
<div className="flex items-center space-x-2">
<span>Git Repository:</span>
<Badge
variant="outline"
className={
validationStatus.details.isGitRepo
? "bg-green-100 text-green-700"
: "bg-yellow-100 text-yellow-700"
}
>
{validationStatus.details.isGitRepo ? "Detected" : "Not Detected"}
</Badge>
</div>
)}
</div>
)}
</div>
)}
</AlertDescription>
</Alert>
</motion.div>
)}
</AnimatePresence>
{/* Error Display */}
<AnimatePresence>
{error && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.3 }}
>
<Alert className="bg-red-50 border-red-200">
<AlertCircle className="h-4 w-4 text-red-600" />
<AlertDescription className="text-red-800">
<strong>Error:</strong> {error}
</AlertDescription>
</Alert>
</motion.div>
)}
</AnimatePresence>
{/* Repository Requirements */}
<div className="bg-slate-50 p-4 rounded-lg border border-slate-200">
<h4 className="font-medium text-slate-800 mb-2">Repository Requirements</h4>
<ul className="text-sm text-slate-600 space-y-1">
<li className="flex items-center space-x-2">
<CheckCircle className="h-3 w-3 text-green-500" />
<span>
Contains <code className="bg-white px-1 rounded">{FILENAMES.ROOT}</code> and/or{" "}
<code className="bg-white px-1 rounded">{FILENAMES.TARGETS}</code> files
</span>
</li>
<li className="flex items-center space-x-2">
<CheckCircle className="h-3 w-3 text-green-500" />
<span>Has commit history with gittuf metadata changes</span>
</li>
<li className="flex items-center space-x-2">
<CheckCircle className="h-3 w-3 text-green-500" />
<span>Accessible via Git protocol (for remote) or local file system</span>
</li>
</ul>
</div>
</CardContent>
</Card>
)
}