Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 1 | /** |
| 2 | * @license |
Ben Rohlfs | 94fcbbc | 2022-05-27 10:45:03 +0200 | [diff] [blame] | 3 | * Copyright 2015 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 5 | */ |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 6 | import '../../shared/gr-account-chip/gr-account-chip'; |
| 7 | import '../../shared/gr-button/gr-button'; |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 8 | import '../../shared/gr-icon/gr-icon'; |
Milutin Kristofic | c43c58a | 2021-08-12 16:12:08 +0200 | [diff] [blame] | 9 | import '../../shared/gr-vote-chip/gr-vote-chip'; |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 10 | import {LitElement, html} from 'lit'; |
Frank Borden | 42c1a45 | 2022-08-11 16:27:20 +0200 | [diff] [blame] | 11 | import {customElement, property, state} from 'lit/decorators.js'; |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 12 | |
Ben Rohlfs | 669433f | 2020-08-14 17:09:25 +0200 | [diff] [blame] | 13 | import { |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 14 | ChangeInfo, |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 15 | AccountInfo, |
| 16 | ApprovalInfo, |
Dhruv Srivastava | cded6de | 2021-04-12 18:51:40 +0200 | [diff] [blame] | 17 | AccountDetailInfo, |
Milutin Kristofic | c43c58a | 2021-08-12 16:12:08 +0200 | [diff] [blame] | 18 | isDetailedLabelInfo, |
| 19 | LabelInfo, |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 20 | } from '../../../types/common'; |
Milutin Kristofic | d21f860 | 2022-05-13 11:58:17 +0200 | [diff] [blame] | 21 | import {getApprovalInfo, getCodeReviewLabel} from '../../../utils/label-util'; |
Milutin Kristofic | 85d3f91 | 2021-10-19 18:55:57 +0200 | [diff] [blame] | 22 | import {sortReviewers} from '../../../utils/attention-set-util'; |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 23 | import {sharedStyles} from '../../../styles/shared-styles'; |
| 24 | import {css} from 'lit'; |
| 25 | import {nothing} from 'lit'; |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 26 | import {fire} from '../../../utils/event-util'; |
| 27 | import {ShowReplyDialogEvent} from '../../../types/events'; |
Milutin Kristofic | 308a414 | 2023-05-10 17:38:02 +0200 | [diff] [blame] | 28 | import {repeat} from 'lit/directives/repeat.js'; |
| 29 | import {accountKey} from '../../../utils/account-util'; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 30 | |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 31 | @customElement('gr-reviewer-list') |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 32 | export class GrReviewerList extends LitElement { |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 33 | @property({type: Object}) change?: ChangeInfo; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 34 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 35 | @property({type: Object}) account?: AccountDetailInfo; |
Dhruv Srivastava | cded6de | 2021-04-12 18:51:40 +0200 | [diff] [blame] | 36 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 37 | @property({type: Boolean, reflect: true}) disabled = false; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 38 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 39 | @property({type: Boolean}) mutable = false; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 40 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 41 | @property({type: Boolean, attribute: 'reviewers-only'}) reviewersOnly = false; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 42 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 43 | @property({type: Boolean, attribute: 'ccs-only'}) ccsOnly = false; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 44 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 45 | @state() displayedReviewers: AccountInfo[] = []; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 46 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 47 | @state() reviewers: AccountInfo[] = []; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 48 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 49 | @state() hiddenReviewerCount?: number; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 50 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 51 | @state() showAllReviewers = false; |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 52 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 53 | static override get styles() { |
| 54 | return [ |
| 55 | sharedStyles, |
| 56 | css` |
| 57 | :host { |
| 58 | display: block; |
| 59 | } |
| 60 | :host([disabled]) { |
| 61 | opacity: 0.8; |
| 62 | pointer-events: none; |
| 63 | } |
| 64 | .container { |
| 65 | display: block; |
| 66 | /* line-height-normal for the chips, 2px for the chip border, spacing-s |
| 67 | for the gap between lines, negative bottom margin for eliminating the |
| 68 | gap after the last line */ |
| 69 | line-height: calc(var(--line-height-normal) + 2px + var(--spacing-s)); |
| 70 | margin-bottom: calc(0px - var(--spacing-s)); |
| 71 | } |
Chris Poucet | 1c71386 | 2022-07-25 13:12:24 +0200 | [diff] [blame] | 72 | .addReviewer gr-icon { |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 73 | color: inherit; |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 74 | } |
| 75 | .controlsContainer { |
| 76 | display: inline-block; |
| 77 | } |
| 78 | gr-button.addReviewer { |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 79 | vertical-align: top; |
Ben Rohlfs | b1b0ac9 | 2022-08-01 14:33:02 +0200 | [diff] [blame] | 80 | --gr-button-padding: var(--spacing-s); |
| 81 | --margin: calc(0px - var(--spacing-s)); |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 82 | } |
| 83 | gr-button { |
| 84 | line-height: var(--line-height-normal); |
| 85 | --gr-button-padding: 0px; |
| 86 | } |
| 87 | gr-account-chip { |
| 88 | line-height: var(--line-height-normal); |
| 89 | vertical-align: top; |
| 90 | display: inline-block; |
| 91 | } |
| 92 | gr-vote-chip { |
| 93 | --gr-vote-chip-width: 14px; |
| 94 | --gr-vote-chip-height: 14px; |
| 95 | } |
Frank Borden | a6f871b | 2024-02-08 17:11:16 +0100 | [diff] [blame] | 96 | .reviewersAndControls { |
| 97 | text-wrap: pretty; |
| 98 | } |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 99 | `, |
| 100 | ]; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 103 | override render() { |
| 104 | this.displayedReviewers = this.computeDisplayedReviewers() ?? []; |
| 105 | this.hiddenReviewerCount = |
| 106 | this.reviewers.length - this.displayedReviewers.length; |
| 107 | return html` |
| 108 | <div class="container"> |
Frank Borden | a6f871b | 2024-02-08 17:11:16 +0100 | [diff] [blame] | 109 | <div class="reviewersAndControls"> |
Milutin Kristofic | 308a414 | 2023-05-10 17:38:02 +0200 | [diff] [blame] | 110 | ${repeat( |
| 111 | this.displayedReviewers, |
| 112 | reviewer => accountKey(reviewer), |
| 113 | reviewer => this.renderAccountChip(reviewer) |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 114 | )} |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 115 | <div class="controlsContainer" ?hidden=${!this.mutable}> |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 116 | <gr-button |
| 117 | link |
| 118 | id="addReviewer" |
| 119 | class="addReviewer" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 120 | @click=${this.handleAddTap} |
| 121 | title=${this.ccsOnly ? 'Add CC' : 'Add reviewer'} |
Ben Rohlfs | b1b0ac9 | 2022-08-01 14:33:02 +0200 | [diff] [blame] | 122 | > |
| 123 | <div> |
| 124 | <gr-icon icon="edit" filled small></gr-icon> |
| 125 | </div> |
Chris Poucet | b8c0639 | 2022-07-08 16:35:43 +0200 | [diff] [blame] | 126 | </gr-button> |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 127 | </div> |
| 128 | </div> |
| 129 | <gr-button |
| 130 | class="hiddenReviewers" |
| 131 | link="" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 132 | ?hidden=${!this.hiddenReviewerCount} |
| 133 | @click=${() => { |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 134 | this.showAllReviewers = true; |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 135 | }} |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 136 | >and ${this.hiddenReviewerCount} more</gr-button |
| 137 | > |
| 138 | </div> |
| 139 | `; |
| 140 | } |
| 141 | |
| 142 | private renderAccountChip(reviewer: AccountInfo) { |
| 143 | const change = this.change; |
| 144 | if (!change) return nothing; |
| 145 | return html` |
| 146 | <gr-account-chip |
| 147 | class="reviewer" |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 148 | .account=${reviewer} |
| 149 | .change=${change} |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 150 | highlightAttention |
Ben Rohlfs | 8003bd3 | 2022-04-05 18:24:42 +0200 | [diff] [blame] | 151 | .vote=${this.computeVote(reviewer)} |
| 152 | .label=${this.computeCodeReviewLabel()} |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 153 | > |
Milutin Kristofic | d21f860 | 2022-05-13 11:58:17 +0200 | [diff] [blame] | 154 | <gr-vote-chip |
| 155 | slot="vote-chip" |
| 156 | .vote=${this.computeVote(reviewer)} |
| 157 | .label=${this.computeCodeReviewLabel()} |
| 158 | circle-shape |
| 159 | ></gr-vote-chip> |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 160 | </gr-account-chip> |
| 161 | `; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 162 | } |
Andrew Bonventre | 78792e8 | 2016-03-04 17:48:22 -0500 | [diff] [blame] | 163 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 164 | private computeVote(reviewer: AccountInfo): ApprovalInfo | undefined { |
| 165 | const codeReviewLabel = this.computeCodeReviewLabel(); |
Milutin Kristofic | c43c58a | 2021-08-12 16:12:08 +0200 | [diff] [blame] | 166 | if (!codeReviewLabel || !isDetailedLabelInfo(codeReviewLabel)) return; |
| 167 | return getApprovalInfo(codeReviewLabel, reviewer); |
| 168 | } |
| 169 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 170 | private computeCodeReviewLabel(): LabelInfo | undefined { |
| 171 | if (!this.change?.labels) return; |
| 172 | return getCodeReviewLabel(this.change.labels); |
Milutin Kristofic | c43c58a | 2021-08-12 16:12:08 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 175 | private computeDisplayedReviewers() { |
| 176 | if (this.change?.owner === undefined) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 177 | return; |
| 178 | } |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 179 | let result: AccountInfo[] = []; |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 180 | const reviewers = this.change.reviewers; |
Ben Rohlfs | 7b71b11 | 2021-02-12 10:36:08 +0100 | [diff] [blame] | 181 | for (const key of Object.keys(reviewers)) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 182 | if (this.reviewersOnly && key !== 'REVIEWER') { |
| 183 | continue; |
| 184 | } |
| 185 | if (this.ccsOnly && key !== 'CC') { |
| 186 | continue; |
| 187 | } |
| 188 | if (key === 'REVIEWER' || key === 'CC') { |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 189 | result = result.concat(reviewers[key]!); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 190 | } |
| 191 | } |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 192 | this.reviewers = result |
| 193 | .filter( |
| 194 | reviewer => reviewer._account_id !== this.change?.owner._account_id |
| 195 | ) |
Milutin Kristofic | 85d3f91 | 2021-10-19 18:55:57 +0200 | [diff] [blame] | 196 | .sort((r1, r2) => sortReviewers(r1, r2, this.change, this.account)); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 197 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 198 | if (this.reviewers.length > 8 && !this.showAllReviewers) { |
| 199 | return this.reviewers.slice(0, 6); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 200 | } else { |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 201 | return this.reviewers; |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Dhruv | e4473ab | 2022-04-04 10:54:30 +0200 | [diff] [blame] | 205 | // private but used in tests |
| 206 | handleAddTap(e: Event) { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 207 | e.preventDefault(); |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 208 | const value = { |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 209 | reviewersOnly: this.reviewersOnly, |
| 210 | ccsOnly: this.ccsOnly, |
Dhruv Srivastava | 4ac17f2 | 2020-09-10 16:21:41 +0200 | [diff] [blame] | 211 | }; |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 212 | fire(this, 'show-reply-dialog', {value}); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 213 | } |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 214 | } |
Dhruv Srivastava | 33a657a | 2021-04-12 20:10:11 +0200 | [diff] [blame] | 215 | |
| 216 | declare global { |
| 217 | interface HTMLElementTagNameMap { |
| 218 | 'gr-reviewer-list': GrReviewerList; |
| 219 | } |
Ben Rohlfs | 6bb9053 | 2023-02-17 18:55:56 +0100 | [diff] [blame] | 220 | interface HTMLElementEventMap { |
| 221 | /** Fired when the "Add reviewer..." button is tapped. */ |
| 222 | 'show-reply-dialog': ShowReplyDialogEvent; |
| 223 | } |
Dhruv Srivastava | 33a657a | 2021-04-12 20:10:11 +0200 | [diff] [blame] | 224 | } |