import { zodResolver } from "@hookform/resolvers/zod"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { Save } from "lucide-react"; 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 { 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 { useScrollToFormError } from "~/client/hooks/use-scroll-to-form-error"; import { COMPRESSION_MODES, azureRepositoryConfigSchema, gcsRepositoryConfigSchema, localRepositoryConfigSchema, r2RepositoryConfigSchema, rcloneRepositoryConfigSchema, restRepositoryConfigSchema, s3RepositoryConfigSchema, sftpRepositoryConfigSchema, } from "@zerobyte/core/restic"; import { Checkbox } from "../../../components/ui/checkbox"; import { LocalRepositoryForm, S3RepositoryForm, R2RepositoryForm, GCSRepositoryForm, AzureRepositoryForm, RcloneRepositoryForm, RestRepositoryForm, SftpRepositoryForm, AdvancedForm, } from "./repository-forms"; import { useServerFn } from "@tanstack/react-start"; import { getServerConstants } from "~/server/lib/functions/server-constants"; import { useSuspenseQuery } from "@tanstack/react-query"; const formBaseFields = { name: z.string().min(2).max(32), compressionMode: z.enum(COMPRESSION_MODES).optional(), }; export const formSchema = z.discriminatedUnion("backend", [ localRepositoryConfigSchema.extend(formBaseFields), s3RepositoryConfigSchema.extend(formBaseFields), r2RepositoryConfigSchema.extend(formBaseFields), gcsRepositoryConfigSchema.extend(formBaseFields), azureRepositoryConfigSchema.extend(formBaseFields), rcloneRepositoryConfigSchema.extend(formBaseFields), restRepositoryConfigSchema.extend(formBaseFields), sftpRepositoryConfigSchema.extend(formBaseFields), ]); export type RepositoryFormValues = z.input; type Props = { onSubmit: (values: RepositoryFormValues) => void; mode?: "create" | "update"; initialValues?: Partial; formId?: string; loading?: boolean; className?: string; }; const defaultValuesForType = (repoBase: string) => ({ local: { backend: "local" as const, compressionMode: "auto" as const, path: repoBase }, 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 getConstants = useServerFn(getServerConstants); const { data: constants } = useSuspenseQuery({ queryKey: ["server-constants"], queryFn: getConstants, }); const form = useForm({ resolver: zodResolver(formSchema, undefined, { raw: true }), 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(); const scrollToFirstError = useScrollToFormError(); 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 recovery key (which you downloaded when creating your account) 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" && } {watchedBackend && watchedBackend !== "local" && } {mode === "update" && ( )} ); };