-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
page.tsx
390 lines (365 loc) · 13.8 KB
/
page.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
'use client'
import { useCallback, useEffect, useMemo, useState } from 'react'
import type {
CreateProjectRequest,
GetAllProjectsResponse,
ProjectWithCount,
Workspace
} from '@keyshade/schema'
import { AddSVG } from '@public/svg/shared'
import { FolderSVG } from '@public/svg/dashboard'
import ProjectCard from '@/components/dashboard/projectCard'
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle
} from '@/components/ui/sheet'
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue
} from '@/components/ui/select'
import { Switch } from '@/components/ui/switch'
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTrigger
} from '@/components/ui/dialog'
import ControllerInstance from '@/lib/controller-instance'
export default function Index(): JSX.Element {
const [isSheetOpen, setIsSheetOpen] = useState<boolean>(false)
const [isProjectEmpty, setIsProjectEmpty] = useState<boolean>(true)
const [isDialogOpen, setIsDialogOpen] = useState<boolean>(false)
// Projects to be displayed in the dashboard
const [projects, setProjects] = useState<ProjectWithCount[]>([])
// Contains the data for the new project
const [newProjectData, setNewProjectData] = useState<CreateProjectRequest>({
name: '',
workspaceSlug: '',
description: '',
storePrivateKey: false,
environments: [
{
name: '',
description: ''
}
],
accessLevel: 'GLOBAL'
})
// Fetches the currently selected workspace from context
const currentWorkspace: Workspace | null = useMemo(
() =>
typeof localStorage !== 'undefined'
? (JSON.parse(
localStorage.getItem('currentWorkspace') ?? '{}'
) as Workspace)
: null,
[]
)
// If a workspace is selected, we want to fetch all the projects
// under that workspace and display it in the dashboard.
useEffect(() => {
async function getAllProjects() {
if (currentWorkspace) {
const { success, error, data } =
await ControllerInstance.getInstance().projectController.getAllProjects(
{ workspaceSlug: currentWorkspace.slug },
{}
)
if (success && data) {
setProjects(data.items)
} else {
// eslint-disable-next-line no-console -- we need to log the error
console.error(error)
}
}
}
getAllProjects()
}, [currentWorkspace])
// Check if the projects array is empty
useEffect(() => setIsProjectEmpty(projects.length === 0), [projects])
// Function to create a new project
const createNewProject = useCallback(async () => {
if (currentWorkspace) {
newProjectData.workspaceSlug = currentWorkspace.slug
const { data, error, success } =
await ControllerInstance.getInstance().projectController.createProject(
newProjectData,
{}
)
if (success && data) {
setProjects([
...projects,
{
...data,
environmentCount: newProjectData.environments
? newProjectData.environments.length
: 0,
secretCount: 0,
variableCount: 0
}
])
} else {
// eslint-disable-next-line no-console -- we need to log the error
console.error(error)
}
setIsDialogOpen(false)
} else {
throw new Error('No workspace selected')
}
}, [currentWorkspace, newProjectData, projects])
const toggleDialog = useCallback(() => setIsDialogOpen((prev) => !prev), [])
return (
<div className="flex flex-col gap-4">
<div className="flex items-center justify-between">
{!isProjectEmpty && (
<h1 className="text-[1.75rem] font-semibold ">My Projects</h1>
)}
<Dialog onOpenChange={setIsDialogOpen} open={isDialogOpen}>
<DialogTrigger>
<Button onClick={toggleDialog}>
{' '}
<AddSVG /> Create a new Project
</Button>
</DialogTrigger>
<DialogContent className="h-[39.5rem] w-[28.625rem] rounded-[12px] border bg-[#1E1E1F] ">
<div className="flex h-[3.125rem] w-[25.625rem] flex-col items-start justify-center">
<DialogHeader className=" font-geist h-[1.875rem] w-[8.5rem] text-[1.125rem] font-semibold text-white ">
Create Projects
</DialogHeader>
<DialogDescription className=" font-inter h-[1.25rem] w-[25.625rem] text-[0.875rem] font-normal text-[#D4D4D4]">
Create your new project
</DialogDescription>
</div>
<div className="flex flex-col gap-y-8">
<div className="flex h-[29.125rem] w-[25.813rem] flex-col gap-[1rem] py-[1rem] ">
{/* NAME */}
<div className="flex h-[2.25rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<Label
className="font-geist h-[1.25rem] w-[4.813rem] gap-[0.25rem] text-left text-[0.875rem] font-[500] "
htmlFor="name"
>
Name
</Label>
<Input
className="col-span-3 h-[2.25rem] w-[20rem] "
id="name"
onChange={(e) => {
setNewProjectData((prev) => ({
...prev,
name: e.target.value
}))
}}
placeholder="Enter the name"
/>
</div>
{/* DESCRIPTION */}
<div className="flex h-[5.625rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<Label
className="font-geist h-[1.25rem] w-[4.813rem] gap-[0.25rem] text-left text-[0.875rem] font-[500] "
htmlFor="name"
>
Description
</Label>
<Input
className="col-span-3 h-[5.625rem] w-[20rem] gap-[0.25rem]"
id="name"
onChange={(e) => {
setNewProjectData((prev) => ({
...prev,
description: e.target.value
}))
}}
placeholder="Enter the name"
/>
</div>
{/* ENV. NAME */}
<div className="flex h-[2.25rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<Label
className="font-geist h-[1.25rem] w-[4.813rem] gap-[0.25rem] text-left text-[0.875rem] font-[500] "
htmlFor="envName"
>
Env. Name
</Label>
<Input
className="col-span-3 h-[2.25rem] w-[20rem] "
id="envName"
onChange={(e) => {
setNewProjectData((prev) => ({
...prev,
envName: e.target.value
}))
}}
placeholder="Your project default environment name"
/>
</div>
{/* ENV. DESCRIPTION */}
<div className="flex h-[4.875rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<Label
className="font-geist h-[1.25rem] w-[4.813rem] gap-[0.25rem] text-left text-[0.875rem] font-[500]"
htmlFor="envDescription"
>
Env. Description
</Label>
<Input
className="col-span-3 h-[4.875rem] w-[20rem] "
id="envDescription"
onChange={(e) => {
setNewProjectData((prev) => ({
...prev,
envDescription: e.target.value
}))
}}
placeholder="Detailed description about your environment"
/>
</div>
{/* ACCESS LEVEL */}
<div className="flex h-[2.25rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<Label
className="font-geist h-[0.875rem] w-[5.5rem] gap-[0.25rem] text-left text-[0.875rem] font-[500] "
htmlFor="accessLevel"
>
Access Level
</Label>
<Select
defaultValue="GLOBAL"
onValueChange={(currValue) => {
setNewProjectData((prevData) => ({
...prevData,
accessLevel: currValue as
| 'GLOBAL'
| 'INTERNAL'
| 'PRIVATE'
}))
}}
>
<SelectTrigger className=" h-[2.25rem] w-[20rem] rounded-[0.375rem] border-[0.013rem] border-white/10 focus:border-[#3b82f6]">
<SelectValue />
</SelectTrigger>
<SelectContent className="border-[0.013rem] border-white/10 bg-neutral-800 text-white ">
<SelectGroup>
{['GLOBAL', 'INTERNAL', 'PRIVATE'].map(
(accessValue) => (
<SelectItem
className="group cursor-pointer rounded-sm"
key={accessValue.toUpperCase()}
value={accessValue.toUpperCase()}
>
{accessValue}
</SelectItem>
)
)}
</SelectGroup>
</SelectContent>
</Select>
</div>
<div className="flex h-[4.875rem] w-[25.813rem] items-center justify-center gap-[1rem]">
<div className="flex h-[2.875rem] w-[22.563rem] flex-col items-start justify-center">
<h1 className="font-geist h-[1.5rem] w-[18.688rem] text-[1rem] font-[500]">
Should the private key be saved or not?
</h1>
<h1 className="font-inter h-[1.25rem] w-[16.563rem] text-[0.8rem] font-normal text-[#A1A1AA] ">
Choose if you want to save your private key
</h1>
</div>
<div className="p-[0.125rem]">
<Switch className="h-[1.25rem] w-[2.25rem] " />
</div>
</div>
</div>
</div>
<div className="flex h-[2.25rem] w-[25.625rem] justify-end">
<Button
className="font-inter h-[2.25rem] w-[8rem] rounded-[0.375rem] text-[0.875rem] font-[500]"
onClick={createNewProject}
variant="secondary"
>
Create project
</Button>
</div>
</DialogContent>
</Dialog>
</div>
{!isProjectEmpty ? (
<div className="grid grid-cols-1 gap-5 overflow-y-scroll scroll-smooth p-2 md:grid-cols-2 xl:grid-cols-3">
{projects.map((project: GetAllProjectsResponse['items'][number]) => {
return (
<ProjectCard
key={project.id}
project={project}
setIsSheetOpen={setIsSheetOpen}
/>
)
})}
</div>
) : (
<div className="mt-[10vh] flex h-[40vh] flex-col items-center justify-center gap-y-4">
<FolderSVG width="150" />
<div className="text-4xl">Start your First Project</div>
<div>
Create a file and start setting up your environment and secret keys
</div>
<Button variant="secondary">Create project</Button>
</div>
)}
<Sheet
onOpenChange={(open) => {
setIsSheetOpen(open)
}}
open={isSheetOpen}
>
<SheetContent className="border-white/15 bg-[#222425]">
<SheetHeader>
<SheetTitle className="text-white">Edit Project</SheetTitle>
<SheetDescription>
Make changes to the project details
</SheetDescription>
</SheetHeader>
<div className="grid gap-4 py-4">
<div className="flex flex-col items-start gap-4">
<Label className="text-right" htmlFor="name">
Project Name
</Label>
<Input className="col-span-3" id="name" />
</div>
<div className="flex flex-col items-start gap-4">
<Label className="text-right" htmlFor="name">
Project description
</Label>
<Input className="col-span-3" id="name" />
</div>
<div className="flex items-center justify-between">
<Label className="w-[10rem] text-left" htmlFor="name">
Do you want us to store the private key?
</Label>
<div className="flex gap-1 text-sm">
<div>No</div>
<Switch />
<div>Yes</div>
</div>
</div>
</div>
<SheetFooter>
<SheetClose asChild>
<Button type="submit" variant="secondary">
Save changes
</Button>
</SheetClose>
</SheetFooter>
</SheetContent>
</Sheet>
</div>
)
}