import { zodResolver } from '@hookform/resolvers/zod' import { GitBranch, RotateCcw, Shield } from 'lucide-react' import { useRef, useState } from 'react' import { useForm } from 'react-hook-form' import toast from 'react-hot-toast' import { AlertDescription_Shadcn_, AlertTitle_Shadcn_, Alert_Shadcn_, Button, CommandEmpty_Shadcn_, CommandGroup_Shadcn_, CommandInput_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, Command_Shadcn_, FormControl_Shadcn_, FormDescription_Shadcn_, FormField_Shadcn_, FormItem_Shadcn_, FormLabel_Shadcn_, Form_Shadcn_, IconCheck, IconChevronDown, Input_Shadcn_, Label_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_, Popover_Shadcn_, Switch, cn, } from 'ui' import * as z from 'zod' import { useBranchUpdateMutation } from 'data/branches/branch-update-mutation' import { useBranchesQuery } from 'data/branches/branches-query' import { useGitHubBranchesQuery } from 'data/integrations/github-branches-query' import { useGitHubConnectionUpdateMutation } from 'data/integrations/github-connection-update-mutation' import type { IntegrationProjectConnection } from 'data/integrations/integrations.types' import { useSelectedOrganization, useSelectedProject } from 'hooks' interface GitHubIntegrationConnectionFormProps { connection: IntegrationProjectConnection } const GitHubIntegrationConnectionForm = ({ connection }: GitHubIntegrationConnectionFormProps) => { const org = useSelectedOrganization() const project = useSelectedProject() const [open, setOpen] = useState(false) const comboBoxRef = useRef(null) const isBranchingEnabled = project?.is_branch_enabled === true || project?.parent_project_ref !== undefined const { data: githubBranches, isLoading: isLoadingBranches } = useGitHubBranchesQuery( { connectionId: Number(connection.id), }, { enabled: isBranchingEnabled } ) const { mutate: updateConnection, isLoading: isUpdatingConnection } = useGitHubConnectionUpdateMutation({ onSuccess: () => toast.success('Successfully updated connection settings'), }) const { mutate: updateBranch, isLoading: isUpdatingProdBranch } = useBranchUpdateMutation({ onSuccess: (data) => { toast.success(`Successfully updated production branch to ${data.git_branch}`) setOpen(false) }, }) const { data: previewBranches } = useBranchesQuery( { projectRef: project?.parentRef }, { enabled: project !== undefined } ) const productionPreviewBranch = previewBranches?.find((branch) => branch.is_default) function onUpdateProductionBranch(branchName: string) { if (!project?.parentRef) return if (!productionPreviewBranch) return updateBranch({ id: productionPreviewBranch.id, projectRef: project.parentRef, branchName: branchName, gitBranch: branchName, }) } const FormSchema = z.object({ supabaseDirectory: z .string() .default(connection.metadata?.supabaseConfig?.supabaseDirectory ?? ''), supabaseChangesOnly: z .boolean() .default(connection.metadata?.supabaseConfig?.supabaseChangesOnly ?? false), }) const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { supabaseDirectory: connection?.metadata?.supabaseConfig?.supabaseDirectory, supabaseChangesOnly: connection?.metadata?.supabaseConfig?.supabaseChangesOnly, }, }) function onSubmit(data: z.infer) { if (org?.id === undefined) return console.error('Org ID is required') updateConnection({ connectionId: connection.id, organizationId: org?.id, workdir: data.supabaseDirectory, supabaseChangesOnly: data.supabaseChangesOnly, }) } return (
{isBranchingEnabled && (
Production branch

All other branches will be treated as Preview branches

{/*
! This should only work if branching is turned on !
*/} No branches found {githubBranches?.map((branch) => { const active = branch.name === productionPreviewBranch?.git_branch return ( { setOpen(false) onUpdateProductionBranch(branch.name) }} >
{active ? ( ) : ( )} {branch.name}
{branch.name === productionPreviewBranch?.git_branch && }
) })}
)}
( Supabase directory Path in your repository where supabase directory for this connection lives.
{ if (event.key === 'Escape') form.reset() }} /> form.reset()} />
)} /> ( { field.onChange(e) form.handleSubmit(onSubmit)() }} />
Supabase changes only Trigger branch creation only when there were changes to supabase{' '} directory.
)} />
) } export default GitHubIntegrationConnectionForm