|
| 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} |
| 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 | +}; |
0 commit comments