|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +/** |
| 6 | + * @fileoverview A list of pass/fail conditions for an insight. |
| 7 | + */ |
| 8 | + |
| 9 | +import '../../../../ui/components/icon_button/icon_button.js'; |
| 10 | + |
| 11 | +import * as i18n from '../../../../core/i18n/i18n.js'; |
| 12 | +import type * as Trace from '../../../../models/trace/trace.js'; |
| 13 | +import * as ComponentHelpers from '../../../../ui/components/helpers/helpers.js'; |
| 14 | +import * as UI from '../../../../ui/legacy/legacy.js'; |
| 15 | +import * as Lit from '../../../../ui/lit/lit.js'; |
| 16 | +import type * as Overlays from '../../overlays/overlays.js'; |
| 17 | + |
| 18 | +import checklistStylesRaw from './checklist.css.js'; |
| 19 | + |
| 20 | +const UIStrings = { |
| 21 | + /** |
| 22 | + *@description Text for a screen-reader label to tell the user that the icon represents a successful insight check |
| 23 | + *@example {Server response time} PH1 |
| 24 | + */ |
| 25 | + successAriaLabel: 'Insight check passed: {PH1}', |
| 26 | + /** |
| 27 | + *@description Text for a screen-reader label to tell the user that the icon represents an unsuccessful insight check |
| 28 | + *@example {Server response time} PH1 |
| 29 | + */ |
| 30 | + failedAriaLabel: 'Insight check failed: {PH1}', |
| 31 | +}; |
| 32 | + |
| 33 | +const str_ = i18n.i18n.registerUIStrings('panels/timeline/components/insights/Checklist.ts', UIStrings); |
| 34 | +const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 35 | + |
| 36 | +// TODO(crbug.com/391381439): Fully migrate off of constructed style sheets. |
| 37 | +const checklistStyles = new CSSStyleSheet(); |
| 38 | +checklistStyles.replaceSync(checklistStylesRaw.cssContent); |
| 39 | + |
| 40 | +const {html} = Lit; |
| 41 | + |
| 42 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 43 | +type GenericChecklist = Trace.Insights.Types.Checklist<any>; |
| 44 | + |
| 45 | +export interface ChecklistData { |
| 46 | + checklist: GenericChecklist; |
| 47 | +} |
| 48 | + |
| 49 | +export interface TableDataRow { |
| 50 | + values: Array<number|string|Lit.LitTemplate>; |
| 51 | + overlays?: Overlays.Overlays.TimelineOverlay[]; |
| 52 | +} |
| 53 | + |
| 54 | +export class Checklist extends HTMLElement { |
| 55 | + readonly #shadow = this.attachShadow({mode: 'open'}); |
| 56 | + readonly #boundRender = this.#render.bind(this); |
| 57 | + #checklist?: GenericChecklist; |
| 58 | + |
| 59 | + set checklist(checklist: GenericChecklist) { |
| 60 | + this.#checklist = checklist; |
| 61 | + void ComponentHelpers.ScheduledRender.scheduleRender(this, this.#boundRender); |
| 62 | + } |
| 63 | + |
| 64 | + connectedCallback(): void { |
| 65 | + this.#shadow.adoptedStyleSheets.push(checklistStyles); |
| 66 | + UI.UIUtils.injectCoreStyles(this.#shadow); |
| 67 | + |
| 68 | + void ComponentHelpers.ScheduledRender.scheduleRender(this, this.#boundRender); |
| 69 | + } |
| 70 | + |
| 71 | + #getIcon(check: GenericChecklist['']): Lit.TemplateResult { |
| 72 | + const icon = check.value ? 'check-circle' : 'clear'; |
| 73 | + |
| 74 | + const ariaLabel = check.value ? i18nString(UIStrings.successAriaLabel, {PH1: check.label}) : |
| 75 | + i18nString(UIStrings.failedAriaLabel, {PH1: check.label}); |
| 76 | + return html` |
| 77 | + <devtools-icon |
| 78 | + aria-label=${ariaLabel} |
| 79 | + name=${icon} |
| 80 | + class=${check.value ? 'check-passed' : 'check-failed'} |
| 81 | + ></devtools-icon> |
| 82 | + `; |
| 83 | + } |
| 84 | + |
| 85 | + async #render(): Promise<void> { |
| 86 | + if (!this.#checklist) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + Lit.render( |
| 91 | + html` |
| 92 | + <ul> |
| 93 | + ${Object.values(this.#checklist).map(check => html`<li> |
| 94 | + ${this.#getIcon(check)} |
| 95 | + <span>${check.label}</span> |
| 96 | + </li>`)} |
| 97 | + </ul>`, |
| 98 | + this.#shadow, {host: this}); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +declare global { |
| 103 | + interface HTMLElementTagNameMap { |
| 104 | + 'devtools-performance-checklist': Checklist; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +customElements.define('devtools-performance-checklist', Checklist); |
0 commit comments