|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +import '../../../ui/components/buttons/buttons.js'; |
| 7 | +import '../../../ui/components/icon_button/icon_button.js'; |
| 8 | + |
| 9 | +import type * as Platform from '../../../core/platform/platform.js'; |
| 10 | +import type * as Trace from '../../../models/trace/trace.js'; |
| 11 | +import * as Buttons from '../../../ui/components/buttons/buttons.js'; |
| 12 | +import * as UI from '../../../ui/legacy/legacy.js'; |
| 13 | +import * as Lit from '../../../ui/lit/lit.js'; |
| 14 | + |
| 15 | +import type {AggregatedPerfIssue, PerfIssueEvent, PerfIssueSeverity} from './RNPerfIssueTypes.js'; |
| 16 | +import styles from './sidebarRNPerfIssueItem.css.js'; |
| 17 | + |
| 18 | +const {html} = Lit; |
| 19 | + |
| 20 | +interface SidebarRNPerfIssueItemData { |
| 21 | + issue: AggregatedPerfIssue; |
| 22 | + onEventSelected?: (event: Trace.Types.Events.Event) => void; |
| 23 | +} |
| 24 | + |
| 25 | +export class SidebarRNPerfIssueItem extends HTMLElement { |
| 26 | + readonly #shadow = this.attachShadow({mode: 'open'}); |
| 27 | + |
| 28 | + #issue: AggregatedPerfIssue|null = null; |
| 29 | + #onEventSelected?: (event: Trace.Types.Events.Event) => void; |
| 30 | + #isOpen = false; |
| 31 | + |
| 32 | + set data(data: SidebarRNPerfIssueItemData) { |
| 33 | + this.#issue = data.issue; |
| 34 | + this.#onEventSelected = data.onEventSelected; |
| 35 | + this.#render(); |
| 36 | + } |
| 37 | + |
| 38 | + #toggleOpen(event: Event): void { |
| 39 | + event.preventDefault(); |
| 40 | + this.#isOpen = !this.#isOpen; |
| 41 | + this.#render(); |
| 42 | + } |
| 43 | + |
| 44 | + #onEventClick(issueEvent: PerfIssueEvent): void { |
| 45 | + if (this.#onEventSelected) { |
| 46 | + this.#onEventSelected(issueEvent.event); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + #onEventKeyDown(event: KeyboardEvent, issueEvent: PerfIssueEvent): void { |
| 51 | + if (event.key === 'Enter' || event.key === ' ') { |
| 52 | + event.preventDefault(); |
| 53 | + this.#onEventClick(issueEvent); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #renderDropdownIcon(isOpen: boolean): Lit.TemplateResult { |
| 58 | + return html` |
| 59 | + <devtools-button .data=${{ |
| 60 | + variant: Buttons.Button.Variant.ICON, |
| 61 | + iconName: 'chevron-right', |
| 62 | + size: Buttons.Button.Size.SMALL, |
| 63 | + } as Buttons.Button.ButtonData} class="dropdown-icon ${isOpen ? 'open' : ''}"></devtools-button> |
| 64 | + `; |
| 65 | + } |
| 66 | + |
| 67 | + #renderSeverityIcon(severity: PerfIssueSeverity): Lit.TemplateResult { |
| 68 | + const iconData = { |
| 69 | + error: {iconName: 'issue-cross-filled', color: 'var(--icon-error)'}, |
| 70 | + warning: {iconName: 'issue-exclamation-filled', color: 'var(--icon-warning)'}, |
| 71 | + info: {iconName: 'issue-text-filled', color: 'var(--icon-info)'}, |
| 72 | + } as const; |
| 73 | + const {iconName, color} = iconData[severity]; |
| 74 | + return html` |
| 75 | + <devtools-icon .data=${{iconName, color, width: '16px', height: '16px'}}></devtools-icon> |
| 76 | + `; |
| 77 | + } |
| 78 | + |
| 79 | + #render(): void { |
| 80 | + const issue = this.#issue; |
| 81 | + if (!issue) { |
| 82 | + Lit.render(Lit.nothing, this.#shadow, {host: this}); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const formatter = new Intl.NumberFormat(undefined, { |
| 87 | + minimumFractionDigits: 1, |
| 88 | + maximumFractionDigits: 1, |
| 89 | + style: 'decimal', |
| 90 | + }); |
| 91 | + const contents = html` |
| 92 | + <style>${styles.cssText}</style> |
| 93 | + <details ?open=${this.#isOpen}> |
| 94 | + <summary @click=${(e: Event) => this.#toggleOpen(e)} class="issue-summary"> |
| 95 | + ${this.#renderDropdownIcon(this.#isOpen)} |
| 96 | + ${this.#renderSeverityIcon(issue.severity)} |
| 97 | + <span class="event-count-pill">${issue.count}</span> |
| 98 | + <div class="issue-info"> |
| 99 | + <div class="issue-name">${issue.name}</div> |
| 100 | + ${issue.description ? html`<div class="issue-description">${issue.description}</div>` : ''} |
| 101 | + ${issue.learnMoreUrl ? html` |
| 102 | + <div class="issue-learn-more"> |
| 103 | + ${UI.XLink.XLink.create(issue.learnMoreUrl as Platform.DevToolsPath.UrlString, 'Learn more')} |
| 104 | + </div> |
| 105 | + ` : ''} |
| 106 | + </div> |
| 107 | + </summary> |
| 108 | + <div class="issue-content"> |
| 109 | + <div class="event-list event-list-${issue.severity}"> |
| 110 | + ${issue.events.map((issueEvent, i) => { |
| 111 | + return html` |
| 112 | + <div class="event-item" |
| 113 | + tabindex="0" |
| 114 | + role="button" |
| 115 | + aria-label="Navigate to item ${i + 1} in timeline" |
| 116 | + @click=${() => this.#onEventClick(issueEvent)} |
| 117 | + @keydown=${(event: KeyboardEvent) => this.#onEventKeyDown(event, issueEvent)}> |
| 118 | + <div class="event-name">${issueEvent.event.name}</div> |
| 119 | + <div class="event-timestamp">${formatter.format(issueEvent.timestampMs)} ms</div> |
| 120 | + </div>`; |
| 121 | + })} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </details> |
| 125 | + `; |
| 126 | + Lit.render(contents, this.#shadow, {host: this}); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +customElements.define('devtools-performance-sidebar-perf-issue-item', SidebarRNPerfIssueItem); |
| 131 | + |
| 132 | +declare global { |
| 133 | + interface HTMLElementTagNameMap { |
| 134 | + 'devtools-performance-sidebar-perf-issue-item': SidebarRNPerfIssueItem; |
| 135 | + } |
| 136 | +} |
0 commit comments