@@ -24,6 +24,7 @@ import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
2424import { writeTextToClipboard } from "../hooks/useCopyToClipboard" ;
2525import { readLocalApi } from "../localApi" ;
2626import { useOpenPrLink } from "../lib/openPullRequestLink" ;
27+ import { shouldLoadNextBranchPageAfterScroll } from "../state/paginatedBranches" ;
2728import { usePaginatedBranches } from "../state/queries" ;
2829import { useProject , useThread } from "../state/entities" ;
2930import { useEnvironmentQuery } from "../state/query" ;
@@ -35,6 +36,7 @@ import { parsePullRequestReference } from "../pullRequestReference";
3536import { getSourceControlPresentation } from "../sourceControlPresentation" ;
3637import {
3738 deriveLocalBranchNameFromRemoteRef ,
39+ resolveBranchTriggerLabel ,
3840 resolveBranchToolbarPrBranch ,
3941 resolveBranchSelectionTarget ,
4042 resolveBranchToolbarValue ,
@@ -83,40 +85,6 @@ function toBranchActionErrorMessage(error: unknown): string {
8385 return error instanceof Error ? error . message : "An error occurred." ;
8486}
8587
86- function getBranchTriggerLabel ( input : {
87- activeWorktreePath : string | null ;
88- effectiveEnvMode : "local" | "worktree" ;
89- resolvedActiveBranch : string | null ;
90- resolvedActiveBranchIsRemote : boolean | null ;
91- startFromOrigin : boolean ;
92- reuseBaseBranch : boolean ;
93- } ) : string {
94- const {
95- activeWorktreePath,
96- effectiveEnvMode,
97- resolvedActiveBranch,
98- resolvedActiveBranchIsRemote,
99- startFromOrigin,
100- reuseBaseBranch,
101- } = input ;
102- if ( ! resolvedActiveBranch ) {
103- return "Select ref" ;
104- }
105- // Reused base branch is checked out as-is (Tim #15); otherwise "From X" for
106- // new worktree branches, with optional origin/ prefix (upstream #4680).
107- if ( effectiveEnvMode === "worktree" && ! activeWorktreePath ) {
108- if ( reuseBaseBranch ) {
109- return resolvedActiveBranch ;
110- }
111- const baseRef =
112- startFromOrigin && resolvedActiveBranchIsRemote === false
113- ? `origin/${ resolvedActiveBranch } `
114- : resolvedActiveBranch ;
115- return `From ${ baseRef } ` ;
116- }
117- return resolvedActiveBranch ;
118- }
119-
12088export function BranchToolbarBranchSelector ( {
12189 className,
12290 environmentId,
@@ -268,7 +236,7 @@ export function BranchToolbarBranchSelector({
268236 const refs = branchRefState . refs ;
269237 const hasNextPage =
270238 branchRefState . data ?. nextCursor !== null && branchRefState . data ?. nextCursor !== undefined ;
271- const isFetchingNextPage = branchRefState . isPending && branchRefState . data !== null ;
239+ const isFetchingNextPage = branchRefState . isFetchingNextPage ;
272240 const isInitialBranchesLoadPending = branchRefState . isPending && branchRefState . data === null ;
273241 const currentGitBranch =
274242 branchStatusQuery . data ?. refName ?? refs . find ( ( refName ) => refName . current ) ?. name ?? null ;
@@ -543,19 +511,16 @@ export function BranchToolbarBranchSelector({
543511 // ---------------------------------------------------------------------------
544512 // Combobox / list plumbing
545513 // ---------------------------------------------------------------------------
546- const handleOpenChange = useCallback (
547- ( open : boolean ) => {
548- setIsBranchMenuOpen ( open ) ;
549- if ( ! open ) {
550- setBranchQuery ( "" ) ;
551- return ;
552- }
553- branchRefState . refresh ( ) ;
554- } ,
555- [ branchRefState . refresh ] ,
556- ) ;
557-
558514 const branchListScrollElementRef = useRef < HTMLElement | null > ( null ) ;
515+ const previousBranchListScrollTopRef = useRef < number | null > ( null ) ;
516+ const handleOpenChange = useCallback ( ( open : boolean ) => {
517+ previousBranchListScrollTopRef . current = null ;
518+ setIsBranchMenuOpen ( open ) ;
519+ if ( ! open ) {
520+ setBranchQuery ( "" ) ;
521+ }
522+ } , [ ] ) ;
523+
559524 const [ showTopBranchScrollFade , setShowTopBranchScrollFade ] = useState ( false ) ;
560525 const [ showBottomBranchScrollFade , setShowBottomBranchScrollFade ] = useState ( false ) ;
561526 const fetchNextBranchPage = useCallback ( ( ) => {
@@ -566,18 +531,24 @@ export function BranchToolbarBranchSelector({
566531 branchRefState . loadNext ( ) ;
567532 } , [ branchRefState . loadNext , hasNextPage , isFetchingNextPage ] ) ;
568533 const maybeFetchNextBranchPage = useCallback ( ( ) => {
569- if ( ! isBranchMenuOpen || ! hasNextPage || isFetchingNextPage ) {
570- return ;
571- }
572-
573534 const scrollElement = branchListScrollElementRef . current ;
574535 if ( ! scrollElement ) {
575536 return ;
576537 }
577538
578- const distanceFromBottom =
579- scrollElement . scrollHeight - scrollElement . scrollTop - scrollElement . clientHeight ;
580- if ( distanceFromBottom > 96 ) {
539+ const previousScrollTop = previousBranchListScrollTopRef . current ;
540+ previousBranchListScrollTopRef . current = scrollElement . scrollTop ;
541+ if (
542+ ! isBranchMenuOpen ||
543+ ! hasNextPage ||
544+ isFetchingNextPage ||
545+ ! shouldLoadNextBranchPageAfterScroll ( {
546+ previousScrollTop,
547+ scrollTop : scrollElement . scrollTop ,
548+ scrollHeight : scrollElement . scrollHeight ,
549+ clientHeight : scrollElement . clientHeight ,
550+ } )
551+ ) {
581552 return ;
582553 }
583554
@@ -627,11 +598,7 @@ export function BranchToolbarBranchSelector({
627598 void branchListRef . current ?. scrollToOffset ?.( { offset : 0 , animated : false } ) ;
628599 } , [ deferredTrimmedBranchQuery , isBranchMenuOpen ] ) ;
629600
630- useEffect ( ( ) => {
631- maybeFetchNextBranchPage ( ) ;
632- } , [ refs . length , maybeFetchNextBranchPage ] ) ;
633-
634- const triggerLabel = getBranchTriggerLabel ( {
601+ const triggerLabel = resolveBranchTriggerLabel ( {
635602 activeWorktreePath,
636603 effectiveEnvMode,
637604 resolvedActiveBranch,
@@ -784,7 +751,7 @@ export function BranchToolbarBranchSelector({
784751 >
785752 < ComboboxTrigger
786753 render = { < Button variant = "ghost" size = "xs" /> }
787- className = "min-w-0 max-w-full shrink text-muted-foreground/70 hover:text-foreground/80"
754+ className = "min-w-0 max-w-full text-muted-foreground/70 hover:text-foreground/80"
788755 disabled = { isInitialBranchesLoadPending || isBranchActionPending }
789756 >
790757 < GitBranchIcon className = "size-3 shrink-0 opacity-70" />
@@ -830,14 +797,10 @@ export function BranchToolbarBranchSelector({
830797 renderItem = { ( { item, index } ) => renderPickerItem ( item , index ) }
831798 estimatedItemSize = { 28 }
832799 drawDistance = { 336 }
833- onEndReached = { ( ) => {
834- if ( hasNextPage && ! isFetchingNextPage ) {
835- fetchNextBranchPage ( ) ;
836- }
837- } }
838800 onLayout = { ( ) => {
839801 updateBranchListScrollFades ( ) ;
840- maybeFetchNextBranchPage ( ) ;
802+ previousBranchListScrollTopRef . current =
803+ branchListScrollElementRef . current ?. scrollTop ?? null ;
841804 } }
842805 onScroll = { ( ) => {
843806 updateBranchListScrollFades ( ) ;
@@ -906,9 +869,8 @@ export function BranchToolbarBranchSelector({
906869 }
907870 />
908871 < TooltipPopup side = "top" className = "max-w-72 whitespace-normal leading-tight" >
909- { reuseBaseBranch
910- ? "Not available when reusing the selected branch."
911- : "Creates the worktree from the latest matching branch on origin instead of your local branch." }
872+ Creates the worktree from the latest matching branch on origin instead of your
873+ local branch.
912874 </ TooltipPopup >
913875 </ Tooltip >
914876 </ div >
0 commit comments