import { arktypeResolver } from "@hookform/resolvers/arktype"; import { type } from "arktype"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { cn, slugify } from "~/client/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"; import { useQuery } from "@tanstack/react-query"; import { Alert, AlertDescription } from "./ui/alert"; import { ExternalLink } from "lucide-react"; import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip"; import { useSystemInfo } from "~/client/hooks/use-system-info"; import { COMPRESSION_MODES, repositoryConfigSchema } from "~/schemas/restic"; import { listRcloneRemotesOptions } from "../api-client/@tanstack/react-query.gen"; 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 }, 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 }, }; 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 { data: rcloneRemotes, isLoading: isLoadingRemotes } = useQuery({ ...listRcloneRemotesOptions(), }); useEffect(() => { form.reset({ name: form.getValues().name, ...defaultValuesForType[watchedBackend as keyof typeof defaultValuesForType], }); }, [watchedBackend, form]); const { capabilities } = useSystemInfo(); 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 === "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. )} /> )} {watchedBackend === "gcs" && ( <> ( Bucket GCS bucket name for storing backups. )} /> ( Project ID Google Cloud project ID. )} /> ( Service Account JSON Service account JSON credentials for authentication. )} /> )} {watchedBackend === "azure" && ( <> ( Container Azure Blob Storage container name for storing backups. )} /> ( Account Name Azure Storage account name. )} /> ( Account Key Azure Storage account key for authentication. )} /> ( Endpoint Suffix (Optional) Custom Azure endpoint suffix (defaults to core.windows.net). )} /> )} {watchedBackend === "rclone" && (!rcloneRemotes || rcloneRemotes.length === 0 ? (

No rclone remotes configured

To use rclone, you need to configure remotes on your host system

View rclone documentation
) : ( <> ( Remote Select the rclone remote configured on your host system. )} /> ( Path Path within the remote where backups will be stored. )} /> ))} {mode === "update" && ( )} ); };