import { arktypeResolver } from "@hookform/resolvers/arktype"; import { type } from "arktype"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { Save } from "lucide-react"; import { cn } from "~/client/lib/utils"; import { deepClean } from "~/utils/object"; 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 { SecretInput } from "../../../components/ui/secret-input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../components/ui/select"; import { Tooltip, TooltipContent, TooltipTrigger } from "../../../components/ui/tooltip"; import { useSystemInfo } from "~/client/hooks/use-system-info"; import { COMPRESSION_MODES, repositoryConfigSchemaBase } from "~/schemas/restic"; import { Checkbox } from "../../../components/ui/checkbox"; import { LocalRepositoryForm, S3RepositoryForm, R2RepositoryForm, GCSRepositoryForm, AzureRepositoryForm, RcloneRepositoryForm, RestRepositoryForm, SftpRepositoryForm, } from "./repository-forms"; export const formSchema = type({ name: "2<=string<=32", compressionMode: type.valueOf(COMPRESSION_MODES).optional(), }).and(repositoryConfigSchemaBase); 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 }, r2: { backend: "r2" as const, compressionMode: "auto" as const }, gcs: { backend: "gcs" as const, compressionMode: "auto" as const }, azure: { backend: "azure" as const, compressionMode: "auto" as const }, rclone: { backend: "rclone" as const, compressionMode: "auto" as const }, rest: { backend: "rest" as const, compressionMode: "auto" as const }, sftp: { backend: "sftp" as const, compressionMode: "auto" as const, port: 22, skipHostKeyCheck: false }, }; 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, setValue } = form; const watchedBackend = watch("backend"); const watchedIsExistingRepository = watch("isExistingRepository"); const [passwordMode, setPasswordMode] = useState<"default" | "custom">("default"); const { capabilities } = useSystemInfo(); useEffect(() => { form.reset({ name: form.getValues().name, isExistingRepository: form.getValues().isExistingRepository, customPassword: form.getValues().customPassword, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType], }); }, [watchedBackend, form]); return (
( Name field.onChange(e.target.value)} maxLength={32} minLength={2} /> Unique identifier for the repository. )} /> ( Backend Choose the storage backend for this repository. )} /> ( Compression Mode Compression mode for backups stored in this repository. )} /> ( { field.onChange(checked); if (!checked) { setPasswordMode("default"); setValue("customPassword", undefined); } }} />
Import existing repository Check this if the repository already exists at the specified location
)} /> {watchedIsExistingRepository && ( <> Repository Password Choose whether to use Zerobyte's master password or enter a custom password for the existing repository. {passwordMode === "custom" && ( ( Repository Password The password used to encrypt this repository. It will be stored securely. )} /> )} )} {watchedBackend === "local" && } {watchedBackend === "s3" && } {watchedBackend === "r2" && } {watchedBackend === "gcs" && } {watchedBackend === "azure" && } {watchedBackend === "rclone" && } {watchedBackend === "rest" && } {watchedBackend === "sftp" && } {mode === "update" && ( )} ); };