Skip to content

Commit 9feaba1

Browse files
committed
Fixes home view bugs
- handle push/pull actions - add link to pr - control force branches reloading
1 parent f6e0a17 commit 9feaba1

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
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: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { css, html, LitElement, nothing } from 'lit';
44
import { customElement, state } from 'lit/decorators.js';
55
import { when } from 'lit/directives/when.js';
66
import type { GitTrackingState } from '../../../../../git/models/branch';
7+
import { createWebviewCommandLink } from '../../../../../system/webview';
78
import type { GetOverviewBranch, State } from '../../../../home/protocol';
89
import { stateContext } from '../../../home/context';
910
import { branchCardStyles, sectionHeadingStyles } from './branch-section';
@@ -83,11 +84,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
8384

8485
private renderComplete(overview: Overview) {
8586
const activeBranches = overview?.repository?.branches?.active;
86-
if (activeBranches == null) return html`<span>None</span>`;
87+
if (!activeBranches) return html`<span>None</span>`;
8788

8889
return html`
8990
<h3 class="section-heading section-heading--actions">
90-
<span><code-icon icon="repo" class="heading-icon"></code-icon> ${overview!.repository.name}</span>
91+
<span><code-icon icon="repo" class="heading-icon"></code-icon> ${overview.repository.name}</span>
9192
${when(
9293
this._homeState.repositories.openCount > 1,
9394
() =>
@@ -116,13 +117,13 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
116117
<span class="branch-item__name">${name}</span>
117118
</p>
118119
${when(state, () => this.renderBranchStateActions(state, upstream))}
119-
${when(pr, () => {
120+
${when(pr, pr => {
120121
return html` <p class="branch-item__main is-end">
121122
<span class="branch-item__icon">
122-
<pr-icon state=${pr!.state}></pr-icon>
123+
<pr-icon state=${pr.state}></pr-icon>
123124
</span>
124-
<span class="branch-item__name">${pr!.title}</span>
125-
<span class="branch-item__identifier">#${pr!.id}</span>
125+
<span class="branch-item__name">${pr.title}</span>
126+
<a href=${pr.url} class="branch-item__identifier">#${pr.id}</a>
126127
</p>`;
127128
})}
128129
${when(workingTreeState, () => this.renderStatus(workingTreeState, state))}
@@ -148,15 +149,25 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
148149
const forcePushTooltip = upstream?.name ? `Force Push to ${upstream.name}` : 'Force Push';
149150
return html`<div>
150151
<button-container>
151-
<gl-button full appearance="secondary" tooltip=${pullTooltip}
152+
<gl-button
153+
href=${createWebviewCommandLink('gitlens.views.home.pull', 'gitlens.views.home', '')}
154+
full
155+
appearance="secondary"
156+
tooltip=${pullTooltip}
152157
><code-icon icon="gl-repo-pull" slot="prefix"></code-icon> Pull
153158
<gl-tracking-pill
154159
.ahead=${state.ahead}
155160
.behind=${state.behind}
156161
slot="suffix"
157162
></gl-tracking-pill
158163
></gl-button>
159-
<gl-button appearance="secondary" density="compact" tooltip=${forcePushTooltip}
164+
<gl-button
165+
href=${createWebviewCommandLink('gitlens.views.home.push', 'gitlens.views.home', '', {
166+
force: true,
167+
})}
168+
appearance="secondary"
169+
density="compact"
170+
tooltip=${forcePushTooltip}
160171
><code-icon icon="repo-force-push"></code-icon
161172
></gl-button>
162173
</button-container>
@@ -166,7 +177,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
166177
const tooltip = upstream?.name ? `Pull from ${upstream.name}` : 'Pull';
167178
return html`<div>
168179
<button-container>
169-
<gl-button full appearance="secondary" tooltip=${tooltip}
180+
<gl-button
181+
href=${createWebviewCommandLink('gitlens.views.home.pull', 'gitlens.views.home', '')}
182+
full
183+
appearance="secondary"
184+
tooltip=${tooltip}
170185
><code-icon icon="gl-repo-pull" slot="prefix"></code-icon> Pull
171186
<gl-tracking-pill
172187
.ahead=${state.ahead}
@@ -180,7 +195,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
180195
const tooltip = upstream?.name ? `Push to ${upstream.name}` : 'Push';
181196
return html`<div>
182197
<button-container>
183-
<gl-button full appearance="secondary" tooltip=${tooltip}
198+
<gl-button
199+
href=${createWebviewCommandLink('gitlens.views.home.push', 'gitlens.views.home', '')}
200+
full
201+
appearance="secondary"
202+
tooltip=${tooltip}
184203
><code-icon icon="repo-push" slot="prefix"></code-icon> Push
185204
<gl-tracking-pill
186205
.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
@@ -104,6 +104,11 @@ export const branchCardStyles = css`
104104
105105
.branch-item__identifier {
106106
color: var(--vscode-descriptionForeground);
107+
text-decoration: none;
108+
}
109+
110+
.branch-item__identifier:hover {
111+
text-decoration: underline;
107112
}
108113
109114
.branch-item__details {
@@ -171,16 +176,15 @@ export class GlBranchCard extends LitElement {
171176

172177
override render() {
173178
const { name, pr, opened: active, timestamp: date, state, workingTreeState } = this.branch;
174-
175179
return html`
176180
<gl-card class="branch-item" .active=${active}>
177181
<p class="branch-item__main">
178182
<span class="branch-item__icon">${this.renderIcon(this.branch)}</span>
179183
${when(
180184
pr,
181-
() =>
182-
html`<span class="branch-item__name">${pr!.title}</span>
183-
<span class="branch-item__identifier">#${pr!.id}</span>`,
185+
pr =>
186+
html`<span class="branch-item__name">${pr.title} </span
187+
><a href=${pr.url} class="branch-item__identifier">#${pr.id}</a>`,
184188
() => html`<span class="branch-item__name">${name}</span>`,
185189
)}
186190
</p>

src/webviews/home/homeWebview.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GlyphChars } from '../../constants';
55
import type { ContextKeys } from '../../constants.context';
66
import type { HomeTelemetryContext } from '../../constants.telemetry';
77
import type { Container } from '../../container';
8+
import { executeGitCommand } from '../../git/actions';
89
import type { BranchContributorOverview } from '../../git/gitProvider';
910
import type { GitBranch } from '../../git/models/branch';
1011
import { sortBranches } from '../../git/models/branch';
@@ -199,6 +200,26 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
199200

200201
registerCommands(): Disposable[] {
201202
return [
203+
registerCommand(
204+
`${this.host.id}.pull`,
205+
() => {
206+
void executeGitCommand({
207+
command: 'pull',
208+
state: {},
209+
});
210+
},
211+
this,
212+
),
213+
registerCommand(
214+
`${this.host.id}.push`,
215+
args => {
216+
void executeGitCommand({
217+
command: 'push',
218+
state: { flags: args.force ? ['--force'] : undefined },
219+
});
220+
},
221+
this,
222+
),
202223
registerCommand(`${this.host.id}.refresh`, () => this.host.refresh(true), this),
203224
registerCommand(
204225
`${this.host.id}.account.resync`,
@@ -426,6 +447,7 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
426447
}
427448

428449
private onWipChanged(_repo: Repository) {
450+
this._invalidateRepositoryBranches = true;
429451
void this.host.notify(DidChangeRepositoryWip, undefined);
430452
}
431453

@@ -437,16 +459,21 @@ export class HomeWebviewProvider implements WebviewProvider<State, State, HomeWe
437459
return this._repositorySubscription?.repo;
438460
}
439461

440-
private _repositoryBranches: Map<string, { branches: GitBranch[]; worktrees: GitWorktree[] }> = new Map();
441-
private async getBranchesAndWorktrees(repo: Repository, force = false) {
462+
private _invalidateRepositoryBranches = true;
463+
private readonly _repositoryBranches: Map<string, { branches: GitBranch[]; worktrees: GitWorktree[] }> = new Map();
464+
private async getBranchesAndWorktrees(repo: Repository, force = this._invalidateRepositoryBranches) {
442465
if (force || !this._repositoryBranches.has(repo.path)) {
443-
const [branchesResult, worktreesResult] = await Promise.allSettled([
444-
repo.git.getBranches({ filter: b => !b.remote }),
445-
repo.git.getWorktrees(),
466+
const worktrees = (await repo.git.getWorktrees()) ?? [];
467+
const worktreesByBranch = groupWorktreesByBranch(worktrees);
468+
const [branchesResult] = await Promise.allSettled([
469+
repo.git.getBranches({
470+
filter: b => !b.remote,
471+
sort: { current: true, openedWorktreesByBranch: getOpenedWorktreesByBranch(worktreesByBranch) },
472+
}),
446473
]);
447474

448475
const branches = getSettledValue(branchesResult)?.values ?? [];
449-
const worktrees = getSettledValue(worktreesResult) ?? [];
476+
this._invalidateRepositoryBranches = false;
450477
this._repositoryBranches.set(repo.path, { branches: branches, worktrees: worktrees });
451478
}
452479

@@ -542,16 +569,11 @@ async function getOverviewBranches(
542569
container: Container,
543570
options: OverviewFilters,
544571
): Promise<GetOverviewBranches | undefined> {
572+
console.log('try to getOverviewBranches');
545573
if (branches.length === 0) return undefined;
546574

547575
const worktreesByBranch = groupWorktreesByBranch(worktrees);
548576

549-
sortBranches(branches, {
550-
current: true,
551-
orderBy: 'date:desc',
552-
openedWorktreesByBranch: getOpenedWorktreesByBranch(worktreesByBranch),
553-
});
554-
555577
const overviewBranches: GetOverviewBranches = {
556578
active: [],
557579
recent: [],
@@ -571,7 +593,7 @@ async function getOverviewBranches(
571593

572594
const timestamp = branch.date?.getTime();
573595
if (branch.current || wt?.opened) {
574-
prPromises.set(branch.id, branch.getAssociatedPullRequest());
596+
prPromises.set(branch.id, branch.getAssociatedPullRequest({ avatarSize: 16 }));
575597
if (wt != null) {
576598
statusPromises.set(branch.id, wt.getStatus());
577599
}
@@ -678,15 +700,15 @@ async function getOverviewBranches(
678700
const prs = new Map(
679701
getSettledValue(prResults)
680702
?.filter(r => r.status === 'fulfilled')
681-
.map(r => [
682-
r.value[0],
683-
r.value[1]
684-
? {
685-
id: r.value[1].id,
686-
title: r.value[1].title,
687-
state: r.value[1].state,
688-
url: r.value[1].url,
689-
}
703+
.map(({ value: [prId, pr] }) => [
704+
prId,
705+
pr
706+
? ({
707+
id: pr.id,
708+
title: pr.title,
709+
state: pr.state,
710+
url: pr.url,
711+
} satisfies GetOverviewBranch['pr'])
690712
: undefined,
691713
]),
692714
);

0 commit comments

Comments
 (0)