import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation } from "@tanstack/react-query"; import { CheckCircle, Loader2, Plug, Save, XCircle } from "lucide-react"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { cn } from "~/client/lib/utils"; import { Button } from "../../../components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "../../../components/ui/form"; import { Input } from "../../../components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select"; import { directoryConfigSchema, nfsConfigSchema, rcloneConfigSchema, sftpConfigSchema, smbConfigSchema, volumeConfigSchema, webdavConfigSchema, } from "@zerobyte/contracts/volumes"; import { testConnectionMutation } from "../../../api-client/@tanstack/react-query.gen"; import { Tooltip, TooltipContent, TooltipTrigger } from "../../../components/ui/tooltip"; import { useSystemInfo } from "~/client/hooks/use-system-info"; import { useScrollToFormError } from "~/client/hooks/use-scroll-to-form-error"; import { DirectoryForm, NFSForm, SMBForm, WebDAVForm, RcloneForm, SFTPForm } from "./volume-forms"; export const formSchema = z .discriminatedUnion("backend", [ directoryConfigSchema.extend({ name: z.string().min(2).max(32) }), nfsConfigSchema.extend({ name: z.string().min(2).max(32) }), smbConfigSchema.extend({ name: z.string().min(2).max(32) }), webdavConfigSchema.extend({ name: z.string().min(2).max(32) }), rcloneConfigSchema.extend({ name: z.string().min(2).max(32) }), sftpConfigSchema.extend({ name: z.string().min(2).max(32) }), ]) .superRefine((value, ctx) => { if (value.backend === "sftp" && !value.skipHostKeyCheck && !value.knownHosts?.trim()) { ctx.addIssue({ code: "custom", message: "Known hosts are required unless host key verification is skipped", path: ["knownHosts"], }); } }); export type FormValues = z.input; type Props = { onSubmit: (values: FormValues) => void; mode?: "create" | "update"; initialValues?: Partial; formId?: string; loading?: boolean; className?: string; readOnly?: boolean; }; 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, mapToContainerUidGid: false }, webdav: { backend: "webdav" as const, port: 80, ssl: false, path: "/webdav" }, rclone: { backend: "rclone" as const, path: "/" }, sftp: { backend: "sftp" as const, port: 22, path: "/", skipHostKeyCheck: false, allowLegacySshRsa: false }, }; export const CreateVolumeForm = ({ onSubmit, mode = "create", initialValues, formId, loading, className }: Props) => { const form = useForm({ resolver: zodResolver(formSchema, undefined, { raw: true }), defaultValues: initialValues || { name: "", backend: "directory", }, resetOptions: { keepDefaultValues: true, keepDirtyValues: false, }, }); const { getValues, watch } = form; const { capabilities } = useSystemInfo(); const scrollToFirstError = useScrollToFormError(); const watchedBackend = watch("backend"); 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(); const { name: _, ...configCandidate } = formValues; const parsedConfig = volumeConfigSchema.safeParse(configCandidate); if (!parsedConfig.success) { setTestMessage({ success: false, message: "Please fix validation errors before testing the connection." }); return; } if ( parsedConfig.data.backend === "nfs" || parsedConfig.data.backend === "smb" || parsedConfig.data.backend === "webdav" || parsedConfig.data.backend === "sftp" ) { testBackendConnection.mutate({ body: { config: parsedConfig.data }, }); } }; return (
( Name Unique identifier for the volume. )} /> ( Backend Choose the storage backend for this volume. )} /> {watchedBackend === "directory" && } {watchedBackend === "nfs" && } {watchedBackend === "webdav" && } {watchedBackend === "smb" && } {watchedBackend === "rclone" && } {watchedBackend === "sftp" && }
{watchedBackend && watchedBackend !== "directory" && watchedBackend !== "rclone" && (
{testMessage && (
{testMessage.message}
)}
)} {mode === "update" && !formId && ( )}
); };