|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { MdDeleteOutline, MdMoreHoriz, MdPause, MdPlayArrow, MdRefresh, MdSettings } from 'react-icons/md'; |
| 5 | +import { ProjectStatus } from '@prisma/client'; |
| 6 | +import { useRouter } from 'next/navigation'; |
| 7 | + |
| 8 | +import { Button } from '@/components/ui/button'; |
| 9 | +import { |
| 10 | + DropdownMenu, |
| 11 | + DropdownMenuContent, |
| 12 | + DropdownMenuItem, |
| 13 | + DropdownMenuSeparator, |
| 14 | + DropdownMenuTrigger, |
| 15 | +} from '@/components/ui/dropdown-menu'; |
| 16 | +import { |
| 17 | + FullScreenDialog, |
| 18 | + FullScreenDialogAction, |
| 19 | + FullScreenDialogClose, |
| 20 | + FullScreenDialogContent, |
| 21 | + FullScreenDialogDescription, |
| 22 | + FullScreenDialogFooter, |
| 23 | + FullScreenDialogHeader, |
| 24 | + FullScreenDialogTitle, |
| 25 | +} from '@/components/ui/fullscreen-dialog'; |
| 26 | +import { Input } from '@/components/ui/input'; |
| 27 | +import { Label } from '@/components/ui/label'; |
| 28 | +import { useProjectOperations } from '@/hooks/use-project-operations'; |
| 29 | + |
| 30 | +interface ProjectActionsMenuProps { |
| 31 | + projectId: string; |
| 32 | + projectName: string; |
| 33 | + status: ProjectStatus; |
| 34 | +} |
| 35 | + |
| 36 | +export function ProjectActionsMenu({ projectId, projectName, status }: ProjectActionsMenuProps) { |
| 37 | + const router = useRouter(); |
| 38 | + const [showDeleteDialog, setShowDeleteDialog] = useState(false); |
| 39 | + const [confirmInput, setConfirmInput] = useState(''); |
| 40 | + const { executeOperation, loading } = useProjectOperations(projectId); |
| 41 | + |
| 42 | + // Determine available actions based on status |
| 43 | + const showStart = status === 'STOPPED'; |
| 44 | + const showStop = status !== 'STOPPED'; |
| 45 | + |
| 46 | + // Check if the confirmation input matches the project name |
| 47 | + const isConfirmValid = confirmInput === projectName; |
| 48 | + |
| 49 | + const handleDeleteClick = () => { |
| 50 | + setShowDeleteDialog(true); |
| 51 | + }; |
| 52 | + |
| 53 | + const handleDeleteConfirm = () => { |
| 54 | + if (!isConfirmValid) return; |
| 55 | + setShowDeleteDialog(false); |
| 56 | + setConfirmInput(''); |
| 57 | + executeOperation('DELETE'); |
| 58 | + }; |
| 59 | + |
| 60 | + const handleDialogOpenChange = (open: boolean) => { |
| 61 | + setShowDeleteDialog(open); |
| 62 | + if (!open) { |
| 63 | + setConfirmInput(''); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const handleSettingsClick = () => { |
| 68 | + router.push(`/projects/${projectId}/settings`); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <> |
| 73 | + <DropdownMenu> |
| 74 | + <DropdownMenuTrigger asChild> |
| 75 | + <Button |
| 76 | + variant="ghost" |
| 77 | + size="icon" |
| 78 | + className="absolute top-3 right-3 h-8 w-8 text-muted-foreground hover:text-white" |
| 79 | + onClick={(e) => e.stopPropagation()} |
| 80 | + > |
| 81 | + <MdMoreHoriz className="w-5 h-5" /> |
| 82 | + <span className="sr-only">More options</span> |
| 83 | + </Button> |
| 84 | + </DropdownMenuTrigger> |
| 85 | + <DropdownMenuContent |
| 86 | + align="end" |
| 87 | + className="w-32 bg-popover/90 backdrop-blur-md" |
| 88 | + > |
| 89 | + {/* Start/Stop based on status */} |
| 90 | + {showStart && ( |
| 91 | + <DropdownMenuItem |
| 92 | + onClick={(e) => { |
| 93 | + e.stopPropagation(); |
| 94 | + executeOperation('START'); |
| 95 | + }} |
| 96 | + disabled={loading !== null} |
| 97 | + className="gap-3 px-3 py-2 text-xs font-medium text-muted-foreground hover:text-white hover:bg-white/5" |
| 98 | + > |
| 99 | + {loading === 'START' ? ( |
| 100 | + <> |
| 101 | + <MdRefresh className="h-[18px] w-[18px] animate-spin" /> |
| 102 | + Starting... |
| 103 | + </> |
| 104 | + ) : ( |
| 105 | + <> |
| 106 | + <MdPlayArrow className="h-[18px] w-[18px]" /> |
| 107 | + Start |
| 108 | + </> |
| 109 | + )} |
| 110 | + </DropdownMenuItem> |
| 111 | + )} |
| 112 | + {showStop && ( |
| 113 | + <DropdownMenuItem |
| 114 | + onClick={(e) => { |
| 115 | + e.stopPropagation(); |
| 116 | + executeOperation('STOP'); |
| 117 | + }} |
| 118 | + disabled={loading !== null} |
| 119 | + className="gap-3 px-3 py-2 text-xs font-medium text-muted-foreground hover:text-white hover:bg-white/5" |
| 120 | + > |
| 121 | + {loading === 'STOP' ? ( |
| 122 | + <> |
| 123 | + <MdRefresh className="h-[18px] w-[18px] animate-spin" /> |
| 124 | + Stopping... |
| 125 | + </> |
| 126 | + ) : ( |
| 127 | + <> |
| 128 | + <MdPause className="h-[18px] w-[18px]" /> |
| 129 | + Stop |
| 130 | + </> |
| 131 | + )} |
| 132 | + </DropdownMenuItem> |
| 133 | + )} |
| 134 | + |
| 135 | + {/* Settings */} |
| 136 | + <DropdownMenuItem |
| 137 | + onClick={(e) => { |
| 138 | + e.stopPropagation(); |
| 139 | + handleSettingsClick(); |
| 140 | + }} |
| 141 | + className="gap-3 px-3 py-2 text-xs font-medium text-muted-foreground hover:text-white hover:bg-white/5" |
| 142 | + > |
| 143 | + <MdSettings className="h-[18px] w-[18px]" /> |
| 144 | + Settings |
| 145 | + </DropdownMenuItem> |
| 146 | + |
| 147 | + <DropdownMenuSeparator className="bg-border/60 mx-2 my-1" /> |
| 148 | + |
| 149 | + {/* Delete */} |
| 150 | + <DropdownMenuItem |
| 151 | + onClick={(e) => { |
| 152 | + e.stopPropagation(); |
| 153 | + handleDeleteClick(); |
| 154 | + }} |
| 155 | + disabled={loading !== null} |
| 156 | + className="gap-3 px-3 py-2 text-xs font-medium text-red-500 hover:text-red-400 hover:bg-red-500/10" |
| 157 | + > |
| 158 | + <MdDeleteOutline className="h-[18px] w-[18px] text-red-500" /> |
| 159 | + Delete |
| 160 | + </DropdownMenuItem> |
| 161 | + </DropdownMenuContent> |
| 162 | + </DropdownMenu> |
| 163 | + |
| 164 | + {/* Delete Confirmation Dialog */} |
| 165 | + <FullScreenDialog open={showDeleteDialog} onOpenChange={handleDialogOpenChange}> |
| 166 | + <FullScreenDialogContent> |
| 167 | + <FullScreenDialogHeader> |
| 168 | + <FullScreenDialogTitle> |
| 169 | + Are you sure you want to delete <br /> |
| 170 | + <span className="text-white">"{projectName}"</span>? |
| 171 | + </FullScreenDialogTitle> |
| 172 | + <FullScreenDialogDescription> |
| 173 | + This will terminate all resources (databases, sandboxes) and cannot be undone. |
| 174 | + </FullScreenDialogDescription> |
| 175 | + </FullScreenDialogHeader> |
| 176 | + |
| 177 | + {/* Confirmation Input */} |
| 178 | + <div className="space-y-2"> |
| 179 | + <Label className="text-xs font-semibold text-muted-foreground uppercase tracking-wider pl-1"> |
| 180 | + Type <span className="text-white select-all">{projectName}</span> to confirm |
| 181 | + </Label> |
| 182 | + <Input |
| 183 | + value={confirmInput} |
| 184 | + onChange={(e) => setConfirmInput(e.target.value)} |
| 185 | + placeholder={projectName} |
| 186 | + className="bg-background border-border rounded-xl px-4 py-3 text-white placeholder:text-muted-foreground/30 focus-visible:border-red-500 focus-visible:ring-red-500/50 font-mono text-sm shadow-inner" |
| 187 | + /> |
| 188 | + </div> |
| 189 | + |
| 190 | + <FullScreenDialogFooter> |
| 191 | + <FullScreenDialogClose> |
| 192 | + Cancel |
| 193 | + </FullScreenDialogClose> |
| 194 | + <FullScreenDialogAction |
| 195 | + onClick={handleDeleteConfirm} |
| 196 | + disabled={!isConfirmValid} |
| 197 | + variant="destructive" |
| 198 | + > |
| 199 | + Permanently Delete |
| 200 | + </FullScreenDialogAction> |
| 201 | + </FullScreenDialogFooter> |
| 202 | + </FullScreenDialogContent> |
| 203 | + </FullScreenDialog> |
| 204 | + </> |
| 205 | + ); |
| 206 | +} |
0 commit comments