import { GithubIcon } from "@/components/icons/data-tools-icons"; 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"; import { zodResolver } from "@hookform/resolvers/zod"; import { CheckIcon, ChevronsUpDown, HelpCircle, X } from "lucide-react"; import Link from "next/link"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const GithubProviderSchema = z.object({ composePath: z.string().min(1), repository: z .object({ repo: z.string().min(1, "Repo is required"), owner: z.string().min(1, "Owner is required"), }) .required(), branch: z.string().min(1, "Branch is required"), githubId: z.string().min(1, "Github Provider is required"), watchPaths: z.array(z.string()).optional(), triggerType: z.enum(["push", "tag"]).default("push"), enableSubmodules: z.boolean().default(false), }); type GithubProvider = z.infer; interface Props { composeId: string; } export const SaveGithubProviderCompose = ({ composeId }: Props) => { const { data: githubProviders } = api.github.githubProviders.useQuery(); const { data, refetch } = api.compose.one.useQuery({ composeId }); const { mutateAsync, isLoading: isSavingGithubProvider } = api.compose.update.useMutation(); const form = useForm({ defaultValues: { composePath: "./docker-compose.yml", repository: { owner: "", repo: "", }, githubId: "", branch: "", watchPaths: [], triggerType: "push", enableSubmodules: false, }, resolver: zodResolver(GithubProviderSchema), }); const repository = form.watch("repository"); const githubId = form.watch("githubId"); const triggerType = form.watch("triggerType"); const { data: repositories, isLoading: isLoadingRepositories } = api.github.getGithubRepositories.useQuery( { githubId, }, { enabled: !!githubId, }, ); const { data: branches, fetchStatus, status, } = api.github.getGithubBranches.useQuery( { owner: repository?.owner, repo: repository?.repo, githubId, }, { enabled: !!repository?.owner && !!repository?.repo && !!githubId, }, ); useEffect(() => { if (data) { form.reset({ branch: data.branch || "", repository: { repo: data.repository || "", owner: data.owner || "", }, composePath: data.composePath, githubId: data.githubId || "", watchPaths: data.watchPaths || [], triggerType: data.triggerType || "push", enableSubmodules: data.enableSubmodules ?? false, }); } }, [form.reset, data?.composeId, form]); const onSubmit = async (data: GithubProvider) => { await mutateAsync({ branch: data.branch, repository: data.repository.repo, composeId, owner: data.repository.owner, composePath: data.composePath, githubId: data.githubId, sourceType: "github", composeStatus: "idle", watchPaths: data.watchPaths, enableSubmodules: data.enableSubmodules, triggerType: data.triggerType, }) .then(async () => { toast.success("Service Provided Saved"); await refetch(); }) .catch(() => { toast.error("Error saving the Github provider"); }); }; return (
( Github Account )} /> (
Repository {field.value.owner && field.value.repo && ( View Repository )}
{isLoadingRepositories && ( Loading Repositories.... )} No repositories found. {repositories?.map((repo) => ( { form.setValue("repository", { owner: repo.owner.login as string, repo: repo.name, }); form.setValue("branch", ""); }} > {repo.name} {repo.owner.login} ))} {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} ))} )} /> ( Compose Path )} /> (
Trigger Type

Choose when to trigger deployments: on push to the selected branch or when a new tag is created.

)} /> {triggerType === "push" && ( (
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); form.setValue("watchPaths", newPaths); }} /> ))}
{ if (e.key === "Enter") { e.preventDefault(); const input = e.currentTarget; const value = input.value.trim(); if (value) { const newPaths = [ ...(field.value || []), value, ]; form.setValue("watchPaths", newPaths); input.value = ""; } } }} />
)} /> )} ( Enable Submodules )} />
); };