Skip to content

Commit 70b62af

Browse files
committed
Adds branch actions to home
1 parent b4b84c5 commit 70b62af

File tree

5 files changed

+291
-19
lines changed

5 files changed

+291
-19
lines changed

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { GitTrackingState } from '../../../../../git/models/branch';
77
import { createWebviewCommandLink } from '../../../../../system/webview';
88
import type { GetOverviewBranch, State } from '../../../../home/protocol';
99
import { stateContext } from '../../../home/context';
10-
import { branchCardStyles, sectionHeadingStyles } from './branch-section';
10+
import { branchCardStyles, createCommandLink, sectionHeadingStyles } from './branch-section';
1111
import type { Overview, OverviewState } from './overviewState';
1212
import { overviewStateContext } from './overviewState';
1313
import '../../../shared/components/button';
@@ -83,12 +83,13 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
8383
}
8484

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

8990
return html`
9091
<h3 class="section-heading section-heading--actions">
91-
<span><code-icon icon="repo" class="heading-icon"></code-icon> ${overview.repository.name}</span>
92+
<span><code-icon icon="repo" class="heading-icon"></code-icon> ${overview!.repository.name}</span>
9293
${when(
9394
this._homeState.repositories.openCount > 1,
9495
() =>
@@ -102,11 +103,11 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
102103
></span>`,
103104
)}
104105
</h3>
105-
${activeBranches.map(branch => this.renderRepoBranchCard(branch))}
106+
${activeBranches.map(branch => this.renderRepoBranchCard(branch, repo.path))}
106107
`;
107108
}
108109

109-
private renderRepoBranchCard(branch: GetOverviewBranch) {
110+
private renderRepoBranchCard(branch: GetOverviewBranch, repo: string) {
110111
const { name, pr, state, workingTreeState, upstream } = branch;
111112
return html`
112113
<gl-card class="branch-item" active>
@@ -127,10 +128,57 @@ export class GlActiveWork extends SignalWatcher(LitElement) {
127128
</p>`;
128129
})}
129130
${when(workingTreeState, () => this.renderStatus(workingTreeState, state))}
131+
${this.renderActions(branch, repo)}
130132
</gl-card>
131133
`;
132134
}
133135

136+
private renderActions(branch: GetOverviewBranch, repo: string) {
137+
const branchRefs = {
138+
repoPath: repo,
139+
branchId: branch.id,
140+
};
141+
const actions = [];
142+
if (branch.pr) {
143+
actions.push(
144+
html`<action-item
145+
label="Open Pull Request Changes"
146+
icon="request-changes"
147+
href=${createCommandLink('gitlens.home.openPullRequestComparison', branchRefs)}
148+
></action-item>`,
149+
);
150+
actions.push(
151+
html`<action-item
152+
label="Open Pull Request on Remote"
153+
icon="globe"
154+
href=${createCommandLink('gitlens.home.openPullRequestOnRemote', branchRefs)}
155+
></action-item>`,
156+
);
157+
} else {
158+
actions.push(
159+
html`<action-item
160+
label="Create Pull Request..."
161+
icon="git-pull-request-create"
162+
href=${createCommandLink('gitlens.home.createPullRequest', branchRefs)}
163+
></action-item>`,
164+
);
165+
}
166+
167+
// branch actions
168+
actions.push(
169+
html`<action-item
170+
label="Fetch"
171+
icon="gl-repo-fetch"
172+
href=${createCommandLink('gitlens.home.fetch', branchRefs)}
173+
></action-item>`,
174+
);
175+
176+
if (!actions.length) {
177+
return nothing;
178+
}
179+
return html`<action-nav class="branch-item__actions">${actions}</action-nav>`;
180+
}
181+
134182
private renderBranchStateActions(state?: GitTrackingState, upstream?: { name: string; missing: boolean }) {
135183
if (upstream?.missing !== false) {
136184
return html`<div>

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

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { css, html, LitElement, nothing } from 'lit';
22
import { customElement, property } from 'lit/decorators.js';
33
import { when } from 'lit/directives/when.js';
4+
import type { Commands } from '../../../../../constants.commands';
45
import type { GitTrackingState } from '../../../../../git/models/branch';
56
import type { GetOverviewBranch } from '../../../../home/protocol';
67
import '../../../shared/components/code-icon';
@@ -11,6 +12,8 @@ import '../../../shared/components/commit/commit-stats';
1112
import '../../../shared/components/formatted-date';
1213
import '../../../shared/components/pills/tracking';
1314
import '../../../shared/components/rich/pr-icon';
15+
import '../../../shared/components/actions/action-item';
16+
import '../../../shared/components/actions/action-nav';
1417

1518
type OverviewBranch = GetOverviewBranch;
1619

@@ -54,14 +57,17 @@ export class GlSection extends LitElement {
5457
@customElement('gl-branch-section')
5558
export class GlBranchSection extends LitElement {
5659
@property({ type: String }) label!: string;
60+
@property() repo!: string;
5761
@property({ type: Array }) branches!: GetOverviewBranch[];
5862

5963
override render() {
6064
return html`
6165
<gl-section>
6266
<span slot="heading">${this.label}</span>
6367
<span slot="heading-actions"><slot name="heading-actions"></slot></span>
64-
${this.branches.map(branch => html`<gl-branch-card .branch=${branch}></gl-branch-card>`)}
68+
${this.branches.map(
69+
branch => html`<gl-branch-card .repo=${this.repo} .branch=${branch}></gl-branch-card>`,
70+
)}
6571
</gl-section>
6672
`;
6773
}
@@ -80,13 +86,16 @@ export const branchCardStyles = css`
8086
/* background-color: var(--vscode-gitDecoration-untrackedResourceForeground); */
8187
}
8288
83-
/* .branch-item {} */
89+
.branch-item {
90+
position: relative;
91+
}
8492
8593
.branch-item__main {
8694
display: flex;
8795
/* flex-direction: column; */
8896
/* align-items: center; */
8997
gap: 0.4rem;
98+
margin-block-end: 0;
9099
}
91100
92101
.branch-item__icon {
@@ -118,6 +127,7 @@ export const branchCardStyles = css`
118127
/* align-items: center; */
119128
font-size: 0.9em;
120129
color: var(--vscode-descriptionForeground);
130+
margin-block-end: 0;
121131
}
122132
.branch-item__details > * {
123133
margin-block: 0;
@@ -165,15 +175,37 @@ export const branchCardStyles = css`
165175
overflow: hidden;
166176
margin-inline-start: auto;
167177
}
178+
179+
.branch-item__actions {
180+
position: absolute;
181+
right: 0.4rem;
182+
bottom: 0.4rem;
183+
padding: 0.2rem 0.4rem;
184+
background-color: var(--gl-card-background);
185+
}
186+
187+
.branch-item:not(:focus-within):not(:hover) .branch-item__actions {
188+
display: none;
189+
}
168190
`;
169191

170192
@customElement('gl-branch-card')
171193
export class GlBranchCard extends LitElement {
172194
static override styles = branchCardStyles;
173195

196+
@property()
197+
repo!: string;
198+
174199
@property({ type: Object })
175200
branch!: GetOverviewBranch;
176201

202+
get branchRefs() {
203+
return {
204+
repoPath: this.repo,
205+
branchId: this.branch.id,
206+
};
207+
}
208+
177209
override render() {
178210
const { name, pr, opened: active, timestamp: date, state, workingTreeState } = this.branch;
179211
return html`
@@ -207,6 +239,7 @@ export class GlBranchCard extends LitElement {
207239
html`<formatted-date .date=${new Date(date!)} class="branch-item__date"></formatted-date>`,
208240
)}
209241
</div>
242+
${this.renderActions()}
210243
</gl-card>
211244
`;
212245
}
@@ -291,4 +324,73 @@ export class GlBranchCard extends LitElement {
291324

292325
return nothing;
293326
}
327+
328+
private renderActions() {
329+
const actions = [];
330+
if (this.branch.pr) {
331+
actions.push(
332+
html`<action-item
333+
label="Open Pull Request Changes"
334+
icon="request-changes"
335+
href=${this.createCommandLink('gitlens.home.openPullRequestComparison')}
336+
></action-item>`,
337+
);
338+
actions.push(
339+
html`<action-item
340+
label="Open Pull Request on Remote"
341+
icon="globe"
342+
href=${this.createCommandLink('gitlens.home.openPullRequestOnRemote')}
343+
></action-item>`,
344+
);
345+
} else {
346+
actions.push(
347+
html`<action-item
348+
label="Create Pull Request..."
349+
icon="git-pull-request-create"
350+
href=${this.createCommandLink('gitlens.home.createPullRequest')}
351+
></action-item>`,
352+
);
353+
}
354+
if (this.branch.worktree) {
355+
actions.push(
356+
html`<action-item
357+
label="Open Worktree"
358+
icon="browser"
359+
href=${this.createCommandLink('gitlens.home.openWorktree')}
360+
></action-item>`,
361+
);
362+
} else {
363+
actions.push(
364+
html`<action-item
365+
label="Switch to Branch..."
366+
icon="gl-switch"
367+
href=${this.createCommandLink('gitlens.home.switchToBranch')}
368+
></action-item>`,
369+
);
370+
}
371+
372+
// branch actions
373+
actions.push(
374+
html`<action-item
375+
label="Fetch"
376+
icon="gl-repo-fetch"
377+
href=${this.createCommandLink('gitlens.home.fetch')}
378+
></action-item>`,
379+
);
380+
381+
if (!actions.length) {
382+
return nothing;
383+
}
384+
return html`<action-nav class="branch-item__actions">${actions}</action-nav>`;
385+
}
386+
387+
private createCommandLink(command: string) {
388+
return createCommandLink(command, this.branchRefs);
389+
}
390+
}
391+
392+
export function createCommandLink<T>(command: Commands | string, args: T) {
393+
if (args == null) return `command:${command}`;
394+
395+
return `command:${command}?${encodeURIComponent(typeof args === 'string' ? args : JSON.stringify(args))}`;
294396
}

src/webviews/apps/plus/home/components/overview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export class GlOverview extends SignalWatcher(LitElement) {
8989
<div class="repository">
9090
<gl-branch-section
9191
label="Recent (${repository.branches.recent.length})"
92+
.repo=${repository.path}
9293
.branches=${repository.branches.recent}
9394
>
9495
<gl-branch-threshold-filter
@@ -108,6 +109,7 @@ export class GlOverview extends SignalWatcher(LitElement) {
108109
<gl-branch-section
109110
hidden
110111
label="Stale (${repository.branches.stale.length})"
112+
.repo=${repository.path}
111113
.branches=${repository.branches.stale}
112114
></gl-branch-section>
113115
</div>

0 commit comments

Comments
 (0)