/* 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 https://mozilla.org/MPL/2.0/. */ import { html, until } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { DownloadUtils: "resource://gre/modules/DownloadUtils.sys.mjs", featureEngineIdToFluentId: "chrome://global/content/ml/Utils.sys.mjs", recordModelCardLinkTelemetry: "chrome://global/content/ml/Utils.sys.mjs", }); const DEFAULT_EXTENSION_ICON = "chrome://mozapps/skin/extensions/extensionGeneric.svg"; export class AddonMLModelDetails extends MozLitElement { static properties = { addon: { type: Object, // Prevent this property from being converted into an DOM attribute // (LitElement would hit a `TypeError: cyclic object value` in tests using // MockProvider). reflect: false, }, lastUsed: { type: String }, modelSize: { type: String }, }; setAddon(addon) { this.addon = addon; this.lastUsed = addon.lastUsed.toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric", }); this.modelSize = lazy.DownloadUtils.getTransferTotal( this.addon.totalSize ?? 0 ); } // NOTE: opt-out from using the shadow dom as render root (and use the element // itself to host the custom element content instead). createRenderRoot() { return this; } async renderUsedByAddons() { const addons = await AddonManager.getAddonsByIDs(this.addon.usedByAddonIds); return addons?.map(addon => { if (!addon) { return null; } const iconURL = addon.iconURL ?? DEFAULT_EXTENSION_ICON; return html`
`; }); } renderUsedByFirefoxFeatures() { return this.addon.usedByFirefoxFeatures.map(engineId => { const fluentId = lazy.featureEngineIdToFluentId(engineId); if (!fluentId) { return null; } return html`
`; }); } handleCardLinkClick() { lazy.recordModelCardLinkTelemetry(this.addon); } render() { if (this.addon?.type !== "mlmodel") { return null; } return html`
${this.renderUsedByFirefoxFeatures()} ${until(this.renderUsedByAddons(), html``)}
${this.modelSize}
${this.lastUsed}
`; } } customElements.define("addon-mlmodel-details", AddonMLModelDetails);