Skip to content

Commit f21ed40

Browse files
committed
Branch selection: If the branch is not part of the listing, remove it
If the selected branch is not part of the response for the branch listings API, then assume it doesn’t exist and unselect it
1 parent 7b748ac commit f21ed40

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

apps/desktop/src/lib/bootstrap/deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export function initDependencies(args: {
191191

192192
const gitService = new GitService(backend, clientState.backendApi);
193193
const baseBranchService = new BaseBranchService(clientState.backendApi);
194-
const branchService = new BranchService(clientState['backendApi']);
194+
const branchService = new BranchService(clientState['backendApi'], uiState);
195195
const cherryApplyService = new CherryApplyService(clientState.backendApi);
196196
const remotesService = new RemotesService(backend);
197197
const hooksService = new HooksService(clientState.backendApi);

apps/desktop/src/lib/branches/branchService.svelte.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { invalidatesList, providesList, ReduxTag } from '$lib/state/tags';
2+
import { retainBranchSelectionInBranchesView, type UiState } from '$lib/state/uiState.svelte';
23
import { InjectionToken } from '@gitbutler/core/context';
34
import type { BranchListing, BranchListingDetails } from '$lib/branches/branchListing';
45
import type { BackendApi, ClientState } from '$lib/state/clientState.svelte';
@@ -8,8 +9,8 @@ export const BRANCH_SERVICE = new InjectionToken<BranchService>('BranchService')
89
export class BranchService {
910
private api: ReturnType<typeof injectEndpoints>;
1011

11-
constructor(backendApi: BackendApi) {
12-
this.api = injectEndpoints(backendApi);
12+
constructor(backendApi: BackendApi, uiState: UiState) {
13+
this.api = injectEndpoints(backendApi, uiState);
1314
}
1415

1516
list(projectId: string) {
@@ -25,13 +26,18 @@ export class BranchService {
2526
}
2627
}
2728

28-
function injectEndpoints(api: ClientState['backendApi']) {
29+
function injectEndpoints(api: ClientState['backendApi'], uiState: UiState) {
2930
return api.injectEndpoints({
3031
endpoints: (build) => ({
3132
listBranches: build.query<BranchListing[], { projectId: string }>({
3233
extraOptions: { command: 'list_branches' },
3334
query: (args) => args,
34-
providesTags: [providesList(ReduxTag.BranchListing)]
35+
providesTags: [providesList(ReduxTag.BranchListing)],
36+
transformResponse: (response: BranchListing[], _, { projectId }) => {
37+
const branches = response.map((branch) => branch.name);
38+
retainBranchSelectionInBranchesView(uiState, projectId, branches);
39+
return response;
40+
}
3541
}),
3642
branchDetails: build.query<BranchListingDetails, { projectId: string; branchName: string }>({
3743
extraOptions: { command: 'get_branch_listing_details' },

apps/desktop/src/lib/state/uiState.svelte.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ export function updateStalePrSelection(uiState: UiState, projectId: string, prs:
528528
}
529529
}
530530

531-
export function updateStaleStackSelectionInBranchesView(
531+
export function updateStaleBranchSelectionInBranchesView(
532532
uiState: UiState,
533533
projectId: string,
534534
deletedBranches: string[]
@@ -539,3 +539,15 @@ export function updateStaleStackSelectionInBranchesView(
539539
projectState.branchesSelection.set({});
540540
}
541541
}
542+
543+
export function retainBranchSelectionInBranchesView(
544+
uiState: UiState,
545+
projectId: string,
546+
branches: string[]
547+
) {
548+
const projectState = uiState.project(projectId);
549+
const selection = projectState.branchesSelection.current;
550+
if (selection.branchName && !branches.includes(selection.branchName)) {
551+
projectState.branchesSelection.set({});
552+
}
553+
}

apps/desktop/src/lib/upstream/upstreamIntegrationService.svelte.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { invalidatesList, providesList, ReduxTag } from '$lib/state/tags';
2-
import { updateStaleStackSelectionInBranchesView, type UiState } from '$lib/state/uiState.svelte';
2+
import { updateStaleBranchSelectionInBranchesView, type UiState } from '$lib/state/uiState.svelte';
33
import { InjectionToken } from '@gitbutler/core/context';
44
import { isDefined } from '@gitbutler/ui/utils/typeguards';
55
import type { StackService } from '$lib/stacks/stackService.svelte';
@@ -103,7 +103,7 @@ function injectEndpoints(api: ClientState['backendApi'], uiState: UiState) {
103103
invalidatesList(ReduxTag.BranchListing)
104104
],
105105
transformResponse: (response: IntegrationOutcome, _, { projectId }) => {
106-
updateStaleStackSelectionInBranchesView(uiState, projectId, response.deletedBranches);
106+
updateStaleBranchSelectionInBranchesView(uiState, projectId, response.deletedBranches);
107107
return response;
108108
}
109109
}),

0 commit comments

Comments
 (0)