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 { cn } from "@/lib/utils"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import { CheckIcon, ChevronsUpDown } from "lucide-react"; 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"), }); type GithubProvider = z.infer; interface Props { composeId: string; } export const SaveGithubProviderCompose = ({ composeId }: Props) => { 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: "", }, branch: "", }, resolver: zodResolver(GithubProviderSchema), }); const repository = form.watch("repository"); const { data: repositories, isLoading: isLoadingRepositories } = api.admin.getRepositories.useQuery(); const { data: branches, fetchStatus, status, } = api.admin.getBranches.useQuery( { owner: repository?.owner, repo: repository?.repo, }, { enabled: !!repository?.owner && !!repository?.repo }, ); useEffect(() => { if (data) { form.reset({ branch: data.branch || "", repository: { repo: data.repository || "", owner: data.owner || "", }, composePath: data.composePath, }); } }, [form.reset, data, form]); const onSubmit = async (data: GithubProvider) => { console.log(data); await mutateAsync({ branch: data.branch, repository: data.repository.repo, composeId: composeId, owner: data.repository.owner, sourceType: "github", composePath: data.composePath, }) .then(async () => { toast.success("Service Provided Saved"); await refetch(); }) .catch(() => { toast.error("Error to save the github provider"); }); }; return (
( 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} ))} {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 )} />
); };