|
| 1 | +import { InteractionContextType, useInteractionContext } from '@lib/contexts/interaction'; |
| 2 | +import { routePath } from '@lib/routes/route-paths'; |
| 3 | +import db from '@lib/storage/db'; |
| 4 | +import { BorderedCard } from '@shared/cards/BorderedCard'; |
| 5 | +import { CardItem } from '@shared/cards/CardItem'; |
| 6 | +import ContextMenuWithTrigger from '@shared/ContextMenuWithTrigger'; |
| 7 | +import { Job, Project } from '@typedefs/deployment'; |
| 8 | +import { useLiveQuery } from 'dexie-react-hooks'; |
| 9 | +import { toast } from 'react-hot-toast'; |
| 10 | +import { RiDeleteBinLine } from 'react-icons/ri'; |
| 11 | +import { Link } from 'react-router-dom'; |
| 12 | + |
| 13 | +export default function DraftCard({ project }: { project: Project }) { |
| 14 | + const confirm = useInteractionContext() as InteractionContextType; |
| 15 | + |
| 16 | + const jobs: Job[] | undefined = useLiveQuery(() => db.jobs.where('projectId').equals(project.id).toArray(), [project]); |
| 17 | + |
| 18 | + const onDeleteProject = async () => { |
| 19 | + try { |
| 20 | + const confirmed = await confirm( |
| 21 | + <div className="col gap-3"> |
| 22 | + <div>Are you sure you want to delete the following project draft?</div> |
| 23 | + |
| 24 | + <div className="row gap-2"> |
| 25 | + <div className="mt-[1px] h-2.5 w-2.5 rounded-full" style={{ backgroundColor: project.color }}></div> |
| 26 | + <div className="font-medium">{project.name}</div> |
| 27 | + </div> |
| 28 | + </div>, |
| 29 | + ); |
| 30 | + |
| 31 | + if (!confirmed) return; |
| 32 | + |
| 33 | + await db.projects.delete(project.id); |
| 34 | + toast.success('Project draft deleted successfully.'); |
| 35 | + } catch (error) { |
| 36 | + console.error('Error deleting project draft:', error); |
| 37 | + toast.error('Failed to delete project draft.'); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Link to={`${routePath.deeploys}/${routePath.project}/${project.id}`}> |
| 43 | + <BorderedCard isHoverable> |
| 44 | + <div className="row justify-between gap-3 lg:gap-6"> |
| 45 | + <div className="min-w-[82px]"> |
| 46 | + <CardItem label="ID" value={<>#{project.id}</>} isBold /> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div className="min-w-[212px]"> |
| 50 | + <CardItem |
| 51 | + label="Name" |
| 52 | + value={ |
| 53 | + <div className="row gap-2"> |
| 54 | + <div |
| 55 | + className="mt-[1px] h-2.5 w-2.5 rounded-full" |
| 56 | + style={{ backgroundColor: project.color }} |
| 57 | + ></div> |
| 58 | + <div>{project.name}</div> |
| 59 | + </div> |
| 60 | + } |
| 61 | + isBold |
| 62 | + /> |
| 63 | + </div> |
| 64 | + |
| 65 | + <div className="min-w-[90px]"> |
| 66 | + <CardItem label="Jobs" value={<>{jobs?.length ?? 0}</>} /> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div className="min-w-[212px]"> |
| 70 | + <CardItem |
| 71 | + label="Created" |
| 72 | + value={ |
| 73 | + <> |
| 74 | + {new Date(project.createdAt).toLocaleString('en-US', { |
| 75 | + month: 'short', |
| 76 | + day: 'numeric', |
| 77 | + year: 'numeric', |
| 78 | + hour: 'numeric', |
| 79 | + minute: 'numeric', |
| 80 | + })} |
| 81 | + </> |
| 82 | + } |
| 83 | + /> |
| 84 | + </div> |
| 85 | + |
| 86 | + <ContextMenuWithTrigger |
| 87 | + items={[ |
| 88 | + { |
| 89 | + key: 'delete', |
| 90 | + label: 'Delete', |
| 91 | + description: 'Removes the draft from storage', |
| 92 | + icon: <RiDeleteBinLine />, |
| 93 | + onPress: () => onDeleteProject(), |
| 94 | + }, |
| 95 | + ]} |
| 96 | + /> |
| 97 | + </div> |
| 98 | + </BorderedCard> |
| 99 | + </Link> |
| 100 | + ); |
| 101 | +} |
0 commit comments