import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema"; import { PlusIcon } from "lucide-react"; import type React from "react"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { AlertBlock } from "@/components/shared/alert-block"; import { CodeEditor } from "@/components/shared/code-editor"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { cn } from "@/lib/utils"; import { api } from "@/utils/api"; interface Props { serviceId: string; serviceType: | "application" | "compose" | "libsql" | "mariadb" | "mongo" | "mysql" | "postgres" | "redis"; refetch: () => void; children?: React.ReactNode; } const mountSchema = z.object({ mountPath: z.string().min(1, "Mount path required"), }); const mySchema = z.discriminatedUnion("type", [ z .object({ type: z.literal("bind"), hostPath: z.string().min(1, "Host path required"), }) .merge(mountSchema), z .object({ type: z.literal("volume"), volumeName: z .string() .min(1, "Volume name required") .regex( /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/, "Invalid volume name. Use letters, numbers, '._-' and start with a letter/number.", ), }) .merge(mountSchema), z .object({ type: z.literal("file"), filePath: z.string().min(1, "File path required"), content: z.string().optional(), }) .merge(mountSchema), ]); type AddMount = z.infer; export const AddVolumes = ({ serviceId, serviceType, refetch, children = , }: Props) => { const [isOpen, setIsOpen] = useState(false); const { mutateAsync } = api.mounts.create.useMutation(); const form = useForm({ defaultValues: { type: serviceType === "compose" ? "file" : "bind", hostPath: "", mountPath: serviceType === "compose" ? "/" : "", }, resolver: zodResolver(mySchema), }); const type = form.watch("type"); useEffect(() => { form.reset(); }, [form, form.reset, form.formState.isSubmitSuccessful]); const onSubmit = async (data: AddMount) => { if (data.type === "bind") { await mutateAsync({ serviceId, hostPath: data.hostPath, mountPath: data.mountPath, type: data.type, serviceType, }) .then(() => { toast.success("Mount Created"); setIsOpen(false); }) .catch(() => { toast.error("Error creating the Bind mount"); }); } else if (data.type === "volume") { await mutateAsync({ serviceId, volumeName: data.volumeName, mountPath: data.mountPath, type: data.type, serviceType, }) .then(() => { toast.success("Mount Created"); setIsOpen(false); }) .catch(() => { toast.error("Error creating the Volume mount"); }); } else if (data.type === "file") { await mutateAsync({ serviceId, content: data.content, mountPath: data.mountPath, filePath: data.filePath, type: data.type, serviceType, }) .then(() => { toast.success("Mount Created"); setIsOpen(false); }) .catch(() => { toast.error("Error creating the File mount"); }); } refetch(); }; return ( Volumes / Mounts {/* {isError && (
{error?.message}
)} */}
{type === "bind" && (

Make sure the host path is a valid path and exists in the host machine.

Cluster Warning: If you're using cluster features, bind mounts may cause deployment failures since the path must exist on all worker/manager nodes. Consider using external tools to distribute the folder across nodes or use named volumes instead.

)} ( Select the Mount Type {serviceType !== "compose" && (
)} {serviceType !== "compose" && (
)}
)} />
Fill the next fields.
{type === "bind" && ( ( Host Path )} /> )} {type === "volume" && ( ( Volume Name )} /> )} {type === "file" && ( <> ( Content )} /> ( File Path )} /> )} {serviceType !== "compose" && ( ( Mount Path (In the container) )} /> )}
); };