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 { Textarea } from "@/components/ui/textarea"; import { cn } from "@/lib/utils"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { PlusIcon } from "lucide-react"; import type React from "react"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; interface Props { serviceId: string; serviceType: | "application" | "postgres" | "redis" | "mongo" | "redis" | "mysql" | "mariadb" | "compose"; 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"), }) .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 { 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"); }) .catch(() => { toast.error("Error to create 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"); }) .catch(() => { toast.error("Error to create 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"); }) .catch(() => { toast.error("Error to create the File mount"); }); } refetch(); }; return ( Volumes / Mounts {/* {isError && (
{error?.message}
)} */}
( Select the Mount Type {serviceType !== "compose" && (
)} {serviceType !== "compose" && (
)}
)} />
Fill the next fields.
{type === "bind" && ( ( Host Path )} /> )} {type === "volume" && ( ( Volume Name )} /> )} {type === "file" && ( <> ( Content