Skip to content

Commit 38d157a

Browse files
committed
fix(compose): move Fresh Volumes into a Danger Zone with typed confirmation
1 parent 3b1fec5 commit 38d157a

3 files changed

Lines changed: 219 additions & 53 deletions

File tree

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
2+
import copy from "copy-to-clipboard";
3+
import { AlertTriangle, Copy, HardDriveDownload } from "lucide-react";
4+
import { useRouter } from "next/router";
5+
import { useState } from "react";
6+
import { useForm } from "react-hook-form";
7+
import { toast } from "sonner";
8+
import { z } from "zod";
9+
import { AlertBlock } from "@/components/shared/alert-block";
10+
import { Badge } from "@/components/ui/badge";
11+
import { Button } from "@/components/ui/button";
12+
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
13+
import {
14+
Dialog,
15+
DialogContent,
16+
DialogDescription,
17+
DialogFooter,
18+
DialogHeader,
19+
DialogTitle,
20+
DialogTrigger,
21+
} from "@/components/ui/dialog";
22+
import {
23+
Form,
24+
FormControl,
25+
FormField,
26+
FormItem,
27+
FormLabel,
28+
FormMessage,
29+
} from "@/components/ui/form";
30+
import { Input } from "@/components/ui/input";
31+
import { api } from "@/utils/api";
32+
33+
const freshVolumesSchema = z.object({
34+
confirmName: z.string().min(1, {
35+
message: "Compose name is required",
36+
}),
37+
});
38+
39+
type FreshVolumesForm = z.infer<typeof freshVolumesSchema>;
40+
41+
interface Props {
42+
composeId: string;
43+
}
44+
45+
export const FreshVolumes = ({ composeId }: Props) => {
46+
const router = useRouter();
47+
const [isOpen, setIsOpen] = useState(false);
48+
const { data: permissions } = api.user.getPermissions.useQuery();
49+
const canDeploy = permissions?.deployment.create ?? false;
50+
const { data, refetch } = api.compose.one.useQuery(
51+
{ composeId },
52+
{ enabled: !!composeId },
53+
);
54+
const { mutateAsync: deploy, isPending } = api.compose.deploy.useMutation();
55+
56+
const form = useForm<FreshVolumesForm>({
57+
defaultValues: { confirmName: "" },
58+
resolver: zodResolver(freshVolumesSchema),
59+
});
60+
61+
const expectedName = `${data?.name}/${data?.appName}`;
62+
const isRunning = data?.composeStatus === "running";
63+
64+
const onSubmit = async (formData: FreshVolumesForm) => {
65+
if (formData.confirmName !== expectedName) {
66+
form.setError("confirmName", {
67+
message: `Compose name must match "${expectedName}"`,
68+
});
69+
return;
70+
}
71+
await deploy({ composeId, freshVolumes: true })
72+
.then(() => {
73+
toast.success("Compose deployed with fresh volumes");
74+
setIsOpen(false);
75+
form.reset();
76+
refetch();
77+
router.push(
78+
`/dashboard/project/${data?.environment.projectId}/environment/${data?.environmentId}/services/compose/${composeId}?tab=deployments`,
79+
);
80+
})
81+
.catch(() => {
82+
toast.error("Error deploying compose");
83+
});
84+
};
85+
86+
if (!canDeploy || data?.composeType !== "docker-compose") return null;
87+
88+
return (
89+
<Card className="bg-background border-destructive/50">
90+
<CardHeader>
91+
<CardTitle className="text-xl flex items-center gap-2">
92+
<AlertTriangle className="h-5 w-5 text-destructive" />
93+
Danger Zone
94+
</CardTitle>
95+
</CardHeader>
96+
<CardContent>
97+
<div className="flex flex-col gap-4">
98+
<div className="flex flex-col gap-2">
99+
<h3 className="text-base font-semibold">
100+
Deploy with Fresh Volumes
101+
</h3>
102+
<p className="text-sm text-muted-foreground">
103+
Permanently wipes every volume of this compose and redeploys it
104+
from a clean state. All persistent data (databases, uploads,
105+
caches) will be lost.
106+
</p>
107+
</div>
108+
<Dialog
109+
open={isOpen}
110+
onOpenChange={(open) => {
111+
setIsOpen(open);
112+
if (!open) form.reset();
113+
}}
114+
>
115+
<DialogTrigger asChild>
116+
<Button
117+
isLoading={isPending}
118+
variant="outline"
119+
className="w-full border-destructive/50 hover:bg-destructive/10 hover:text-destructive text-destructive"
120+
>
121+
<HardDriveDownload className="mr-2 h-4 w-4" />
122+
Deploy with Fresh Volumes
123+
</Button>
124+
</DialogTrigger>
125+
<DialogContent className="sm:max-w-lg">
126+
<DialogHeader>
127+
<DialogTitle className="flex items-center gap-2">
128+
<AlertTriangle className="h-5 w-5 text-destructive" />
129+
Are you absolutely sure?
130+
</DialogTitle>
131+
<DialogDescription asChild>
132+
<div className="space-y-2">
133+
<p>This action will:</p>
134+
<ul className="list-disc list-inside space-y-1">
135+
<li>Stop the current compose</li>
136+
<li>Delete all volumes and their data</li>
137+
<li>Redeploy the compose with empty volumes</li>
138+
</ul>
139+
<p className="font-medium text-destructive mt-4">
140+
This action cannot be undone.
141+
</p>
142+
</div>
143+
</DialogDescription>
144+
</DialogHeader>
145+
<Form {...form}>
146+
<form
147+
onSubmit={form.handleSubmit(onSubmit)}
148+
id="hook-form-fresh-volumes"
149+
className="grid w-full gap-4"
150+
>
151+
<FormField
152+
control={form.control}
153+
name="confirmName"
154+
render={({ field }) => (
155+
<FormItem>
156+
<FormLabel className="flex items-center gap-2">
157+
<span>
158+
To confirm, type{" "}
159+
<Badge
160+
className="p-2 rounded-md ml-1 mr-1 hover:border-primary hover:text-primary-foreground hover:bg-primary hover:cursor-pointer"
161+
variant="outline"
162+
onClick={() => {
163+
if (data?.name && data?.appName) {
164+
copy(expectedName);
165+
toast.success(
166+
"Copied to clipboard. Be careful!",
167+
);
168+
}
169+
}}
170+
>
171+
{expectedName}&nbsp;
172+
<Copy className="h-4 w-4 ml-1 text-muted-foreground" />
173+
</Badge>{" "}
174+
in the box below:
175+
</span>
176+
</FormLabel>
177+
<FormControl>
178+
<Input
179+
placeholder="Enter compose name to confirm"
180+
autoComplete="off"
181+
{...field}
182+
/>
183+
</FormControl>
184+
<FormMessage />
185+
</FormItem>
186+
)}
187+
/>
188+
</form>
189+
</Form>
190+
{isRunning && (
191+
<AlertBlock type="warning" className="w-full">
192+
Cannot deploy with fresh volumes while a deployment is
193+
running. Please wait for it to finish and then try again.
194+
</AlertBlock>
195+
)}
196+
<DialogFooter>
197+
<Button variant="secondary" onClick={() => setIsOpen(false)}>
198+
Cancel
199+
</Button>
200+
<Button
201+
isLoading={isPending}
202+
disabled={isRunning}
203+
form="hook-form-fresh-volumes"
204+
type="submit"
205+
variant="destructive"
206+
>
207+
Wipe volumes and deploy
208+
</Button>
209+
</DialogFooter>
210+
</DialogContent>
211+
</Dialog>
212+
</div>
213+
</CardContent>
214+
</Card>
215+
);
216+
};

‎apps/dokploy/components/dashboard/compose/general/actions.tsx‎

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
Ban,
3-
CheckCircle2,
4-
HardDriveDownload,
5-
RefreshCcw,
6-
Rocket,
7-
Terminal,
8-
} from "lucide-react";
1+
import { Ban, CheckCircle2, RefreshCcw, Rocket, Terminal } from "lucide-react";
92
import { useRouter } from "next/router";
103
import { Tooltip as TooltipPrimitive } from "radix-ui";
114
import { toast } from "sonner";
@@ -89,51 +82,6 @@ export const ComposeActions = ({ composeId }: Props) => {
8982
</Button>
9083
</DialogAction>
9184
)}
92-
{canDeploy && data?.composeType === "docker-compose" && (
93-
<DialogAction
94-
title="Deploy with Fresh Volumes"
95-
description="This will remove all volumes and redeploy with a clean state. All persistent data will be permanently deleted."
96-
type="destructive"
97-
onClick={async () => {
98-
await deploy({
99-
composeId: composeId,
100-
freshVolumes: true,
101-
})
102-
.then(() => {
103-
toast.success("Compose deployed with fresh volumes");
104-
refetch();
105-
router.push(
106-
`/dashboard/project/${data?.environment.projectId}/environment/${data?.environmentId}/services/compose/${composeId}?tab=deployments`,
107-
);
108-
})
109-
.catch(() => {
110-
toast.error("Error deploying compose");
111-
});
112-
}}
113-
>
114-
<Button
115-
variant="outline"
116-
isLoading={data?.composeStatus === "running"}
117-
className="flex items-center gap-1.5 group focus-visible:ring-2 focus-visible:ring-offset-2"
118-
>
119-
<Tooltip>
120-
<TooltipTrigger asChild>
121-
<div className="flex items-center">
122-
<HardDriveDownload className="size-4 mr-1" />
123-
Fresh Volumes
124-
</div>
125-
</TooltipTrigger>
126-
<TooltipPrimitive.Portal>
127-
<TooltipContent sideOffset={5} className="z-[60]">
128-
<p>
129-
Deploy with fresh volumes (removes all persistent data)
130-
</p>
131-
</TooltipContent>
132-
</TooltipPrimitive.Portal>
133-
</Tooltip>
134-
</Button>
135-
</DialogAction>
136-
)}
13785
{canDeploy && (
13886
<DialogAction
13987
title="Rebuild Compose"

‎apps/dokploy/pages/dashboard/project/[projectId]/environment/[environmentId]/services/compose/[composeId].tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ShowSchedules } from "@/components/dashboard/application/schedules/show
2323
import { ShowVolumeBackups } from "@/components/dashboard/application/volume-backups/show-volume-backups";
2424
import { AddCommandCompose } from "@/components/dashboard/compose/advanced/add-command";
2525
import { IsolatedDeploymentTab } from "@/components/dashboard/compose/advanced/add-isolation";
26+
import { FreshVolumes } from "@/components/dashboard/compose/advanced/fresh-volumes";
2627
import { ShowComposeContainers } from "@/components/dashboard/compose/containers/show-compose-containers";
2728
import { DeleteService } from "@/components/dashboard/compose/delete-service";
2829
import { ShowGeneralCompose } from "@/components/dashboard/compose/general/show";
@@ -442,6 +443,7 @@ const Service = (
442443
<ShowImport composeId={composeId} />
443444
<AssignComposeNetworks composeId={composeId} />
444445
<IsolatedDeploymentTab composeId={composeId} />
446+
<FreshVolumes composeId={composeId} />
445447
</div>
446448
</TabsContent>
447449
)}

0 commit comments

Comments
 (0)