Initialise comments tab only when selected

Change 305003 added the behaviour of showing All Comments when opening
the comments tab and no unresolved comments being present.
This behaviour was being triggered whenever _commentThreads was
updated(which happened when interacting with any comment) and resulted
in the tab switching.
Switch tabs only when user explicitly selects the tab and not on
commentThreads updates.

Change-Id: I89229e407ee7b66c27d56b26977b6111d5c8adfa
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
index c6d2ee0..f05d665 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view.ts
@@ -503,6 +503,9 @@
   _activeTabs: string[] = [PrimaryTab.FILES, SecondaryTab.CHANGE_LOG];
 
   @property({type: Boolean})
+  unresolvedOnly = false;
+
+  @property({type: Boolean})
   _showAllRobotComments = false;
 
   @property({type: Boolean})
@@ -819,6 +822,15 @@
       if (tabName) break;
       target = target?.parentElement as HTMLElement | null;
     } while (target);
+
+    if (tabName === PrimaryTab.COMMENT_THREADS) {
+      // Show unresolved threads by default only if they are present
+      const hasUnresolvedThreads =
+        (this._commentThreads ?? []).filter(thread => isUnresolved(thread))
+          .length > 0;
+      if (hasUnresolvedThreads) this.unresolvedOnly = true;
+    }
+
     this.reporting.reportInteraction('show-tab', {
       tabName,
       src: 'paper-tab-click',
@@ -900,13 +912,6 @@
     return false;
   }
 
-  _computeShowUnresolved(threads?: CommentThread[]) {
-    // If all threads are resolved and the Comments Tab is opened then show
-    // all threads instead
-    if (!threads?.length) return true;
-    return threads.filter(thread => isUnresolved(thread)).length > 0;
-  }
-
   _robotCommentCountPerPatchSet(threads: CommentThread[]) {
     return threads.reduce((robotCommentCountMap, thread) => {
       const comments = thread.comments;
diff --git a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_html.ts b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_html.ts
index 6025268..069b720 100644
--- a/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_html.ts
+++ b/polygerrit-ui/app/elements/change/gr-change-view/gr-change-view_html.ts
@@ -570,7 +570,7 @@
           logged-in="[[_loggedIn]]"
           comment-tab-state="[[_tabState.commentTab]]"
           only-show-robot-comments-with-human-reply=""
-          unresolved-only="[[_computeShowUnresolved(_commentThreads)]]"
+          unresolved-only="[[unresolvedOnly]]"
           show-comment-context
         ></gr-thread-list>
       </template>