TypeScript
fix(mobile): branch search finds remote and space-typed branches (#13454)
Bilal Bakr84c436bcf714
diff --git a/apps/mobile/src/features/threads/new-task-context-presentation.test.ts b/apps/mobile/src/features/threads/new-task-context-presentation.test.tsindex 3c81d823121..f48ba92396e 100644--- a/apps/mobile/src/features/threads/new-task-context-presentation.test.ts+++ b/apps/mobile/src/features/threads/new-task-context-presentation.test.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "vite-plus/test";import {+ filterNewTaskBranches,resolveNewTaskBranchWorktreePath,resolveNewTaskBranchLabel,resolveNewTaskLocalWorkspaceSelection,@@ -126,3 +127,26 @@ describe("resolveNewTaskBranchLabel", () => {).toBe("Choose branch");});});++describe("filterNewTaskBranches", () => {+ const branches = [+ { name: "main", isRemote: false },+ { name: "Feature/Login-Page", isRemote: false },+ { name: "origin/fix/remote-only", isRemote: true },+ ];+ const search = (query: string) =>+ filterNewTaskBranches(branches, query).map((branch) => branch.name);++ it("ignores case in both the query and the branch name", () => {+ expect(search("feature/login")).toEqual(["Feature/Login-Page"]);+ expect(search("MAIN")).toEqual(["main"]);+ });++ it("keeps remote-only branches searchable", () => {+ expect(search("remote-only")).toEqual(["origin/fix/remote-only"]);+ });++ it("matches a typed space against the dash a branch name uses", () => {+ expect(search(" login page ")).toEqual(["Feature/Login-Page"]);+ });+});diff --git a/apps/mobile/src/features/threads/new-task-context-presentation.ts b/apps/mobile/src/features/threads/new-task-context-presentation.tsindex 99eee3ea48a..87c9b2595f4 100644--- a/apps/mobile/src/features/threads/new-task-context-presentation.ts+++ b/apps/mobile/src/features/threads/new-task-context-presentation.ts@@ -1,3 +1,5 @@+import { sanitizeNewRefName } from "@t3tools/shared/git";+type WorkspaceMode = "local" | "worktree";export function resolveNewTaskWorkspaceLabel(input: {@@ -81,3 +83,13 @@ export function shouldCheckoutNewTaskBranch(input: {}): boolean {return input.workspaceMode === "local" && !input.branchIsCurrent && !input.branchWorktreePath;}++export function filterNewTaskBranches<T extends { readonly name: string }>(+ branches: ReadonlyArray<T>,+ rawQuery: string,+): ReadonlyArray<T> {+ const query = sanitizeNewRefName(rawQuery).toLowerCase();+ return query.length === 0+ ? branches+ : branches.filter((branch) => branch.name.toLowerCase().includes(query));+}diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsxindex 4d069bca9d0..61b77afffba 100644--- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx+++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx@@ -18,6 +18,7 @@ import {T3_PROJECT_FILE_NAME,ThreadId,} from "@t3tools/contracts";+import { sanitizeNewRefName } from "@t3tools/shared/git";import { resolveProjectSettings } from "@t3tools/shared/projectSettings";import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";import * as Arr from "effect/Array";@@ -92,6 +93,7 @@ import {} from "../../state/legacy-plan-mode";import { useLegacyPlanModeState } from "./use-legacy-plan-mode-enabled";import {+ filterNewTaskBranches,resolveNewTaskBranchWorktreePath,resolveNewTaskLocalWorkspaceSelection,} from "./new-task-context-presentation";@@ -129,6 +131,9 @@ export function branchBadgeLabel(input: {if (input.branch.worktreePath && input.branch.worktreePath !== input.project?.workspaceRoot) {return "worktree";}+ if (input.branch.isRemote) {+ return "remote";+ }if (input.branch.isDefault) {return "default";}@@ -626,7 +631,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {}replaceComposerDraftAttachments(selectedProjectDraftKey, []);}, [selectedProjectDraftKey]);- const debouncedBranchQuery = useDebouncedValue(branchQuery, BRANCH_SEARCH_DEBOUNCE_MS);+ const branchSearchQuery = sanitizeNewRefName(branchQuery);+ const debouncedBranchQuery = useDebouncedValue(branchSearchQuery, BRANCH_SEARCH_DEBOUNCE_MS);const branchTarget = useMemo(() => ({environmentId: selectedProject?.environmentId ?? null,@@ -637,7 +643,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {[debouncedBranchQuery, selectedProject?.environmentId, selectedProject?.workspaceRoot],);const branchState = usePaginatedBranches(branchTarget);- const branchSearchIsDebouncing = branchQuery.trim() !== debouncedBranchQuery.trim();+ const branchSearchIsDebouncing = branchSearchQuery !== debouncedBranchQuery;const branchesLoading =branchSearchIsDebouncing || (branchState.isPending && branchState.data === null);const branchesFetchingNextPage = branchState.isFetchingNextPage;@@ -669,17 +675,10 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {);const currentCheckoutBranchName = projectGitStatus.data?.refName ?? null;- const filteredBranches = useMemo(() => {- const query = branchQuery.trim().toLowerCase();- if (query.length === 0) {- return availableBranches;- }-- return pipe(- availableBranches,- Arr.filter((branch) => branch.name.toLowerCase().includes(query)),- );- }, [availableBranches, branchQuery]);+ const filteredBranches = useMemo(+ () => filterNewTaskBranches(allBranchRefs, branchQuery),+ [allBranchRefs, branchQuery],+ );// The composer's draft follows the project it will be sent to: switching// mid-compose keeps the same draft and moves it, so typed text follows thediff --git a/apps/web/src/components/BranchToolbar.logic.ts b/apps/web/src/components/BranchToolbar.logic.tsindex 5b046ed6a68..85d306f919a 100644--- a/apps/web/src/components/BranchToolbar.logic.ts+++ b/apps/web/src/components/BranchToolbar.logic.ts@@ -6,10 +6,12 @@ import type {WorktreeSubmodules,} from "@t3tools/contracts";import * as Schema from "effect/Schema";+import { sanitizeNewRefName } from "@t3tools/shared/git";import { toSortableTimestamp } from "../lib/threadSort";export {dedupeRemoteBranchesWithLocalMatches,deriveLocalBranchNameFromRemoteRef,+ sanitizeNewRefName,} from "@t3tools/shared/git";export interface EnvironmentOption {@@ -275,19 +277,6 @@ export function resolveBranchSelectionTarget(input: {};}-// Git rejects ASCII space and the ASCII control characters (tab, newline and-// friends) in ref names, so the picker's "Create new ref" entry can only fail-// for a typed name like "new branch". Replacing runs of those with a dash makes-// the name usable without reimplementing check-ref-format: names invalid for-// other reasons still surface the git error. Only the whitespace git actually-// rejects is replaced — git accepts U+00A0 and friends, and rewriting those-// would silently create a ref the user never asked for. Case and existing-// dashes are left alone, since ref names are case sensitive and consecutive-// dashes are valid.-export function sanitizeNewRefName(rawName: string): string {- return rawName.trim().replace(/[ \t\n\r\f\v]+/g, "-");-}-export function shouldIncludeBranchPickerItem(input: {itemValue: string;normalizedQuery: string;diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.tsindex 0c727955175..61d18314179 100644--- a/packages/shared/src/git.ts+++ b/packages/shared/src/git.ts@@ -265,6 +265,19 @@ function deriveLocalBranchNameCandidatesFromRemoteRef(return [...candidates];}+// Git rejects ASCII space and the ASCII control characters (tab, newline and+// friends) in ref names, so the picker's "Create new ref" entry can only fail+// for a typed name like "new branch". Replacing runs of those with a dash makes+// the name usable without reimplementing check-ref-format: names invalid for+// other reasons still surface the git error. Only the whitespace git actually+// rejects is replaced — git accepts U+00A0 and friends, and rewriting those+// would silently create a ref the user never asked for. Case and existing+// dashes are left alone, since ref names are case sensitive and consecutive+// dashes are valid.+export function sanitizeNewRefName(rawName: string): string {+ return rawName.trim().replace(/[ \t\n\r\f\v]+/g, "-");+}+/*** Hide `origin/*` remote refs when a matching local refName already exists.*/