import { arktypeResolver } from "@hookform/resolvers/arktype"; import { useMutation } from "@tanstack/react-query"; import { type } from "arktype"; import { CheckCircle, Loader2, XCircle } from "lucide-react"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { cn, slugify } from "~/client/lib/utils"; import { deepClean } from "~/utils/object"; import { volumeConfigSchema } from "~/schemas/volumes"; import { testConnectionMutation } from "~/client/api-client/@tanstack/react-query.gen"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "~/client/components/ui/form"; import { Input } from "~/client/components/ui/input"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "~/client/components/ui/select"; import { Button } from "~/client/components/ui/button"; import { DirectoryBrowser } from "~/client/components/directory-browser"; const SUPPORTS_CONNECTION_TEST = ["nfs", "smb", "webdav", "mariadb", "mysql", "postgres"]; export const formSchema = type({ name: "2<=string<=32", }).and(volumeConfigSchema); const cleanSchema = type.pipe((d) => formSchema(deepClean(d))); export type FormValues = typeof formSchema.inferIn; type Props = { onSubmit: (values: FormValues) => void; mode?: "create" | "update"; initialValues?: Partial; formId?: string; loading?: boolean; className?: string; }; const defaultValuesForType = { directory: { backend: "directory" as const, path: "/" }, nfs: { backend: "nfs" as const, port: 2049, version: "4.1" as const }, smb: { backend: "smb" as const, port: 445, vers: "3.0" as const }, webdav: { backend: "webdav" as const, port: 80, ssl: false }, mariadb: { backend: "mariadb" as const, port: 3306 }, mysql: { backend: "mysql" as const, port: 3306 }, postgres: { backend: "postgres" as const, port: 5432, dumpFormat: "custom" as const }, }; export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, formId, loading, className }: Props) => { const form = useForm({ resolver: arktypeResolver(cleanSchema as unknown as typeof formSchema), defaultValues: initialValues, resetOptions: { keepDefaultValues: true, keepDirtyValues: false, }, }); const { watch, getValues } = form; const watchedBackend = watch("backend"); useEffect(() => { if (mode === "create") { form.reset({ name: form.getValues().name, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType], }); } }, [watchedBackend, form, mode]); const [testMessage, setTestMessage] = useState<{ success: boolean; message: string } | null>(null); const testBackendConnection = useMutation({ ...testConnectionMutation(), onMutate: () => { setTestMessage(null); }, onError: (error) => { setTestMessage({ success: false, message: error?.message || "Failed to test connection. Please try again.", }); }, onSuccess: (data) => { setTestMessage(data); }, }); const handleTestConnection = async () => { const formValues = getValues(); if (SUPPORTS_CONNECTION_TEST.includes(formValues.backend)) { testBackendConnection.mutate({ body: { config: formValues }, }); } }; return (
( Name field.onChange(slugify(e.target.value))} max={32} min={1} disabled={mode === "update"} className={mode === "update" ? "bg-gray-50" : ""} /> Unique identifier for the volume. )} /> ( Backend Choose the storage backend for this volume. )} /> {watchedBackend === "directory" && ( { return ( Directory Path {field.value ? (
Selected path:
{field.value}
) : ( field.onChange(path)} selectedPath={field.value} /> )}
Browse and select a directory on the host filesystem to track.
); }} /> )} {watchedBackend === "nfs" && ( <> ( Server NFS server IP address or hostname. )} /> ( Export Path Path to the NFS export on the server. )} /> ( Port NFS server port (default: 2049). )} /> ( Version NFS protocol version to use. )} /> ( Read-only Mode
field.onChange(e.target.checked)} className="rounded border-gray-300" /> Mount volume as read-only
Prevent any modifications to the volume. Recommended for backup sources and sensitive data.
)} /> )} {watchedBackend === "webdav" && ( <> ( Server WebDAV server hostname or IP address. )} /> ( Path Path to the WebDAV directory on the server. )} /> ( Username (Optional) Username for WebDAV authentication (optional). )} /> ( Password (Optional) Password for WebDAV authentication (optional). )} /> ( Port WebDAV server port (default: 80 for HTTP, 443 for HTTPS). )} /> ( Use SSL/HTTPS
field.onChange(e.target.checked)} className="rounded border-gray-300" /> Enable HTTPS for secure connections
Use HTTPS instead of HTTP for secure connections.
)} /> ( Read-only Mode
field.onChange(e.target.checked)} className="rounded border-gray-300" /> Mount volume as read-only
Prevent any modifications to the volume. Recommended for backup sources and sensitive data.
)} /> )} {watchedBackend === "smb" && ( <> ( Server SMB server IP address or hostname. )} /> ( Share SMB share name on the server. )} /> ( Username Username for SMB authentication. )} /> ( Password Password for SMB authentication. )} /> ( SMB Version SMB protocol version to use (default: 3.0). )} /> ( Domain (Optional) Domain or workgroup for authentication (optional). )} /> ( Port field.onChange(parseInt(e.target.value, 10) || undefined)} /> SMB server port (default: 445). )} /> ( Read-only Mode
field.onChange(e.target.checked)} className="rounded border-gray-300" /> Mount volume as read-only
Prevent any modifications to the volume. Recommended for backup sources and sensitive data.
)} /> )} {watchedBackend === "mariadb" && ( <> ( Host MariaDB server hostname or IP address. )} /> ( Port MariaDB server port (default: 3306). )} /> ( Username Database user with backup privileges. )} /> ( Password Password for database authentication. )} /> ( Database Name of the database to backup. )} /> )} {watchedBackend === "mysql" && ( <> ( Host MySQL server hostname or IP address. )} /> ( Port MySQL server port (default: 3306). )} /> ( Username Database user with backup privileges. )} /> ( Password Password for database authentication. )} /> ( Database Name of the database to backup. )} /> )} {watchedBackend === "postgres" && ( <> ( Host PostgreSQL server hostname or IP address. )} /> ( Port PostgreSQL server port (default: 5432). )} /> ( Username Database user with backup privileges. )} /> ( Password Password for database authentication. )} /> ( Database Name of the database to backup. )} /> ( Dump Format Format for database dumps (custom recommended). )} /> )} {watchedBackend !== "directory" && (
{testMessage && (
{testMessage.message}
)}
)} {mode === "update" && ( )} ); };