| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { FolderOpen, Plus |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | import type { z |
| 9 | import { Button } |
| 10 | import { |
| 11 | Empty, |
| 12 | EmptyContent, |
| 13 | EmptyDescription, |
| 14 | EmptyHeader, |
| 15 | EmptyMedia, |
| 16 | EmptyTitle, |
| 17 | } from '@/components/ui/empty' |
| 18 | import { analyticsService } |
| 19 | import CreateProjectDialog from './_components/create-project-dialog |
| 20 | import ProjectCard from './_components/project-card |
| 21 | import { createProjectAction } |
| 22 | import type { createProjectSchema |
| 23 | import type { ProjectsClientProps |
| 24 | |
| 25 | export function ProjectsClient({ |
| 26 | projects, |
| 27 | organizationId, |
| 28 | orgSlug, |
| 29 | canCreate, |
| 30 | openNewProjectDialog, |
| 31 | }: ProjectsClientProps) |
| 32 | const [dialogOpen, |
| 33 | (openNewProjectDialog && canCreate |
| 34 | ) |
| 35 | const router = |
| 36 | const { execute, |
| 37 | onSuccess() { |
| 38 | toast.success('Project created |
| 39 | setDialogOpen(false) |
| 40 | analyticsService.track('project_create |
| 41 | router.refresh() |
| 42 | }, |
| 43 | onError({ error |
| 44 | if (error.validationErrors |
| 45 | toast.error('Please check your input and try again. |
| 46 | } else { |
| 47 | toast.error( |
| 48 | error.serverError ?? |
| 49 | 'Failed to create project. Slug may already be taken.' |
| 50 | ) |
| 51 | } |
| 52 | }, |
| 53 | }) |
| 54 | |
| 55 | const handleSubmit = |
| 56 | execute(data) |
| 57 | } |
| 58 | |
| 59 | return ( |
| 60 | <div className='w-full |
| 61 | <div className='mb-6 flex items-center justify-between |
| 62 | <h1 className='font-semibold text-2xl |
| 63 | {canCreate && projects |
| 64 | <Button kbd=' |
| 65 | <Plus className=' |
| 66 | New Project |
| 67 | </Button> |
| 68 | )} |
| 69 | </div> |
| 70 | |
| 71 | {projects.length === |
| 72 | <Empty> |
| 73 | <EmptyHeader> |
| 74 | <EmptyMedia variant=' |
| 75 | <FolderOpen /> |
| 76 | </EmptyMedia> |
| 77 | <EmptyTitle>No projects yet</EmptyTitle |
| 78 | <EmptyDescription> |
| 79 | Create your first project to get started. |
| 80 | </EmptyDescription> |
| 81 | </EmptyHeader> |
| 82 | <EmptyContent> |
| 83 | <Button kbd=' |
| 84 | Create Project |
| 85 | </Button> |
| 86 | </EmptyContent> |
| 87 | </Empty> |
| 88 | ) : ( |
| 89 | <div className='grid gap-4 sm:grid-cols-2 lg:grid-cols-3 |
| 90 | {projects.map(( |
| 91 | <ProjectCard key={ |
| 92 | ))} |
| 93 | </div> |
| 94 | )} |
| 95 | <CreateProjectDialog |
| 96 | dialogOpen={dialogOpen} |
| 97 | handleSubmit={handleSubmit} |
| 98 | isPending={isPending} |
| 99 | organizationId={organizationId} |
| 100 | orgSlug={orgSlug} |
| 101 | setDialogOpen={setDialogOpen} |
| 102 | /> |
| 103 | </div> |
| 104 | ) |
| 105 | } |