import { arktypeResolver } from "@hookform/resolvers/arktype"; import { COMPRESSION_MODES, repositoryConfigSchema } from "@ironmount/schemas/restic"; import { type } from "arktype"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { cn, slugify } from "~/lib/utils"; import { deepClean } from "~/utils/object"; 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"; export const formSchema = type({ name: "2<=string<=32", compressionMode: type.valueOf(COMPRESSION_MODES).optional(), }).and(repositoryConfigSchema); const cleanSchema = type.pipe((d) => formSchema(deepClean(d))); export type RepositoryFormValues = typeof formSchema.inferIn; type Props = { onSubmit: (values: RepositoryFormValues) => void; mode?: "create" | "update"; initialValues?: Partial; formId?: string; loading?: boolean; className?: string; }; const defaultValuesForType = { local: { backend: "local" as const, compressionMode: "auto" as const }, s3: { backend: "s3" as const, compressionMode: "auto" as const }, }; export const CreateRepositoryForm = ({ 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 } = form; const watchedBackend = watch("backend"); const watchedName = watch("name"); useEffect(() => { if (watchedBackend && watchedBackend in defaultValuesForType) { form.reset({ name: watchedName, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType] }); } }, [watchedBackend, watchedName, form]); return (
( Name field.onChange(slugify(e.target.value))} max={32} min={2} disabled={mode === "update"} className={mode === "update" ? "bg-gray-50" : ""} /> Unique identifier for the repository. )} /> ( Backend Choose the storage backend for this repository. )} /> ( Compression Mode Compression mode for backups stored in this repository. )} /> {watchedBackend === "local" && ( ( Path Local filesystem path where the repository will be stored. )} /> )} {watchedBackend === "s3" && ( <> ( Endpoint S3-compatible endpoint URL. )} /> ( Bucket S3 bucket name for storing backups. )} /> ( Access Key ID S3 access key ID for authentication. )} /> ( Secret Access Key S3 secret access key for authentication. )} /> )} {mode === "update" && ( )} ); };