From b5f0b438027b856cf2747134a3948a678faf20de Mon Sep 17 00:00:00 2001 From: Keith Daulton Date: Mon, 27 Mar 2023 18:48:26 -0400 Subject: [PATCH] Updates pull request actions in the focus view - disables "switch to branch" when on the current branch - disables "open worktree" when already in it --- src/plus/webviews/focus/focusWebview.ts | 36 ++++++++++++++++++++-- src/plus/webviews/focus/protocol.ts | 2 ++ .../apps/plus/focus/components/pull-request-row.ts | 21 ++++++++++--- src/webviews/apps/plus/focus/focus.ts | 4 ++- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/plus/webviews/focus/focusWebview.ts b/src/plus/webviews/focus/focusWebview.ts index 544ae02..5bcfb35 100644 --- a/src/plus/webviews/focus/focusWebview.ts +++ b/src/plus/webviews/focus/focusWebview.ts @@ -19,6 +19,7 @@ import { createReference } from '../../../git/models/reference'; import type { GitRemote } from '../../../git/models/remote'; import type { Repository, RepositoryChangeEvent } from '../../../git/models/repository'; import { RepositoryChange, RepositoryChangeComparisonMode } from '../../../git/models/repository'; +import type { GitWorktree } from '../../../git/models/worktree'; import { parseGitRemoteUrl } from '../../../git/parsers/remoteParser'; import type { RichRemoteProvider } from '../../../git/remotes/richRemoteProvider'; import type { Subscription } from '../../../subscription'; @@ -45,6 +46,8 @@ interface RepoWithRichRemote { interface SearchedPullRequestWithRemote extends SearchedPullRequest { repoAndRemote: RepoWithRichRemote; + isCurrentBranch?: boolean; + isCurrentWorktree?: boolean; } export class FocusWebviewProvider implements WebviewProvider { @@ -267,6 +270,8 @@ export class FocusWebviewProvider implements WebviewProvider { const serializedPrs = prs.map(pr => ({ pullRequest: serializePullRequest(pr.pullRequest), reasons: pr.reasons, + isCurrentBranch: pr.isCurrentBranch ?? false, + iscurrentWorktree: pr.isCurrentWorktree ?? false, })); const issues = await this.getMyIssues(connectedRepos); @@ -333,15 +338,42 @@ export class FocusWebviewProvider implements WebviewProvider { } } + private async getWorktreeForPullRequest( + searchedPullRequest: SearchedPullRequestWithRemote, + ): Promise { + const worktree = await this.container.git.getWorktree( + searchedPullRequest.repoAndRemote.remote.repoPath, + w => w.branch === searchedPullRequest.pullRequest.refs!.head.branch, + ); + return worktree; + } + private async getMyPullRequests(richRepos: RepoWithRichRemote[]): Promise { - const allPrs = []; + const allPrs: SearchedPullRequestWithRemote[] = []; for (const richRepo of richRepos) { const { remote } = richRepo; const prs = await this.container.git.getMyPullRequests(remote); if (prs == null) { continue; } - allPrs.push(...prs.filter(pr => pr.reasons.length > 0).map(pr => ({ ...pr, repoAndRemote: richRepo }))); + + for (const pr of prs) { + if (pr.reasons.length === 0) { + continue; + } + const entry: SearchedPullRequestWithRemote = { + ...pr, + repoAndRemote: richRepo, + isCurrentWorktree: false, + isCurrentBranch: false, + }; + const worktree = await this.getWorktreeForPullRequest(entry); + entry.isCurrentWorktree = worktree?.opened === true; + const branch = await richRepo.repo.getBranch(); + entry.isCurrentBranch = branch?.name === entry.pullRequest.refs!.head.branch; + + allPrs.push(entry); + } } function getScore(pr: SearchedPullRequest) { diff --git a/src/plus/webviews/focus/protocol.ts b/src/plus/webviews/focus/protocol.ts index 757d11c..8b92021 100644 --- a/src/plus/webviews/focus/protocol.ts +++ b/src/plus/webviews/focus/protocol.ts @@ -22,6 +22,8 @@ export interface IssueResult extends SearchResultBase { export interface PullRequestResult extends SearchResultBase { pullRequest: PullRequestShape; + isCurrentBranch: boolean; + iscurrentWorktree: boolean; } export interface RepoWithRichProvider { diff --git a/src/webviews/apps/plus/focus/components/pull-request-row.ts b/src/webviews/apps/plus/focus/components/pull-request-row.ts index f6eebc8..c2c9fd0 100644 --- a/src/webviews/apps/plus/focus/components/pull-request-row.ts +++ b/src/webviews/apps/plus/focus/components/pull-request-row.ts @@ -102,14 +102,16 @@ const template = html` @@ -208,6 +210,11 @@ const styles = css` .actions a:active { background-color: var(--vscode-toolbar-activeBackground); } + .actions a[tabindex='-1'] { + opacity: 0.5; + cursor: default; + pointer-events: none; + } .actions a code-icon { font-size: 1.6rem; @@ -275,6 +282,12 @@ export class PullRequestRow extends FASTElement { @observable public checks?: boolean; + @observable + public isCurrentBranch = false; + + @observable + public iscurrentWorktree = false; + @volatile get lastUpdatedDate() { return new Date(this.pullRequest!.date); diff --git a/src/webviews/apps/plus/focus/focus.ts b/src/webviews/apps/plus/focus/focus.ts index 69e440d..9d5ddad 100644 --- a/src/webviews/apps/plus/focus/focus.ts +++ b/src/webviews/apps/plus/focus/focus.ts @@ -154,11 +154,13 @@ export class FocusApp extends App { } else { noneEl.setAttribute('hidden', 'true'); loadingEl.setAttribute('hidden', 'true'); - this.state.pullRequests.forEach(({ pullRequest, reasons }) => { + this.state.pullRequests.forEach(({ pullRequest, reasons, isCurrentBranch, iscurrentWorktree }) => { if (this._prFilter == null || this._prFilter === '' || reasons.includes(this._prFilter)) { const rowEl = document.createElement('pull-request-row') as PullRequestRow; rowEl.pullRequest = pullRequest; rowEl.reasons = reasons; + rowEl.isCurrentBranch = isCurrentBranch; + rowEl.iscurrentWorktree = iscurrentWorktree; tableEl.append(rowEl); }