From 12eba66bd938c787376d7358e3e111080ccfe3c4 Mon Sep 17 00:00:00 2001
From: Eric Amodio <eamodio@gmail.com>
Date: Wed, 14 Jun 2023 16:22:07 -0400
Subject: [PATCH] Fixes #2738 not unique enough ids

---
 CHANGELOG.md                           |  1 +
 src/views/nodes/autolinkedItemsNode.ts |  2 +-
 src/views/nodes/compareBranchNode.ts   |  6 +-----
 src/views/nodes/compareResultsNode.ts  |  6 +-----
 src/views/nodes/resultsCommitsNode.ts  | 11 +++++------
 src/views/nodes/resultsFilesNode.ts    |  5 ++++-
 src/views/nodes/viewNode.ts            |  7 ++++---
 7 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ff3c00f..d00725d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -75,6 +75,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
 
 ### Fixed
 
+- Fixes [#2738](https://github.com/gitkraken/vscode-gitlens/issues/2738) - Element with id ... is already registered
 - Fixes [#2728](https://github.com/gitkraken/vscode-gitlens/issues/2728) - Submodule commit graph will not open in the panel layout
 - Fixes [#2734](https://github.com/gitkraken/vscode-gitlens/issues/2734) - 🐛 File History: Browse ... not working
 - Fixes [#2671](https://github.com/gitkraken/vscode-gitlens/issues/2671) - Incorrect locale information provided GitLens
diff --git a/src/views/nodes/autolinkedItemsNode.ts b/src/views/nodes/autolinkedItemsNode.ts
index a359f1b..c53309d 100644
--- a/src/views/nodes/autolinkedItemsNode.ts
+++ b/src/views/nodes/autolinkedItemsNode.ts
@@ -29,7 +29,7 @@ export class AutolinkedItemsNode extends ViewNode<ViewsWithCommits> {
 
 		this._instanceId = instanceId++;
 		this.updateContext({ autolinksId: String(this._instanceId) });
-		this._uniqueId = getViewNodeId(`autolinks`, this.context);
+		this._uniqueId = getViewNodeId('autolinks', this.context);
 	}
 
 	override get id(): string {
diff --git a/src/views/nodes/compareBranchNode.ts b/src/views/nodes/compareBranchNode.ts
index 105780d..fcd9f8b 100644
--- a/src/views/nodes/compareBranchNode.ts
+++ b/src/views/nodes/compareBranchNode.ts
@@ -89,7 +89,6 @@ export class CompareBranchNode extends ViewNode<ViewsWithBranches | WorktreesVie
 						},
 					},
 					{
-						id: 'behind',
 						description: pluralize('commit', aheadBehindCounts?.behind ?? 0),
 						expand: false,
 					},
@@ -112,7 +111,6 @@ export class CompareBranchNode extends ViewNode<ViewsWithBranches | WorktreesVie
 						},
 					},
 					{
-						id: 'ahead',
 						description: pluralize('commit', aheadBehindCounts?.ahead ?? 0),
 						expand: false,
 					},
@@ -125,9 +123,7 @@ export class CompareBranchNode extends ViewNode<ViewsWithBranches | WorktreesVie
 					this.compareWithWorkingTree ? '' : this.branch.ref,
 					this.getFilesQuery.bind(this),
 					undefined,
-					{
-						expand: false,
-					},
+					{ expand: false },
 				),
 			];
 		}
diff --git a/src/views/nodes/compareResultsNode.ts b/src/views/nodes/compareResultsNode.ts
index 932165e..df181a5 100644
--- a/src/views/nodes/compareResultsNode.ts
+++ b/src/views/nodes/compareResultsNode.ts
@@ -101,7 +101,6 @@ export class CompareResultsNode extends ViewNode<SearchAndCompareView> {
 						},
 					},
 					{
-						id: 'behind',
 						description: pluralize('commit', aheadBehindCounts?.behind ?? 0),
 						expand: false,
 					},
@@ -122,7 +121,6 @@ export class CompareResultsNode extends ViewNode<SearchAndCompareView> {
 						},
 					},
 					{
-						id: 'ahead',
 						description: pluralize('commit', aheadBehindCounts?.ahead ?? 0),
 						expand: false,
 					},
@@ -135,9 +133,7 @@ export class CompareResultsNode extends ViewNode<SearchAndCompareView> {
 					this._ref.ref,
 					this.getFilesQuery.bind(this),
 					undefined,
-					{
-						expand: false,
-					},
+					{ expand: false },
 				),
 			];
 		}
diff --git a/src/views/nodes/resultsCommitsNode.ts b/src/views/nodes/resultsCommitsNode.ts
index df4b34c..6619d49 100644
--- a/src/views/nodes/resultsCommitsNode.ts
+++ b/src/views/nodes/resultsCommitsNode.ts
@@ -47,16 +47,15 @@ export class ResultsCommitsNode<View extends ViewsWithCommits = ViewsWithCommits
 				query: () => Promise<FilesQueryResults>;
 			};
 		},
-		private readonly _options: {
-			id?: string;
-			description?: string;
-			expand?: boolean;
-		} = undefined!,
+		private readonly _options: { description?: string; expand?: boolean } = undefined!,
 		splatted?: boolean,
 	) {
 		super(GitUri.fromRepoPath(repoPath), view, parent);
 
-		this._uniqueId = getViewNodeId(`results-commits${_options?.id ? `+${_options.id}` : ''}`, this.context);
+		if (_results.direction != null) {
+			this.updateContext({ branchStatusUpstreamType: _results.direction });
+		}
+		this._uniqueId = getViewNodeId('results-commits', this.context);
 		this.limit = this.view.getNodeLastKnownLimit(this);
 
 		this._options = { expand: true, ..._options };
diff --git a/src/views/nodes/resultsFilesNode.ts b/src/views/nodes/resultsFilesNode.ts
index 7dc24ec..d1cca4e 100644
--- a/src/views/nodes/resultsFilesNode.ts
+++ b/src/views/nodes/resultsFilesNode.ts
@@ -44,7 +44,10 @@ export class ResultsFilesNode extends ViewNode<ViewsWithCommits> {
 	) {
 		super(GitUri.fromRepoPath(repoPath), view, parent);
 
-		this._uniqueId = getViewNodeId(`results-files${direction ? `+${direction}` : ''}`, this.context);
+		if (this.direction != null) {
+			this.updateContext({ branchStatusUpstreamType: this.direction });
+		}
+		this._uniqueId = getViewNodeId('results-files', this.context);
 		this._options = { expand: true, ..._options };
 	}
 
diff --git a/src/views/nodes/viewNode.ts b/src/views/nodes/viewNode.ts
index 9391338..97b0899 100644
--- a/src/views/nodes/viewNode.ts
+++ b/src/views/nodes/viewNode.ts
@@ -144,9 +144,10 @@ export function getViewNodeId(type: string, context: AmbientContext): string {
 		uniqueness += `/branch/${context.branch.id}`;
 	}
 	if (context.branchStatus != null) {
-		uniqueness += `/status/${context.branchStatus.upstream ?? '-'}${
-			context.branchStatusUpstreamType ? `/${context.branchStatusUpstreamType}` : ''
-		}`;
+		uniqueness += `/status/${context.branchStatus.upstream ?? '-'}`;
+	}
+	if (context.branchStatusUpstreamType != null) {
+		uniqueness += `/status-direction/${context.branchStatusUpstreamType}`;
 	}
 	if (context.reflog != null) {
 		uniqueness += `/reflog/${context.reflog.sha}+${context.reflog.selector}+${context.reflog.command}+${