MessageIcon.js (2307B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 8 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js"); 9 const { 10 l10n, 11 } = require("resource://devtools/client/webconsole/utils/messages.js"); 12 const { 13 MESSAGE_TYPE, 14 } = require("resource://devtools/client/webconsole/constants.js"); 15 16 const l10nLevels = { 17 error: "level.error", 18 warn: "level.warn", 19 info: "level.info", 20 log: "level.log", 21 debug: "level.debug", 22 jstracer: "level.jstracer", 23 }; 24 25 // Store common icons so they can be used without recreating the element 26 // during render. 27 const CONSTANT_ICONS = Object.entries(l10nLevels).reduce( 28 (acc, [key, l10nLabel]) => { 29 acc[key] = getIconElement(l10nLabel); 30 return acc; 31 }, 32 {} 33 ); 34 35 function getIconElement(level, type, title) { 36 title = title || l10n.getStr(l10nLevels[level] || level); 37 const classnames = ["icon"]; 38 39 if (type === "logPoint") { 40 title = l10n.getStr("logpoint.title"); 41 classnames.push("logpoint"); 42 } else if (type === "blockedReason") { 43 title = l10n.getStr("blockedrequest.label2"); 44 } else if (type === MESSAGE_TYPE.COMMAND) { 45 title = l10n.getStr("command.title"); 46 } else if (type === MESSAGE_TYPE.RESULT) { 47 title = l10n.getStr("result.title"); 48 } else if (level == "level.jstracer" || type == MESSAGE_TYPE.JSTRACER) { 49 // Actual traces uses JSTracerTrace objects and `level` attribute, 50 // while log messages relating to tracing uses ConsoleApiCall, where level == "log" and so rather uses `type` attribute. 51 classnames.push("logtrace"); 52 } 53 54 return dom.span({ 55 className: classnames.join(" "), 56 title, 57 "aria-live": "off", 58 }); 59 } 60 61 MessageIcon.displayName = "MessageIcon"; 62 MessageIcon.propTypes = { 63 level: PropTypes.string.isRequired, 64 type: PropTypes.string, 65 title: PropTypes.string, 66 }; 67 68 function MessageIcon(props) { 69 const { level, type, title } = props; 70 71 if (type) { 72 return getIconElement(level, type, title); 73 } 74 75 return CONSTANT_ICONS[level] || getIconElement(level); 76 } 77 78 module.exports = MessageIcon;