Skip to content

Commit 0276021

Browse files
committed
feat: list associated services in delete server modal
Show the services attached to a remote server directly in its delete confirmation modal, with a link to each service and a per-service delete action, instead of only showing a generic 'has active services' blocker.
1 parent 0478f34 commit 0276021

4 files changed

Lines changed: 343 additions & 53 deletions

File tree

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
};

‎apps/dokploy/components/dashboard/settings/servers/show-servers.tsx‎

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import {
1212
} from "lucide-react";
1313
import Link from "next/link";
1414
import { useRouter } from "next/router";
15-
import { toast } from "sonner";
16-
import { AlertBlock } from "@/components/shared/alert-block";
17-
import { DialogAction } from "@/components/shared/dialog-action";
1815
import { Badge } from "@/components/ui/badge";
1916
import { Button } from "@/components/ui/button";
2017
import {
@@ -33,6 +30,7 @@ import {
3330
import { api } from "@/utils/api";
3431
import { TerminalModal } from "../web-server/terminal-modal";
3532
import { ShowServerActions } from "./actions/show-server-actions";
33+
import { DeleteServerModal } from "./delete-server-modal";
3634
import { HandleServers } from "./handle-servers";
3735
import { SetupServer } from "./setup-server";
3836
import { ShowHealthModal } from "./show-health-modal";
@@ -43,7 +41,6 @@ export const ShowServers = () => {
4341
const router = useRouter();
4442
const query = router.query;
4543
const { data, refetch, isPending } = api.server.all.useQuery();
46-
const { mutateAsync } = api.server.remove.useMutation();
4744
const { data: sshKeys } = api.sshKey.all.useQuery();
4845
const { data: isCloud } = api.settings.isCloud.useQuery();
4946
const { data: canCreateMoreServers } =
@@ -111,7 +108,6 @@ export const ShowServers = () => {
111108
<div className="flex flex-col gap-4 min-h-[25vh]">
112109
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
113110
{data?.map((server) => {
114-
const canDelete = server.totalSum === 0;
115111
const isActive = server.serverStatus === "active";
116112
const isBuildServer = server.serverType === "build";
117113
return (
@@ -350,64 +346,22 @@ export const ShowServers = () => {
350346
<Tooltip>
351347
<TooltipTrigger asChild>
352348
<div>
353-
<DialogAction
354-
disabled={!canDelete}
355-
title={
356-
canDelete
357-
? "Delete Server"
358-
: "Server has active services"
359-
}
360-
description={
361-
canDelete ? (
362-
"This will delete the server and all associated data"
363-
) : (
364-
<div className="flex flex-col gap-2">
365-
You can not delete this
366-
server because it has
367-
active services.
368-
<AlertBlock type="warning">
369-
You have active
370-
services associated
371-
with this server,
372-
please delete them
373-
first.
374-
</AlertBlock>
375-
</div>
376-
)
377-
}
378-
onClick={async () => {
379-
await mutateAsync({
380-
serverId: server.serverId,
381-
})
382-
.then(() => {
383-
refetch();
384-
toast.success(
385-
`Server ${server.name} deleted successfully`,
386-
);
387-
})
388-
.catch((err) => {
389-
toast.error(
390-
err.message,
391-
);
392-
});
393-
}}
349+
<DeleteServerModal
350+
serverId={server.serverId}
351+
serverName={server.name}
394352
>
395353
<Button
396354
variant="ghost"
397355
size="icon"
398-
className={`h-9 w-9 ${canDelete ? "text-destructive hover:text-destructive hover:bg-destructive/10" : "text-muted-foreground hover:bg-muted"}`}
356+
className="h-9 w-9 text-destructive hover:text-destructive hover:bg-destructive/10"
399357
>
400358
<Trash2 className="h-4 w-4" />
401359
</Button>
402-
</DialogAction>
360+
</DeleteServerModal>
403361
</div>
404362
</TooltipTrigger>
405363
<TooltipContent>
406-
<p>
407-
{canDelete
408-
? "Delete Server"
409-
: "Cannot delete - has active services"}
410-
</p>
364+
<p>Delete Server</p>
411365
</TooltipContent>
412366
</Tooltip>
413367
)}

‎apps/dokploy/server/api/routers/server.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
findUserById,
88
getAccessibleServerIds,
99
getPublicIpWithFallback,
10+
getServicesByServerId,
1011
haveActiveServices,
1112
IS_CLOUD,
1213
redactServerSshKey,
@@ -18,6 +19,7 @@ import {
1819
updateServerById,
1920
} from "@dokploy/server";
2021
import { db } from "@dokploy/server/db";
22+
import { findMemberByUserId } from "@dokploy/server/services/permission";
2123
import { hasValidLicense } from "@dokploy/server/services/proprietary/license-key";
2224
import { TRPCError } from "@trpc/server";
2325
import { observable } from "@trpc/server/observable";
@@ -115,6 +117,41 @@ export const serverRouter = createTRPCRouter({
115117
const isBuildServer = server.serverType === "build";
116118
return defaultCommand(isBuildServer);
117119
}),
120+
getServices: withPermission("server", "read")
121+
.input(apiFindOneServer)
122+
.query(async ({ input, ctx }) => {
123+
const currentServer = await findServerById(input.serverId);
124+
if (currentServer.organizationId !== ctx.session.activeOrganizationId) {
125+
throw new TRPCError({
126+
code: "UNAUTHORIZED",
127+
message: "You are not authorized to access this server",
128+
});
129+
}
130+
131+
const accessibleIds = await getAccessibleServerIds(ctx.session);
132+
if (!accessibleIds.has(input.serverId)) {
133+
throw new TRPCError({
134+
code: "UNAUTHORIZED",
135+
message: "You are not authorized to access this server",
136+
});
137+
}
138+
139+
const services = await getServicesByServerId(input.serverId);
140+
141+
const isPrivileged =
142+
ctx.user.role === "owner" || ctx.user.role === "admin";
143+
if (isPrivileged) {
144+
return services;
145+
}
146+
147+
const { accessedServices } = await findMemberByUserId(
148+
ctx.user.id,
149+
ctx.session.activeOrganizationId,
150+
);
151+
return services.filter((service) =>
152+
accessedServices.includes(service.id),
153+
);
154+
}),
118155
all: withPermission("server", "read").query(async ({ ctx }) => {
119156
const accessibleIds = await getAccessibleServerIds(ctx.session);
120157

0 commit comments

Comments
 (0)