import { zodResolver } from "@hookform/resolvers/zod"; import { CheckIcon, ChevronsUpDown, 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"; import { BitbucketIcon } 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 BitbucketProviderSchema = 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"), bitbucketId: z.string().min(1, "Bitbucket Provider is required"), watchPaths: z.array(z.string()).optional(), enableSubmodules: z.boolean().default(false), }); type BitbucketProvider = z.infer; interface Props { composeId: string; } export const SaveBitbucketProviderCompose = ({ composeId }: Props) => { const { data: bitbucketProviders } = api.bitbucket.bitbucketProviders.useQuery(); const { data, refetch } = api.compose.one.useQuery({ composeId }); const { mutateAsync, isLoading: isSavingBitbucketProvider } = api.compose.update.useMutation(); const form = useForm({ defaultValues: { composePath: "./docker-compose.yml", repository: { owner: "", repo: "", }, bitbucketId: "", branch: "", watchPaths: [], enableSubmodules: false, }, resolver: zodResolver(BitbucketProviderSchema), }); const repository = form.watch("repository"); const bitbucketId = form.watch("bitbucketId"); const { data: repositories, isLoading: isLoadingRepositories, error, } = api.bitbucket.getBitbucketRepositories.useQuery( { bitbucketId, }, { enabled: !!bitbucketId, }, ); const { data: branches, fetchStatus, status, } = api.bitbucket.getBitbucketBranches.useQuery( { owner: repository?.owner, repo: repository?.repo, bitbucketId, }, { enabled: !!repository?.owner && !!repository?.repo && !!bitbucketId, }, ); useEffect(() => { if (data) { form.reset({ branch: data.bitbucketBranch || "", repository: { repo: data.bitbucketRepository || "", owner: data.bitbucketOwner || "", }, composePath: data.composePath, bitbucketId: data.bitbucketId || "", watchPaths: data.watchPaths || [], enableSubmodules: data.enableSubmodules ?? false, }); } }, [form.reset, data?.composeId, form]); const onSubmit = async (data: BitbucketProvider) => { await mutateAsync({ bitbucketBranch: data.branch, bitbucketRepository: data.repository.repo, bitbucketOwner: data.repository.owner, bitbucketId: data.bitbucketId, composePath: data.composePath, composeId, sourceType: "bitbucket", composeStatus: "idle", watchPaths: data.watchPaths, enableSubmodules: data.enableSubmodules, }) .then(async () => { toast.success("Service Provided Saved"); await refetch(); }) .catch(() => { toast.error("Error saving the Bitbucket provider"); }); }; return (
{error && ( Repositories: {error.message} )}
( Bitbucket 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.username as string, repo: repo.name, }); 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} ))} )} /> ( Compose 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); 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 )} />
); };