Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix issue #8327: Unable to delete the entire default branch name in input box (#8329)

Robert Brennan81ba80dde06d
diff --git a/frontend/src/components/features/home/repo-selection-form.tsx b/frontend/src/components/features/home/repo-selection-form.tsx
index 2b09040cdc..cce9b14b07 100644
--- a/frontend/src/components/features/home/repo-selection-form.tsx
+++ b/frontend/src/components/features/home/repo-selection-form.tsx
@@ -30,6 +30,8 @@ export function RepositorySelectionForm({
const [selectedBranch, setSelectedBranch] = React.useState<Branch | null>(
null,
);
+ // Add a ref to track if the branch was manually cleared by the user
+ const branchManuallyClearedRef = React.useRef<boolean>(false);
const {
data: repositories,
isLoading: isLoadingRepositories,
@@ -52,13 +54,14 @@ export function RepositorySelectionForm({
const debouncedSearchQuery = useDebounce(searchQuery, 300);
const { data: searchedRepos } = useSearchRepositories(debouncedSearchQuery);
- // Auto-select main or master branch if it exists
+ // Auto-select main or master branch if it exists, but only if the branch wasn't manually cleared
React.useEffect(() => {
if (
branches &&
branches.length > 0 &&
!selectedBranch &&
- !isLoadingBranches
+ !isLoadingBranches &&
+ !branchManuallyClearedRef.current // Only auto-select if not manually cleared
) {
// Look for main or master branch
const mainBranch = branches.find((branch) => branch.name === "main");
@@ -71,7 +74,7 @@ export function RepositorySelectionForm({
setSelectedBranch(masterBranch);
}
}
- }, [branches, selectedBranch, isLoadingBranches]);
+ }, [branches, isLoadingBranches, selectedBranch]);
// We check for isSuccess because the app might require time to render
// into the new conversation screen after the conversation is created.
@@ -97,11 +100,14 @@ export function RepositorySelectionForm({
if (selectedRepo) onRepoSelection(selectedRepo.full_name);
setSelectedRepository(selectedRepo || null);
setSelectedBranch(null); // Reset branch selection when repo changes
+ branchManuallyClearedRef.current = false; // Reset the flag when repo changes
};
const handleBranchSelection = (key: React.Key | null) => {
const selectedBranchObj = branches?.find((branch) => branch.name === key);
setSelectedBranch(selectedBranchObj || null);
+ // Reset the manually cleared flag when a branch is explicitly selected
+ branchManuallyClearedRef.current = false;
};
const handleRepoInputChange = (value: string) => {
@@ -116,8 +122,15 @@ export function RepositorySelectionForm({
};
const handleBranchInputChange = (value: string) => {
- if (value === "") {
+ // Clear the selected branch if the input is empty or contains only whitespace
+ // This fixes the issue where users can't delete the entire default branch name
+ if (value === "" || value.trim() === "") {
setSelectedBranch(null);
+ // Set the flag to indicate that the branch was manually cleared
+ branchManuallyClearedRef.current = true;
+ } else {
+ // Reset the flag when the user starts typing again
+ branchManuallyClearedRef.current = false;
}
};
diff --git a/frontend/tests/repo-selection-form.test.tsx b/frontend/tests/repo-selection-form.test.tsx
new file mode 100644
index 0000000000..642b933620
--- /dev/null
+++ b/frontend/tests/repo-selection-form.test.tsx
@@ -0,0 +1,130 @@
+import { describe, it, expect, vi, beforeEach } from "vitest";
+import { render, screen, fireEvent } from "@testing-library/react";
+import { RepositorySelectionForm } from "../src/components/features/home/repo-selection-form";
+import { useUserRepositories } from "../src/hooks/query/use-user-repositories";
+import { useRepositoryBranches } from "../src/hooks/query/use-repository-branches";
+import { useCreateConversation } from "../src/hooks/mutation/use-create-conversation";
+import { useIsCreatingConversation } from "../src/hooks/use-is-creating-conversation";
+
+// Mock the hooks
+vi.mock("../src/hooks/query/use-user-repositories");
+vi.mock("../src/hooks/query/use-repository-branches");
+vi.mock("../src/hooks/mutation/use-create-conversation");
+vi.mock("../src/hooks/use-is-creating-conversation");
+vi.mock("react-i18next", () => ({
+ useTranslation: () => ({
+ t: (key: string) => key,
+ }),
+}));
+
+describe("RepositorySelectionForm", () => {
+ const mockOnRepoSelection = vi.fn();
+
+ beforeEach(() => {
+ vi.resetAllMocks();
+
+ // Mock the hooks with default values
+ (useUserRepositories as any).mockReturnValue({
+ data: [
+ { id: "1", full_name: "test/repo1" },
+ { id: "2", full_name: "test/repo2" }
+ ],
+ isLoading: false,
+ isError: false,
+ });
+
+ (useRepositoryBranches as any).mockReturnValue({
+ data: [
+ { name: "main" },
+ { name: "develop" }
+ ],
+ isLoading: false,
+ isError: false,
+ });
+
+ (useCreateConversation as any).mockReturnValue({
+ mutate: vi.fn(),
+ isPending: false,
+ isSuccess: false,
+ });
+
+ (useIsCreatingConversation as any).mockReturnValue(false);
+ });
+
+ it("should clear selected branch when input is empty", async () => {
+ render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
+
+ // First select a repository to enable the branch dropdown
+ const repoDropdown = screen.getByTestId("repository-dropdown");
+ fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
+
+ // Get the branch dropdown and verify it's enabled
+ const branchDropdown = screen.getByTestId("branch-dropdown");
+ expect(branchDropdown).not.toBeDisabled();
+
+ // Simulate deleting all text in the branch input
+ fireEvent.change(branchDropdown, { target: { value: "" } });
+
+ // Verify the branch input is cleared (no selected branch)
+ expect(branchDropdown).toHaveValue("");
+ });
+
+ it("should clear selected branch when input contains only whitespace", async () => {
+ render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
+
+ // First select a repository to enable the branch dropdown
+ const repoDropdown = screen.getByTestId("repository-dropdown");
+ fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
+
+ // Get the branch dropdown and verify it's enabled
+ const branchDropdown = screen.getByTestId("branch-dropdown");
+ expect(branchDropdown).not.toBeDisabled();
+
+ // Simulate entering only whitespace in the branch input
+ fireEvent.change(branchDropdown, { target: { value: " " } });
+
+ // Verify the branch input is cleared (no selected branch)
+ expect(branchDropdown).toHaveValue("");
+ });
+
+ it("should keep branch empty after being cleared even with auto-selection", async () => {
+ render(<RepositorySelectionForm onRepoSelection={mockOnRepoSelection} />);
+
+ // First select a repository to enable the branch dropdown
+ const repoDropdown = screen.getByTestId("repository-dropdown");
+ fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
+
+ // Get the branch dropdown and verify it's enabled
+ const branchDropdown = screen.getByTestId("branch-dropdown");
+ expect(branchDropdown).not.toBeDisabled();
+
+ // The branch should be auto-selected to "main" initially
+ expect(branchDropdown).toHaveValue("main");
+
+ // Simulate deleting all text in the branch input
+ fireEvent.change(branchDropdown, { target: { value: "" } });
+
+ // Verify the branch input is cleared (no selected branch)
+ expect(branchDropdown).toHaveValue("");
+
+ // Trigger a re-render by changing something else
+ fireEvent.change(repoDropdown, { target: { value: "test/repo2" } });
+ fireEvent.change(repoDropdown, { target: { value: "test/repo1" } });
+
+ // The branch should be auto-selected to "main" again after repo change
+ expect(branchDropdown).toHaveValue("main");
+
+ // Clear it again
+ fireEvent.change(branchDropdown, { target: { value: "" } });
+
+ // Verify it stays empty
+ expect(branchDropdown).toHaveValue("");
+
+ // Simulate a component update without changing repos
+ // This would normally trigger the useEffect if our fix wasn't working
+ fireEvent.blur(branchDropdown);
+
+ // Verify it still stays empty
+ expect(branchDropdown).toHaveValue("");
+ });
+});
\ No newline at end of file