import { zodResolver } from "@hookform/resolvers/zod"; import { CheckIcon, ChevronsUpDown, HelpCircle, Plus, X } from "lucide-react"; import Link from "next/link"; import { useEffect, useMemo } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { GitlabIcon } from "@/components/icons/data-tools-icons"; import { AlertBlock } from "@/components/shared/alert-block"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, } from "@/components/ui/command"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; import { api } from "@/utils/api"; const GitlabProviderSchema = z.object({ buildPath: z.string().min(1, "Path is required").default("/"), repository: z .object({ repo: z.string().min(1, "Repo is required"), owner: z.string().min(1, "Owner is required"), gitlabPathNamespace: z.string().min(1), id: z.number().nullable(), }) .required(), branch: z.string().min(1, "Branch is required"), gitlabId: z.string().min(1, "Gitlab Provider is required"), watchPaths: z.array(z.string()).optional(), enableSubmodules: z.boolean().default(false), }); type GitlabProvider = z.infer; interface Props { applicationId: string; } export const SaveGitlabProvider = ({ applicationId }: Props) => { const { data: gitlabProviders } = api.gitlab.gitlabProviders.useQuery(); const { data, refetch } = api.application.one.useQuery({ applicationId }); const { mutateAsync, isLoading: isSavingGitlabProvider } = api.application.saveGitlabProvider.useMutation(); const form = useForm({ defaultValues: { buildPath: "/", repository: { owner: "", repo: "", gitlabPathNamespace: "", id: null, }, gitlabId: "", branch: "", enableSubmodules: false, }, resolver: zodResolver(GitlabProviderSchema), }); const repository = form.watch("repository"); const gitlabId = form.watch("gitlabId"); const gitlabUrl = useMemo(() => { const url = gitlabProviders?.find( (provider) => provider.gitlabId === gitlabId, )?.gitlabUrl; const gitlabUrl = url?.replace(/\/$/, ""); return gitlabUrl || "https://gitlab.com"; }, [gitlabId, gitlabProviders]); const { data: repositories, isLoading: isLoadingRepositories, error, } = api.gitlab.getGitlabRepositories.useQuery( { gitlabId, }, { enabled: !!gitlabId, }, ); const { data: branches, fetchStatus, status, } = api.gitlab.getGitlabBranches.useQuery( { owner: repository?.owner, repo: repository?.repo, id: repository?.id || 0, gitlabId: gitlabId, }, { enabled: !!repository?.owner && !!repository?.repo && !!gitlabId, }, ); useEffect(() => { if (data) { form.reset({ branch: data.gitlabBranch || "", repository: { repo: data.gitlabRepository || "", owner: data.gitlabOwner || "", gitlabPathNamespace: data.gitlabPathNamespace || "", id: data.gitlabProjectId, }, buildPath: data.gitlabBuildPath || "/", gitlabId: data.gitlabId || "", watchPaths: data.watchPaths || [], enableSubmodules: data.enableSubmodules ?? false, }); } }, [form.reset, data?.applicationId, form]); const onSubmit = async (data: GitlabProvider) => { await mutateAsync({ gitlabBranch: data.branch, gitlabRepository: data.repository.repo, gitlabOwner: data.repository.owner, gitlabBuildPath: data.buildPath, gitlabId: data.gitlabId, applicationId, gitlabProjectId: data.repository.id, gitlabPathNamespace: data.repository.gitlabPathNamespace, watchPaths: data.watchPaths || [], enableSubmodules: data.enableSubmodules, }) .then(async () => { toast.success("Service Provider Saved"); await refetch(); }) .catch(() => { toast.error("Error saving the gitlab provider"); }); }; return (
{error && {error?.message}}
( Gitlab Account )} /> (
Repository {field.value.owner && field.value.repo && ( View Repository )}
{isLoadingRepositories && ( Loading Repositories.... )} No repositories found. {repositories && repositories.length === 0 && ( No repositories found. )} {repositories?.map((repo) => { return ( { form.setValue("repository", { owner: repo.owner.username as string, repo: repo.name, id: repo.id, gitlabPathNamespace: repo.url, }); form.setValue("branch", ""); }} > {repo.name} {repo.owner.username} ); })} {form.formState.errors.repository && (

Repository is required

)}
)} /> ( Branch {status === "loading" && fetchStatus === "fetching" && ( Loading Branches.... )} {!repository?.owner && ( Select a repository )} No branch found. {branches?.map((branch) => ( { form.setValue("branch", branch.name); }} > {branch.name} ))} )} /> ( Build Path )} /> (
Watch Paths

Add paths to watch for changes. When files in these paths change, a new deployment will be triggered.

{field.value?.map((path, index) => ( {path} { const newPaths = [...(field.value || [])]; newPaths.splice(index, 1); field.onChange(newPaths); }} /> ))}
{ if (e.key === "Enter") { e.preventDefault(); const input = e.currentTarget; const path = input.value.trim(); if (path) { field.onChange([...(field.value || []), path]); input.value = ""; } } }} />
)} /> ( Enable Submodules )} />
); };