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 { DirectoryBrowser } from "./directory-browser"; import { Button } from "./ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "./ui/form"; import { Input } from "./ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select"; import { volumeConfigSchema } from "~/schemas/volumes"; import { testConnectionMutation } from "../api-client/@tanstack/react-query.gen"; 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 }, }; 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 (formValues.backend === "nfs" || formValues.backend === "smb" || formValues.backend === "webdav") { 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.
)} /> )}
{testMessage && (
{testMessage.message}
)}
{mode === "update" && ( )} ); };