|
| 1 | +// unused now |
| 2 | +import { consume } from '@lit/context'; |
| 3 | +import { Task } from '@lit/task'; |
| 4 | +import { css, html } from 'lit'; |
| 5 | +import { customElement, property, state } from 'lit/decorators.js'; |
| 6 | +import { repeat } from 'lit/directives/repeat.js'; |
| 7 | +import { when } from 'lit/directives/when.js'; |
| 8 | +import { IterUtils } from '../../../../../system/iterable'; |
| 9 | +import { pluralize } from '../../../../../system/string'; |
| 10 | +import type { RepoOwner } from '../../../../home/protocol'; |
| 11 | +import { GetRepoOwners } from '../../../../home/protocol'; |
| 12 | +import '../../../shared/components/checkbox/checkbox'; |
| 13 | +import '../../../shared/components/code-icon'; |
| 14 | +import { GlElement } from '../../../shared/components/element'; |
| 15 | +import '../../../shared/components/menu/index'; |
| 16 | +import '../../../shared/components/menu/menu-item'; |
| 17 | +import '../../../shared/components/menu/menu-list'; |
| 18 | +import '../../../shared/components/overlays/popover'; |
| 19 | +import { ipcContext } from '../../../shared/context'; |
| 20 | +import type { HostIpc } from '../../../shared/ipc'; |
| 21 | + |
| 22 | +@customElement('gl-branch-owner-filter') |
| 23 | +export class GlBranchOwnerFilter extends GlElement { |
| 24 | + static override readonly styles = [ |
| 25 | + // should be shared with other filters |
| 26 | + css` |
| 27 | + .owner-filter:focus { |
| 28 | + outline: 1px solid; |
| 29 | + } |
| 30 | + .owner-filter { |
| 31 | + background: none; |
| 32 | + outline: none; |
| 33 | + border: none; |
| 34 | + cursor: pointer; |
| 35 | + color: var(--vscode-disabledForeground); |
| 36 | + text-decoration: none !important; |
| 37 | + font-weight: 500; |
| 38 | + } |
| 39 | + .owner-filter:hover { |
| 40 | + color: var(--vscode-foreground); |
| 41 | + text-decoration: underline !important; |
| 42 | + } |
| 43 | + .owner-item { |
| 44 | + display: flex; |
| 45 | + flex-direction: column; |
| 46 | + gap: 2px; |
| 47 | + } |
| 48 | + .owner-label { |
| 49 | + display: flex; |
| 50 | + gap: 4px; |
| 51 | + align-items: center; |
| 52 | + } |
| 53 | + .owner-email { |
| 54 | + color: var(--vscode-disabledForeground); |
| 55 | + } |
| 56 | + .current { |
| 57 | + display: inline-block; |
| 58 | + padding: 0px 2px; |
| 59 | + background: var(--vscode-disabledForeground); |
| 60 | + border-radius: 2px; |
| 61 | + } |
| 62 | + `, |
| 63 | + ]; |
| 64 | + |
| 65 | + @property({ type: Array }) filter: RepoOwner[] | undefined; |
| 66 | + @consume({ context: ipcContext }) |
| 67 | + private readonly _ipc!: HostIpc; |
| 68 | + |
| 69 | + private renderOwnerFilterLabel() { |
| 70 | + console.log('test', this.filter); |
| 71 | + if (!this.filter?.length) { |
| 72 | + return 'By all users'; |
| 73 | + } |
| 74 | + |
| 75 | + const additionalLabel = this.filter.length > 1 ? ` and ${pluralize('other', this.filter.length - 1)}` : ''; |
| 76 | + if (this.filter.some(x => x.current)) { |
| 77 | + return `By me${additionalLabel}`; |
| 78 | + } |
| 79 | + return `By ${this.filter[0].label}${additionalLabel}`; |
| 80 | + } |
| 81 | + private readonly _getOwners = new Task(this, { |
| 82 | + task: async () => { |
| 83 | + return this._ipc.sendRequest(GetRepoOwners, undefined); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + private renderOwnerItem(owner: RepoOwner) { |
| 88 | + return html`<div class="owner-item"> |
| 89 | + <div class="owner-label"> |
| 90 | + ${when(owner.avatarSrc, src => html`<gl-avatar src=${src}></gl-avatar>`)}</gl-avatar> |
| 91 | + <span>${owner.label}</span> |
| 92 | + ${when(owner.current, () => html`<span class="current">current</span>`)} |
| 93 | + </div> |
| 94 | + ${when(owner.email, email => html`<span class="owner-email">${email}</span>`)}</gl-avatar> |
| 95 | + </div>`; |
| 96 | + } |
| 97 | + |
| 98 | + @state() |
| 99 | + ownerQuery: string = ''; |
| 100 | + |
| 101 | + private filterState: Record<string, boolean> = {}; |
| 102 | + private renderOwnerList(ownerList: undefined | RepoOwner[]) { |
| 103 | + if (!ownerList) { |
| 104 | + return html`<p>No available owners</p>`; |
| 105 | + } |
| 106 | + |
| 107 | + return html` <input |
| 108 | + value=${this.ownerQuery} |
| 109 | + @input=${(e: Event) => { |
| 110 | + this.ownerQuery = (e.target as HTMLInputElement | null)?.value ?? ''; |
| 111 | + }} |
| 112 | + /> |
| 113 | + ${repeat( |
| 114 | + ownerList.filter( |
| 115 | + x => !this.ownerQuery || x.label.includes(this.ownerQuery) || x.email?.includes(this.ownerQuery), |
| 116 | + ), |
| 117 | + item => |
| 118 | + html`<menu-item role="none" |
| 119 | + ><gl-checkbox |
| 120 | + @gl-change-value=${() => { |
| 121 | + if (!item.email) { |
| 122 | + return; |
| 123 | + } |
| 124 | + this.filterState[item.email] = !this.filterState[item.email]; |
| 125 | + }} |
| 126 | + ?disabled=${!item.email} |
| 127 | + ?checked=${this.filter?.some(x => x.email === item.email)} |
| 128 | + >${this.renderOwnerItem(item)}</gl-checkbox |
| 129 | + ></menu-item |
| 130 | + >`, |
| 131 | + )} |
| 132 | + <menu-item |
| 133 | + @click=${() => { |
| 134 | + this.setOwnersFilter( |
| 135 | + Object.keys(this.filterState) |
| 136 | + .filter(x => this.filterState[x]) |
| 137 | + .map(email => this._getOwners.value?.find(x => x.email === email)) |
| 138 | + .filter(IterUtils.notNull), |
| 139 | + ); |
| 140 | + }} |
| 141 | + >Apply</menu-item |
| 142 | + > |
| 143 | + <menu-item |
| 144 | + @click=${() => { |
| 145 | + this.setOwnersFilter([]); |
| 146 | + }} |
| 147 | + >Clear</menu-item |
| 148 | + >`; |
| 149 | + } |
| 150 | + |
| 151 | + private setOwnersFilter(ownersList: RepoOwner[]) { |
| 152 | + const event = new CustomEvent('owners-filter-change', { |
| 153 | + detail: { ownersList: ownersList }, |
| 154 | + }); |
| 155 | + this.dispatchEvent(event); |
| 156 | + } |
| 157 | + |
| 158 | + override render() { |
| 159 | + return html` |
| 160 | + <gl-popover |
| 161 | + placement="bottom-start" |
| 162 | + trigger="focus" |
| 163 | + @gl-popover-show=${() => { |
| 164 | + this.ownerQuery = ''; |
| 165 | + this.filterState = {}; |
| 166 | + this.filter?.forEach(x => x.email && (this.filterState[x.email] = true)); |
| 167 | + void this._getOwners.run(); |
| 168 | + }} |
| 169 | + ?arrow=${false} |
| 170 | + > |
| 171 | + <button type="button" slot="anchor" class="owner-filter"> |
| 172 | + ${this.renderOwnerFilterLabel()}<code-icon icon="chevron-down"></code-icon> |
| 173 | + </button> |
| 174 | +
|
| 175 | + <div slot="content"> |
| 176 | + ${this._getOwners.render({ |
| 177 | + initial: () => html`<menu-item>Waiting to get owners</menu-item>`, |
| 178 | + pending: () => html`<p>Getting repo owners</p>`, |
| 179 | + complete: this.renderOwnerList.bind(this), |
| 180 | + error: error => html`<p>Oops, something went wrong: ${error}</p>`, |
| 181 | + })} |
| 182 | + </div> |
| 183 | + </gl-popover> |
| 184 | + `; |
| 185 | + } |
| 186 | +} |
0 commit comments