-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateProjectForm.tsx
573 lines (544 loc) · 29.2 KB
/
CreateProjectForm.tsx
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
'use client'
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue } from "@/components/ui/select";
import { createProjectAction } from "@/data/user/projects";
import { useSAToastMutation } from "@/hooks/useSAToastMutation";
import { generateSlug } from "@/lib/utils";
import { zodResolver } from "@hookform/resolvers/zod";
import { GitHubLogoIcon } from "@radix-ui/react-icons";
import { motion } from "framer-motion";
import { AlertCircle, Briefcase, Github, Users } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { FormEvent, useState } from 'react';
import { Controller, useForm } from "react-hook-form";
import { z } from "zod";
import { InputTags } from "./InputTags";
import { T } from "./ui/Typography";
import { Switch } from "./ui/switch";
const MotionCard = motion(Card);
const createProjectFormSchema = z.object({
name: z.string().min(1, "Project name is required"),
repository: z.bigint().positive("Please select a repository"),
terraformDir: z.string().min(1, "Terraform working directory is required"),
iac_type: z.enum(["terraform", "terragrunt", "opentofu"]).default("terraform"),
workspace: z.string().default("default").optional(),
workflow_file: z.string().default("digger_workflow.yml").optional(),
include_patterns: z.string().optional(),
exclude_patterns: z.string().optional(),
branch: z.string().min(1, "Branch is required").default("main"),
labels: z.array(z.string()),
managedState: z.boolean().default(true),
teamId: z.number().int().positive().nullable(),
is_drift_detection_enabled: z.boolean().default(false),
drift_crontab: z.string().optional(),
});
type CreateProjectFormData = z.infer<typeof createProjectFormSchema>;
type Repository = {
id: bigint;
repo_full_name: string | null;
};
type Team = {
id: number;
name: string;
};
type CreateProjectFormProps = {
organizationId: string;
repositories: Repository[];
teams: Team[];
teamId: number | undefined;
};
export default function CreateProjectForm({ organizationId, repositories, teams, teamId }: CreateProjectFormProps) {
const router = useRouter();
const githubAppSlug = process.env.NEXT_PUBLIC_GITHUB_APP_SLUG;
const { control, handleSubmit, watch, setValue, formState: { errors } } = useForm<CreateProjectFormData>({
resolver: zodResolver(createProjectFormSchema),
defaultValues: {
name: "",
repository: repositories[0]?.id || BigInt(0),
terraformDir: "",
iac_type: "terraform",
workflow_file: "digger_workflow.yml",
workspace: "default",
managedState: true,
labels: [],
teamId: teamId || null,
is_drift_detection_enabled: false,
drift_crontab: '',
},
});
const createProjectMutation = useSAToastMutation(
async (data: CreateProjectFormData) => {
const slug = generateSlug(data.name);
return await createProjectAction({
name: data.name,
slug,
repoId: data.repository,
branch: data.branch,
organizationId: organizationId,
teamId: data.teamId,
terraformWorkingDir: data.terraformDir,
iac_type: data.iac_type,
workspace: data.workspace,
workflow_file: data.workflow_file,
include_patterns: data.include_patterns,
exclude_patterns: data.exclude_patterns,
labels: data.labels,
managedState: data.managedState,
is_drift_detection_enabled: data.is_drift_detection_enabled,
drift_crontab: data.drift_crontab,
});
},
{
loadingMessage: "Creating project...",
successMessage: "Project created!",
errorMessage: "Failed to create project",
onSuccess: (response) => {
if (response.status === "success" && response.data) {
router.push(`/project/${response.data.slug}`);
}
}
},
)
// isSubmitting is used to disable the submit button while the form is being submitted
const [isSubmitting, setIsSubmitting] = useState(false);
const onSubmit = (data: CreateProjectFormData) => {
setIsSubmitting(true);
createProjectMutation.mutate(data);
};
const handleFormSubmit = (e: FormEvent) => {
e.preventDefault();
handleSubmit(onSubmit)();
};
return (
<div className="p-6 max-w-4xl mx-auto">
<form onSubmit={handleFormSubmit}>
<div className="mb-6 flex justify-start items-center">
<div>
<T.H3>Create new Project</T.H3>
<T.P className="text-muted-foreground">Create a new project within your organization.</T.P>
</div>
</div>
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.1 }}
>
<CardHeader>
<div className="flex flex-col">
<CardTitle className="text-lg ">Project Details</CardTitle>
<CardDescription className="text-sm text-muted-foreground">Provide the name of your project</CardDescription>
</div>
</CardHeader>
<CardContent >
<div>
<Label htmlFor="name">Project Name *</Label>
<Controller
name="name"
control={control}
render={({ field }) => (
<div className="relative">
<Input
id="name"
placeholder="Enter project name"
className={`mt-1 ${errors.name ? 'border-destructive' : ''}`}
{...field}
/>
{errors.name && (
<div className="flex items-center mt-1 text-destructive">
<AlertCircle className="h-4 w-4 mr-1" />
<span className="text-sm">{errors.name.message}</span>
</div>
)}
</div>
)}
/>
</div>
</CardContent>
</MotionCard>
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
>
<CardHeader className="flex flex-row justify-between items-center w-full">
<div className="flex flex-col">
<CardTitle className="text-lg">Select a repository</CardTitle>
<CardDescription className="text-sm text-muted-foreground">Choose the repository for your project</CardDescription>
</div>
<Link
href={`https://github.com/apps/${githubAppSlug}/installations/select_target?organization_id=${organizationId}`}
onClick={(e) => e.stopPropagation()}
target="_blank"
rel="noopener noreferrer"
>
<Button
type="button"
variant='secondary'
size='sm'
className="gap-1"
onClick={(e) => e.stopPropagation()}
>
<GitHubLogoIcon className="h-4 w-4 mr-1" />
Configure Github
</Button>
</Link>
</CardHeader>
<CardContent>
{repositories.length > 0 ? (
<Controller
name="repository"
control={control}
render={({ field }) => (
<div className="relative">
<Select onValueChange={(value) => field.onChange(BigInt(value))} value={field.value.toString()}>
<SelectTrigger className={`w-full ${errors.repository ? 'border-destructive' : ''}`}>
<SelectValue placeholder="Select a repository" />
</SelectTrigger>
<SelectContent>
{repositories.map((repo) => (
<SelectItem key={repo.id} value={repo.id.toString()}>
<div className="flex items-center">
<Github className="mr-2 h-4 w-4" />
<span>{repo.repo_full_name}</span>
</div>
</SelectItem>
))}
</SelectContent>
</Select>
{errors.repository && (
<div className="flex items-center mt-1 text-destructive">
<AlertCircle className="h-4 w-4 mr-1" />
<span className="text-sm">{errors.repository.message}</span>
</div>
)}
</div>
)}
/>
) : (
<div className="text-center py-8">
<div className="bg-muted/50 rounded-full p-4 inline-block">
<Github className="mx-auto size-8 text-muted-foreground" />
</div>
<T.H4 className="mb-1 mt-4">No Repositories Found</T.H4>
<T.P className="text-muted-foreground mb-4">
It looks like there are no repositories.
</T.P>
</div>
)}
</CardContent>
</MotionCard>
{teams.length !== 0 && (
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
>
<CardHeader className="flex flex-col space-y-0">
<CardTitle className="text-lg mb-0">Select a Team</CardTitle>
<CardDescription className="text-sm text-muted-foreground mt-0">Choose the team for your project or create it at the organization level</CardDescription>
</CardHeader>
<CardContent>
<Controller
name="teamId"
control={control}
render={({ field }) => (
<div className="relative">
<Select onValueChange={(value) => {
if (value === 'null') {
field.onChange(null);
} else {
field.onChange(parseInt(value));
}
}} value={field.value?.toString() || "null"}
>
<SelectTrigger className={`w-full ${errors.teamId ? 'border-destructive' : ''}`}>
<SelectValue placeholder="Select a team" />
</SelectTrigger>
<SelectContent>
<SelectItem value="null">
<div className="flex items-center">
<Briefcase className="mr-2 h-4 w-4" />
<span>Create at organization level</span>
</div>
</SelectItem>
<SelectSeparator />
<SelectGroup>
<SelectLabel className='ml-0'>My teams</SelectLabel>
{teams.map((team) => (
<SelectItem key={team.id} value={team.id.toString()}>
<div className="flex items-center">
<Users className="mr-2 h-4 w-4" />
<span>{team.name}</span>
</div>
</SelectItem>
))}
</SelectGroup>
</SelectContent>
</Select>
{errors.teamId && (
<div className="flex items-center mt-1 text-destructive">
<AlertCircle className="h-4 w-4 mr-1" />
<span className="text-sm">{errors.teamId.message}</span>
</div>
)}
</div>
)}
/>
</CardContent>
</MotionCard>
)}
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.3 }}
>
<CardHeader>
<div className="flex flex-col">
<CardTitle className="text-lg ">Configuration</CardTitle>
<CardDescription className="text-sm text-muted-foreground">Specify key settings for Terraform</CardDescription>
</div>
</CardHeader>
<CardContent>
<div className="space-y-4">
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="terraformDir">Terraform Working Directory *</Label>
<Controller
name="terraformDir"
control={control}
render={({ field }) => (
<div className="relative">
<Input
id="terraformDir"
placeholder="e.g. ./"
className={`mt-1 ${errors.terraformDir ? 'border-destructive' : ''}`}
{...field}
/>
{errors.terraformDir && (
<div className="flex items-center mt-1 text-destructive">
<AlertCircle className="h-4 w-4 mr-1" />
<span className="text-sm">{errors.terraformDir.message}</span>
</div>
)}
</div>
)}
/>
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="branch">Branch</Label>
<Controller
name="branch"
control={control}
render={({ field }) => (
<div className="relative">
<Input
id="branch"
placeholder="if not specified, main branch will be used"
className={`mt-1 ${errors.branch ? 'border-destructive' : ''}`}
{...field}
/>
{errors.branch && (
<div className="flex items-center mt-1 text-destructive">
<AlertCircle className="h-4 w-4 mr-1" />
<span className="text-sm">{errors.branch.message}</span>
</div>
)}
</div>
)}
/>
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="iac_type">IAC type</Label>
<Controller
name="iac_type"
control={control}
render={({ field }) => (
<Select onValueChange={field.onChange} value={field.value}>
<SelectTrigger>
<SelectValue placeholder="Select IAC type" />
</SelectTrigger>
<SelectContent className="rounded-xl">
<SelectItem value="terraform" className="rounded-lg">
Terraform
</SelectItem>
<SelectItem value="terragrunt" className="rounded-lg">
Terragrunt
</SelectItem>
<SelectItem value="opentofu" className="rounded-lg">
Opentofu
</SelectItem>
</SelectContent>
</Select>
)}
/>
</motion.div>
</div>
</CardContent>
</MotionCard>
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.4 }}
>
<CardHeader>
<div className="flex flex-col">
<CardTitle className="text-lg ">Additional Settings</CardTitle>
<CardDescription className="text-sm text-muted-foreground">Configure additional project settings</CardDescription>
</div>
</CardHeader>
<CardContent>
<div className="space-y-4">
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="workspace">Workspace</Label>
<Controller
name="workspace"
control={control}
render={({ field }) => (
<Input id="workspace" {...field} />
)}
/>
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="workflow_file">Workflow file</Label>
<Controller
name="workflow_file"
control={control}
render={({ field }) => (
<Input id="workflow_file" {...field} />
)}
/>
</motion.div>
<div className="grid grid-cols-2 gap-6">
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="include_patterns">Include patterns</Label>
<Controller
name="include_patterns"
control={control}
render={({ field }) => (
<Input id="include_patterns" {...field} />
)}
/>
</motion.div>
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.15, delay: 0.4 }}
>
<Label htmlFor="Exclude patterns">Exclude patterns</Label>
<Controller
name="exclude_patterns"
control={control}
render={({ field }) => (
<Input id="exclude_patterns" {...field} />
)}
/>
</motion.div>
</div>
<div>
<Label htmlFor="labels">Labels</Label>
<Controller
name="labels"
control={control}
render={({ field }) => (
<InputTags
id="labels"
value={field.value}
onChange={field.onChange}
placeholder="Add labels"
className="mt-1"
/>
)}
/>
</div>
</div>
</CardContent>
</MotionCard>
<MotionCard
className="mb-6"
initial={{ opacity: 0, y: 20 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.5 }}
>
<CardHeader>
<div className="flex flex-col">
<CardTitle className="text-lg">Drift Detection</CardTitle>
<CardDescription className="text-sm text-muted-foreground">Configure drift detection settings</CardDescription>
</div>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="flex items-center space-x-2">
<Controller
name="is_drift_detection_enabled"
control={control}
render={({ field }) => (
<Switch
checked={field.value}
onCheckedChange={field.onChange}
id="is_drift_detection_enabled"
/>
)}
/>
<Label htmlFor="is_drift_detection_enabled">Enable Drift Detection</Label>
</div>
{watch('is_drift_detection_enabled') && (
<div>
<Label htmlFor="drift_crontab">Drift Detection Schedule (Crontab)</Label>
<Controller
name="drift_crontab"
control={control}
render={({ field }) => (
<Input
id="drift_crontab"
placeholder="Enter crontab schedule (e.g., 0 0 * * *)"
{...field}
/>
)}
/>
</div>
)}
</div>
</CardContent>
</MotionCard>
<div className="flex justify-end w-full gap-3 mt-6">
<Button type="button" variant="outline" onClick={() => router.back()}>Cancel</Button>
<Button type="submit" disabled={isSubmitting || createProjectMutation.isLoading}>
{isSubmitting || createProjectMutation.isLoading ? "Creating..." : "Create Project"}
</Button>
</div>
</form>
</div>
);
}