Skip to content

fix: Combine api calls in commits tab #3837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions src/pages/RepoPage/CommitsTab/CommitsTab.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Suspense, useEffect, useRef, useState } from 'react'
import { Suspense, useState } from 'react'
import { useHistory, useParams } from 'react-router-dom'

import { useBranchHasCommits } from 'services/branches/useBranchHasCommits'
import { useLocationParams } from 'services/navigation/useLocationParams'
import { ALL_BRANCHES, useNavLinks } from 'services/navigation/useNavLinks'
import { useRepoOverview } from 'services/repo'
Expand Down Expand Up @@ -60,37 +59,12 @@ function CommitsTab() {
branchParam ?? overview?.defaultBranch
)

const initialRenderDone = useRef(false)

const { data: branchHasCommits } = useBranchHasCommits({
provider,
owner,
repo,
branch: selectedBranch,
opts: {
suspense: true,
enabled: !initialRenderDone.current,
},
})

useEffect(() => {
if (
branchHasCommits === false &&
selectedBranch !== ALL_BRANCHES &&
!initialRenderDone.current
) {
initialRenderDone.current = true
setSelectedBranch(ALL_BRANCHES)
}
}, [branchHasCommits, selectedBranch])

const { updateParams, selectedStates, setSelectedStates, search } =
useControlParams()

const {
branchList,
branchSelectorProps,
branchesFetchNextPage,
branchListIsFetching,
branchListHasNextPage,
branchListFetchNextPage,
Expand Down Expand Up @@ -139,7 +113,6 @@ function CommitsTab() {
}}
onLoadMore={() => {
if (branchListHasNextPage) {
branchesFetchNextPage()
branchListFetchNextPage()
}
}}
Expand Down
40 changes: 8 additions & 32 deletions src/pages/RepoPage/CommitsTab/hooks/useCommitsTabBranchSelector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from 'react'
import { useParams } from 'react-router-dom'

import { useBranch, query as useBranchQuery } from 'services/branches/useBranch'
import { useBranches } from 'services/branches/useBranches'

interface URLParams {
Expand All @@ -23,7 +22,7 @@ export const useCommitsTabBranchSelector = ({
const [branchSearchTerm, setBranchSearchTerm] = useState('')

const {
data: branchList,
data: branchesData,
isFetching: branchListIsFetching,
hasNextPage: branchListHasNextPage,
fetchNextPage: branchListFetchNextPage,
Expand All @@ -37,39 +36,17 @@ export const useCommitsTabBranchSelector = ({
},
})

const { data: branchesData, fetchNextPage: branchesFetchNextPage } =
useBranches({
repo,
owner,
provider,
filters: {},
opts: { suspense: false },
})

const selectedBranch = passedBranch ?? defaultBranch

const { data: searchBranchValue } = useBranch({
provider,
owner,
repo,
branch: selectedBranch,
opts: {
queryKey: [
'GetCommitsTabSelectedBranch',
provider,
owner,
repo,
selectedBranch,
useBranchQuery,
],
enabled: !!selectedBranch,
},
})
// Check if the selected branch exists in the branches list
const branchExists = branchesData?.branches?.some(
(branch) => branch?.name === selectedBranch
)

let selection = searchBranchValue?.branch?.name
let selection = selectedBranch
if (isAllCommits) {
selection = 'All branches'
} else if (!selection) {
} else if (!branchExists) {
selection = 'Select branch'
}

Expand All @@ -80,8 +57,7 @@ export const useCommitsTabBranchSelector = ({
value: selection,
},
currentBranchSelected: selection,
branchesFetchNextPage,
branchList: branchList?.branches?.map((branch) => branch?.name) || [],
branchList: branchesData?.branches?.map((branch) => branch?.name) || [],
branchListIsFetching,
branchListHasNextPage,
branchListFetchNextPage,
Expand Down
Loading