Skip to content

Commit 0edc19a

Browse files
committed
Fixes home view bugs
- handle push/pull actions - add link to pr - control force branches reloading
1 parent 219d620 commit 0edc19a

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed

src/system/webview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { WebviewIds, WebviewViewIds } from '../constants.views';
22

3-
export function createWebviewCommandLink(
3+
export function createWebviewCommandLink<T>(
44
command: `${WebviewIds | WebviewViewIds}.${string}`,
55
webviewId: WebviewIds | WebviewViewIds,
66
webviewInstanceId: string | undefined,
7+
args?: T,
78
): string {
89
return `command:${command}?${encodeURIComponent(
9-
JSON.stringify({ webview: webviewId, webviewInstance: webviewInstanceId } satisfies WebviewContext),
10+
JSON.stringify({ webview: webviewId, webviewInstance: webviewInstanceId, ...args } satisfies WebviewContext),
1011
)}`;
1112
}
1213

src/webviews/apps/plus/home/components/active-work.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { customElement, property, state } from 'lit/decorators.js';
55
import { repeat } from 'lit/directives/repeat.js';
66
import { when } from 'lit/directives/when.js';
77
import type { GitTrackingState } from '../../../../../git/models/branch';
8+
import { createWebviewCommandLink } from '../../../../../system/webview';
89
import type { GetOverviewBranch, RepositoryChoice, State } from '../../../../home/protocol';
910
import { stateContext } from '../../../home/context';
1011
import { GlElement } from '../../../shared/components/element';
@@ -75,14 +76,14 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
7576

7677
private renderComplete(overview: Overview) {
7778
const activeBranches = overview?.repository?.branches?.active;
78-
if (activeBranches == null) return html`<span>None</span>`;
79+
if (!activeBranches) return html`<span>None</span>`;
7980

8081
return html`
8182
<h3 class="section-heading section-heading--actions">
8283
<span>Active (${activeBranches.length})</span>
83-
${when(overview!.choices, () => html`<gl-change-repo .options=${overview!.choices}></gl-change-repo>`)}
84+
${when(overview.choices, () => html`<gl-change-repo .options=${overview.choices}></gl-change-repo>`)}
8485
</h3>
85-
${activeBranches.map(branch => this.renderRepoBranchCard(overview!.repository.name, branch))}
86+
${repeat(activeBranches, branch => this.renderRepoBranchCard(overview.repository.name, branch))}
8687
`;
8788
}
8889

@@ -105,14 +106,14 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
105106
</p>
106107
${when(workingTreeState, () => this.renderStatus(workingTreeState, state))}
107108
</gl-card>
108-
${when(pr, () => {
109+
${when(pr, pr => {
109110
return html`<gl-card class="branch-item">
110111
<p class="branch-item__main">
111112
<span class="branch-item__icon">
112-
<pr-icon state=${pr!.state}></pr-icon>
113+
<pr-icon state=${pr.state}></pr-icon>
113114
</span>
114-
<span class="branch-item__name">${pr!.title}</span>
115-
<span class="branch-item__identifier">#${pr!.id}</span>
115+
<span class="branch-item__name">${pr.title}</span>
116+
<a href=${pr.url} class="branch-item__identifier">#${pr.id}</a>
116117
</p>
117118
</gl-card>`;
118119
})}
@@ -137,15 +138,25 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
137138
const forcePushTooltip = upstream?.name ? `Force Push to ${upstream.name}` : 'Force Push';
138139
return html`<div>
139140
<button-container>
140-
<gl-button full appearance="secondary" tooltip=${pullTooltip}
141+
<gl-button
142+
href=${createWebviewCommandLink('gitlens.views.home.pull', 'gitlens.views.home', '')}
143+
full
144+
appearance="secondary"
145+
tooltip=${pullTooltip}
141146
><code-icon icon="gl-repo-pull" slot="prefix"></code-icon> Pull
142147
<gl-tracking-pill
143148
.ahead=${state.ahead}
144149
.behind=${state.behind}
145150
slot="suffix"
146151
></gl-tracking-pill
147152
></gl-button>
148-
<gl-button appearance="secondary" density="compact" tooltip=${forcePushTooltip}
153+
<gl-button
154+
href=${createWebviewCommandLink('gitlens.views.home.push', 'gitlens.views.home', '', {
155+
force: true,
156+
})}
157+
appearance="secondary"
158+
density="compact"
159+
tooltip=${forcePushTooltip}
149160
><code-icon icon="repo-force-push"></code-icon
150161
></gl-button>
151162
</button-container>
@@ -155,7 +166,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
155166
const tooltip = upstream?.name ? `Pull from ${upstream.name}` : 'Pull';
156167
return html`<div>
157168
<button-container>
158-
<gl-button full appearance="secondary" tooltip=${tooltip}
169+
<gl-button
170+
href=${createWebviewCommandLink('gitlens.views.home.pull', 'gitlens.views.home', '')}
171+
full
172+
appearance="secondary"
173+
tooltip=${tooltip}
159174
><code-icon icon="gl-repo-pull" slot="prefix"></code-icon> Pull
160175
<gl-tracking-pill
161176
.ahead=${state.ahead}
@@ -169,7 +184,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
169184
const tooltip = upstream?.name ? `Push to ${upstream.name}` : 'Push';
170185
return html`<div>
171186
<button-container>
172-
<gl-button full appearance="secondary" tooltip=${tooltip}
187+
<gl-button
188+
href=${createWebviewCommandLink('gitlens.views.home.push', 'gitlens.views.home', '')}
189+
full
190+
appearance="secondary"
191+
tooltip=${tooltip}
173192
><code-icon icon="repo-push" slot="prefix"></code-icon> Push
174193
<gl-tracking-pill
175194
.ahead=${state.ahead}

src/webviews/apps/plus/home/components/branch-section.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ export const branchCardStyles = css`
9292
9393
.branch-item__identifier {
9494
color: var(--vscode-descriptionForeground);
95+
text-decoration: none;
96+
}
97+
98+
.branch-item__identifier:hover {
99+
text-decoration: underline;
95100
}
96101
97102
.branch-item__details {
@@ -159,16 +164,15 @@ export class GlBranchCard extends LitElement {
159164

160165
override render() {
161166
const { name, pr, opened: active, timestamp: date, state, workingTreeState } = this.branch;
162-
163167
return html`
164168
<gl-card class="branch-item" .active=${active}>
165169
<p class="branch-item__main">
166170
<span class="branch-item__icon">${this.renderIcon(this.branch)}</span>
167171
${when(
168172
pr,
169-
() =>
170-
html`<span class="branch-item__name">${pr!.title}</span>
171-
<span class="branch-item__identifier">#${pr!.id}</span>`,
173+
pr =>
174+
html`<span class="branch-item__name">${pr.title} </span
175+
><a href=${pr.url} class="branch-item__identifier">#${pr.id}</a>`,
172176
() => html`<span class="branch-item__name">${name}</span>`,
173177
)}
174178
</p>

src/webviews/home/homeWebview.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getAvatarUriFromGravatarEmail } from '../../avatars';
44
import type { ContextKeys } from '../../constants.context';
55
import type { WebviewTelemetryContext } from '../../constants.telemetry';
66
import type { Container } from '../../container';
7+
import { executeGitCommand } from '../../git/actions';
78
import type { BranchContributorOverview } from '../../git/gitProvider';
89
import type { GitBranch } from '../../git/models/branch';
910
import { sortBranches } from '../../git/models/branch';
@@ -179,6 +180,26 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
179180

180181
registerCommands(): Disposable[] {
181182
return [
183+
registerCommand(
184+
`${this.host.id}.pull`,
185+
() => {
186+
void executeGitCommand({
187+
command: 'pull',
188+
state: {},
189+
});
190+
},
191+
this,
192+
),
193+
registerCommand(
194+
`${this.host.id}.push`,
195+
args => {
196+
void executeGitCommand({
197+
command: 'push',
198+
state: { flags: args.force ? ['--force'] : undefined },
199+
});
200+
},
201+
this,
202+
),
182203
registerCommand(`${this.host.id}.refresh`, () => this.host.refresh(true), this),
183204
registerCommand(
184205
`${this.host.id}.account.resync`,
@@ -420,6 +441,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
420441
}
421442

422443
private onWipChanged(_repo: Repository) {
444+
this._invalidateRepositoryBranches = true;
423445
void this.host.notify(DidChangeRepositoryWip, undefined);
424446
}
425447

@@ -431,16 +453,21 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
431453
return this._repositorySubscription?.repo;
432454
}
433455

434-
private _repositoryBranches: Map<string, { branches: GitBranch[]; worktrees: GitWorktree[] }> = new Map();
435-
private async getBranchesAndWorktrees(repo: Repository, force = false) {
456+
private _invalidateRepositoryBranches = true;
457+
private readonly _repositoryBranches: Map<string, { branches: GitBranch[]; worktrees: GitWorktree[] }> = new Map();
458+
private async getBranchesAndWorktrees(repo: Repository, force = this._invalidateRepositoryBranches) {
436459
if (force || !this._repositoryBranches.has(repo.path)) {
437-
const [branchesResult, worktreesResult] = await Promise.allSettled([
438-
repo.git.getBranches({ filter: b => !b.remote }),
439-
repo.git.getWorktrees(),
460+
const worktrees = (await repo.git.getWorktrees()) ?? [];
461+
const worktreesByBranch = groupWorktreesByBranch(worktrees);
462+
const [branchesResult] = await Promise.allSettled([
463+
repo.git.getBranches({
464+
filter: b => !b.remote,
465+
sort: { current: true, openedWorktreesByBranch: getOpenedWorktreesByBranch(worktreesByBranch) },
466+
}),
440467
]);
441468

442469
const branches = getSettledValue(branchesResult)?.values ?? [];
443-
const worktrees = getSettledValue(worktreesResult) ?? [];
470+
this._invalidateRepositoryBranches = false;
444471
this._repositoryBranches.set(repo.path, { branches: branches, worktrees: worktrees });
445472
}
446473

@@ -537,16 +564,11 @@ async function getOverviewBranches(
537564
container: Container,
538565
options: OverviewFilters,
539566
): Promise<GetOverviewBranches | undefined> {
567+
console.log('try to getOverviewBranches');
540568
if (branches.length === 0) return undefined;
541569

542570
const worktreesByBranch = groupWorktreesByBranch(worktrees);
543571

544-
sortBranches(branches, {
545-
current: true,
546-
orderBy: 'date:desc',
547-
openedWorktreesByBranch: getOpenedWorktreesByBranch(worktreesByBranch),
548-
});
549-
550572
const overviewBranches: GetOverviewBranches = {
551573
active: [],
552574
recent: [],
@@ -566,7 +588,7 @@ async function getOverviewBranches(
566588

567589
const timestamp = branch.date?.getTime();
568590
if (branch.current || wt?.opened) {
569-
prPromises.set(branch.id, branch.getAssociatedPullRequest());
591+
prPromises.set(branch.id, branch.getAssociatedPullRequest({ avatarSize: 16 }));
570592
if (wt != null) {
571593
statusPromises.set(branch.id, wt.getStatus());
572594
}
@@ -673,15 +695,15 @@ async function getOverviewBranches(
673695
const prs = new Map(
674696
getSettledValue(prResults)
675697
?.filter(r => r.status === 'fulfilled')
676-
.map(r => [
677-
r.value[0],
678-
r.value[1]
679-
? {
680-
id: r.value[1].id,
681-
title: r.value[1].title,
682-
state: r.value[1].state,
683-
url: r.value[1].url,
684-
}
698+
.map(({ value: [prId, pr] }) => [
699+
prId,
700+
pr
701+
? ({
702+
id: pr.id,
703+
title: pr.title,
704+
state: pr.state,
705+
url: pr.url,
706+
} satisfies GetOverviewBranch['pr'])
685707
: undefined,
686708
]),
687709
);

0 commit comments

Comments
 (0)