|
| 1 | +import { ExternalLink, Loader2, Trash2 } from "lucide-react"; |
| 2 | +import Link from "next/link"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { toast } from "sonner"; |
| 5 | +import { AlertBlock } from "@/components/shared/alert-block"; |
| 6 | +import { DialogAction } from "@/components/shared/dialog-action"; |
| 7 | +import { Badge } from "@/components/ui/badge"; |
| 8 | +import { Button } from "@/components/ui/button"; |
| 9 | +import { |
| 10 | + Dialog, |
| 11 | + DialogContent, |
| 12 | + DialogDescription, |
| 13 | + DialogFooter, |
| 14 | + DialogHeader, |
| 15 | + DialogTitle, |
| 16 | + DialogTrigger, |
| 17 | +} from "@/components/ui/dialog"; |
| 18 | +import { api } from "@/utils/api"; |
| 19 | + |
| 20 | +interface Props { |
| 21 | + serverId: string; |
| 22 | + serverName: string; |
| 23 | + children: React.ReactNode; |
| 24 | +} |
| 25 | + |
| 26 | +const serviceTypeLabel: Record<string, string> = { |
| 27 | + application: "Application", |
| 28 | + compose: "Compose", |
| 29 | + postgres: "Postgres", |
| 30 | + mysql: "MySQL", |
| 31 | + mariadb: "MariaDB", |
| 32 | + mongo: "MongoDB", |
| 33 | + redis: "Redis", |
| 34 | + libsql: "LibSQL", |
| 35 | +}; |
| 36 | + |
| 37 | +export const DeleteServerModal = ({ |
| 38 | + serverId, |
| 39 | + serverName, |
| 40 | + children, |
| 41 | +}: Props) => { |
| 42 | + const [open, setOpen] = useState(false); |
| 43 | + const [deletingId, setDeletingId] = useState<string | null>(null); |
| 44 | + const utils = api.useUtils(); |
| 45 | + |
| 46 | + const { |
| 47 | + data: services, |
| 48 | + isLoading, |
| 49 | + refetch, |
| 50 | + } = api.server.getServices.useQuery({ serverId }, { enabled: open }); |
| 51 | + |
| 52 | + const { mutateAsync: deleteApplication } = |
| 53 | + api.application.delete.useMutation(); |
| 54 | + const { mutateAsync: deleteCompose } = api.compose.delete.useMutation(); |
| 55 | + const { mutateAsync: deletePostgres } = api.postgres.remove.useMutation(); |
| 56 | + const { mutateAsync: deleteMysql } = api.mysql.remove.useMutation(); |
| 57 | + const { mutateAsync: deleteMariadb } = api.mariadb.remove.useMutation(); |
| 58 | + const { mutateAsync: deleteMongo } = api.mongo.remove.useMutation(); |
| 59 | + const { mutateAsync: deleteRedis } = api.redis.remove.useMutation(); |
| 60 | + const { mutateAsync: deleteLibsql } = api.libsql.remove.useMutation(); |
| 61 | + const { mutateAsync: deleteServer, isPending: isDeletingServer } = |
| 62 | + api.server.remove.useMutation(); |
| 63 | + |
| 64 | + const canDelete = (services?.length ?? 0) === 0; |
| 65 | + |
| 66 | + const handleDeleteService = async ( |
| 67 | + service: NonNullable<typeof services>[number], |
| 68 | + ) => { |
| 69 | + setDeletingId(service.id); |
| 70 | + try { |
| 71 | + switch (service.type) { |
| 72 | + case "application": |
| 73 | + await deleteApplication({ applicationId: service.id }); |
| 74 | + break; |
| 75 | + case "compose": |
| 76 | + await deleteCompose({ |
| 77 | + composeId: service.id, |
| 78 | + deleteVolumes: false, |
| 79 | + }); |
| 80 | + break; |
| 81 | + case "postgres": |
| 82 | + await deletePostgres({ postgresId: service.id }); |
| 83 | + break; |
| 84 | + case "mysql": |
| 85 | + await deleteMysql({ mysqlId: service.id }); |
| 86 | + break; |
| 87 | + case "mariadb": |
| 88 | + await deleteMariadb({ mariadbId: service.id }); |
| 89 | + break; |
| 90 | + case "mongo": |
| 91 | + await deleteMongo({ mongoId: service.id }); |
| 92 | + break; |
| 93 | + case "redis": |
| 94 | + await deleteRedis({ redisId: service.id }); |
| 95 | + break; |
| 96 | + case "libsql": |
| 97 | + await deleteLibsql({ libsqlId: service.id }); |
| 98 | + break; |
| 99 | + } |
| 100 | + toast.success(`${service.name} deleted successfully`); |
| 101 | + await refetch(); |
| 102 | + utils.server.all.invalidate(); |
| 103 | + } catch (error: any) { |
| 104 | + toast.error(error.message); |
| 105 | + } finally { |
| 106 | + setDeletingId(null); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + const handleDeleteServer = async () => { |
| 111 | + try { |
| 112 | + await deleteServer({ serverId }); |
| 113 | + toast.success(`Server ${serverName} deleted successfully`); |
| 114 | + setOpen(false); |
| 115 | + utils.server.all.invalidate(); |
| 116 | + } catch (error: any) { |
| 117 | + toast.error(error.message); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + return ( |
| 122 | + <Dialog open={open} onOpenChange={setOpen}> |
| 123 | + <DialogTrigger asChild>{children}</DialogTrigger> |
| 124 | + <DialogContent className="max-w-lg"> |
| 125 | + <DialogHeader> |
| 126 | + <DialogTitle>Delete Server</DialogTitle> |
| 127 | + <DialogDescription> |
| 128 | + This will permanently delete "{serverName}" and all associated data. |
| 129 | + </DialogDescription> |
| 130 | + </DialogHeader> |
| 131 | + |
| 132 | + {isLoading ? ( |
| 133 | + <div className="flex justify-center py-6"> |
| 134 | + <Loader2 className="size-5 animate-spin text-muted-foreground" /> |
| 135 | + </div> |
| 136 | + ) : canDelete ? ( |
| 137 | + <AlertBlock type="info"> |
| 138 | + No services are associated with this server. You can delete it |
| 139 | + safely. |
| 140 | + </AlertBlock> |
| 141 | + ) : ( |
| 142 | + <div className="flex flex-col gap-3"> |
| 143 | + <AlertBlock type="warning"> |
| 144 | + This server has {services?.length} service |
| 145 | + {services?.length === 1 ? "" : "s"} associated. Delete them before |
| 146 | + removing the server. |
| 147 | + </AlertBlock> |
| 148 | + <div className="flex max-h-72 flex-col gap-2 overflow-y-auto pr-1"> |
| 149 | + {services?.map((service) => ( |
| 150 | + <div |
| 151 | + key={`${service.type}-${service.id}`} |
| 152 | + className="flex items-center justify-between gap-2 rounded-lg border p-2" |
| 153 | + > |
| 154 | + <div className="flex min-w-0 flex-col gap-1"> |
| 155 | + <span className="truncate text-sm font-medium"> |
| 156 | + {service.name} |
| 157 | + </span> |
| 158 | + <Badge variant="outline" className="w-fit text-xs"> |
| 159 | + {serviceTypeLabel[service.type]} |
| 160 | + </Badge> |
| 161 | + </div> |
| 162 | + <div className="flex shrink-0 items-center gap-1"> |
| 163 | + <Link href={service.url} target="_blank"> |
| 164 | + <Button variant="ghost" size="icon" className="h-8 w-8"> |
| 165 | + <ExternalLink className="h-4 w-4" /> |
| 166 | + </Button> |
| 167 | + </Link> |
| 168 | + <DialogAction |
| 169 | + title="Delete Service" |
| 170 | + description={`This will permanently delete "${service.name}" and all its associated data.`} |
| 171 | + onClick={() => handleDeleteService(service)} |
| 172 | + > |
| 173 | + <Button |
| 174 | + variant="ghost" |
| 175 | + size="icon" |
| 176 | + disabled={deletingId === service.id} |
| 177 | + className="h-8 w-8 text-destructive hover:bg-destructive/10 hover:text-destructive" |
| 178 | + > |
| 179 | + {deletingId === service.id ? ( |
| 180 | + <Loader2 className="h-4 w-4 animate-spin" /> |
| 181 | + ) : ( |
| 182 | + <Trash2 className="h-4 w-4" /> |
| 183 | + )} |
| 184 | + </Button> |
| 185 | + </DialogAction> |
| 186 | + </div> |
| 187 | + </div> |
| 188 | + ))} |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + )} |
| 192 | + |
| 193 | + <DialogFooter> |
| 194 | + <Button variant="outline" onClick={() => setOpen(false)}> |
| 195 | + Cancel |
| 196 | + </Button> |
| 197 | + <Button |
| 198 | + variant="destructive" |
| 199 | + disabled={!canDelete || isDeletingServer} |
| 200 | + onClick={handleDeleteServer} |
| 201 | + > |
| 202 | + Delete Server |
| 203 | + </Button> |
| 204 | + </DialogFooter> |
| 205 | + </DialogContent> |
| 206 | + </Dialog> |
| 207 | + ); |
| 208 | +}; |
0 commit comments