summaryrefslogtreecommitdiffstats
path: root/browser/components/shopping/content/letter-grade.mjs
blob: 71badb1c4578f25fb5171a7d9265cee838e97bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { html } from "chrome://global/content/vendor/lit.all.mjs";

import { MozLitElement } from "chrome://global/content/lit-utils.mjs";

const VALID_LETTER_GRADE_L10N_IDS = new Map([
  ["A", "shopping-letter-grade-description-ab"],
  ["B", "shopping-letter-grade-description-ab"],
  ["C", "shopping-letter-grade-description-c"],
  ["D", "shopping-letter-grade-description-df"],
  ["F", "shopping-letter-grade-description-df"],
]);

class LetterGrade extends MozLitElement {
  #descriptionL10N;

  static properties = {
    letter: { type: String, reflect: true },
    showdescription: { type: Boolean, reflect: true },
  };

  get fluentStrings() {
    if (!this._fluentStrings) {
      this._fluentStrings = new Localization(["browser/shopping.ftl"], true);
    }
    return this._fluentStrings;
  }

  descriptionTemplate() {
    if (this.showdescription) {
      return html`<p
        id="letter-grade-description"
        data-l10n-id=${this.#descriptionL10N}
      ></p>`;
    }

    return null;
  }

  render() {
    // Do not render if letter is invalid
    if (!VALID_LETTER_GRADE_L10N_IDS.has(this.letter)) {
      return null;
    }

    this.#descriptionL10N = VALID_LETTER_GRADE_L10N_IDS.get(this.letter);
    let tooltipL10NArgs;
    // Do not localize tooltip if using Storybook.
    if (!window.IS_STORYBOOK) {
      const localizedDescription = this.fluentStrings.formatValueSync(
        this.#descriptionL10N
      );
      tooltipL10NArgs = `{"letter": "${this.letter}", "description": "${localizedDescription}"}`;
    }

    return html`
      <link
        rel="stylesheet"
        href="chrome://browser/content/shopping/letter-grade.css"
      />
      <article
        id="letter-grade-wrapper"
        data-l10n-id="shopping-letter-grade-tooltip"
        data-l10n-attrs="title"
        data-l10n-args=${tooltipL10NArgs}
      >
        <p id="letter-grade-term">${this.letter}</p>
        ${this.descriptionTemplate()}
      </article>
    `;
  }
}

customElements.define("letter-grade", LetterGrade);