diff --git a/apps/studio/components/layouts/AppLayout/EnableBranchingButton/EnableBranchingModal.tsx b/apps/studio/components/layouts/AppLayout/EnableBranchingButton/EnableBranchingModal.tsx index 8a099fcc3ba..9c94ef6e9b2 100644 --- a/apps/studio/components/layouts/AppLayout/EnableBranchingButton/EnableBranchingModal.tsx +++ b/apps/studio/components/layouts/AppLayout/EnableBranchingButton/EnableBranchingModal.tsx @@ -3,6 +3,7 @@ import { useParams } from 'common' import { last } from 'lodash' import { useEffect, useState } from 'react' import { useForm } from 'react-hook-form' +import type { UseFormReturn } from 'react-hook-form' import { toast } from 'sonner' import * as z from 'zod' import Link from 'next/link' @@ -50,7 +51,6 @@ const EnableBranchingModal = () => { const selectedOrg = useSelectedOrganization() const project = useSelectedProject() - // Keep track if the optional GitHub branch is valid const [isGitBranchValid, setIsGitBranchValid] = useState(false) const { @@ -97,19 +97,28 @@ const EnableBranchingModal = () => { const FormSchema = z .object({ productionBranchName: z.string().min(1, 'Production branch name cannot be empty'), - branchName: z.string().optional(), // Optional GitHub branch name + branchName: z.string().optional(), }) .superRefine(async (val, ctx) => { - // Validate optional GitHub branch name only if provided and connection exists - if (val.branchName && val.branchName.length > 0 && githubConnection?.id) { + if (githubConnection && (!val.branchName || val.branchName.length === 0)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: 'GitHub branch is required when a repository is connected.', + path: ['branchName'], + }) + setIsGitBranchValid(false) + return + } + + if (githubConnection && val.branchName && val.branchName.length > 0) { try { await checkGithubBranchValidity({ connectionId: githubConnection.id, branchName: val.branchName, }) - setIsGitBranchValid(true) // Set validity state for UI feedback + setIsGitBranchValid(true) } catch (error) { - setIsGitBranchValid(false) // Set validity state for UI feedback + setIsGitBranchValid(false) ctx.addIssue({ code: z.ZodIssueCode.custom, message: `Unable to find branch "${val.branchName}" in ${repoOwner}/${repoName}`, @@ -117,14 +126,13 @@ const EnableBranchingModal = () => { }) } } else { - // Reset validity state if field is empty or no connection - setIsGitBranchValid(!val.branchName || val.branchName.length === 0) + setIsGitBranchValid(true) } }) const form = useForm>({ mode: 'onBlur', - reValidateMode: 'onChange', // Keep reValidateMode to ensure async validation triggers + reValidateMode: 'onChange', resolver: zodResolver(FormSchema), defaultValues: { productionBranchName: 'main', branchName: '' }, }) @@ -133,8 +141,7 @@ const EnableBranchingModal = () => { const isError = isErrorConnections const isSuccess = isSuccessConnections - // Form is valid if production branch name is set AND (either no git branch name is provided OR it's provided and valid) - const isFormValid = form.formState.isValid && (!form.getValues('branchName') || isGitBranchValid) + const isFormValid = form.formState.isValid const canSubmit = isFormValid && !isCreating && !isChecking const onSubmit = (data: z.infer) => { @@ -153,7 +160,6 @@ const EnableBranchingModal = () => { } }, [form, snap.showEnableBranchingModal]) - // Reset git branch validity when connection changes or modal opens/closes useEffect(() => { setIsGitBranchValid(!form.getValues('branchName') || form.getValues('branchName')?.length === 0) }, [githubConnection?.id, form.getValues('branchName')]) @@ -215,7 +221,6 @@ const EnableBranchingModal = () => { ) : ( <> - {/* Production Branch Name Input */} { )} /> {githubConnection ? ( -
-
-

GitHub Repository

-
- -

- Connected to{' '} - - {repoOwner}/{repoName} - -

- - - -
-
+ <> ( - +
+ +
+ + + {repoOwner}/{repoName} + + + + +
+
{
)} /> - - {/* Display Migration Info if GitHub is connected */} -
-
-
- -
-
-
-

- Migrations are applied from your GitHub repository -

-

- Migration files in your{' '} - ./supabase directory will run on - both Preview Branches and Production when pushing and merging - branches. -

-
-
-
+ ) : (
@@ -322,10 +308,29 @@ const EnableBranchingModal = () => { )} - {/* Billing and Migration Info */}

Please keep in mind the following:

- {/* Dollar Sign Section */} + + {githubConnection && ( +
+
+
+ +
+
+
+

+ Migrations are applied from your GitHub repository +

+

+ Migration files in your ./supabase{' '} + directory will run on both Preview Branches and Production when pushing + and merging branches. +

+
+
+ )} +
@@ -341,7 +346,6 @@ const EnableBranchingModal = () => {

- {/* PITR Notice - Only show if not already enabled */} {!hasPitrEnabled && }
@@ -374,10 +378,7 @@ const EnableBranchingModal = () => { - + ) }