tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

date-time.mjs (2101B)


      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 import PropTypes from "resource://devtools/client/shared/vendor/react-prop-types.mjs";
      6 import { span } from "resource://devtools/client/shared/vendor/react-dom-factories.mjs";
      7 
      8 import { getGripType, wrapRender } from "./rep-utils.mjs";
      9 
     10 /**
     11 * Used to render JS built-in Date() object.
     12 */
     13 
     14 DateTime.propTypes = {
     15  object: PropTypes.object.isRequired,
     16  shouldRenderTooltip: PropTypes.bool,
     17 };
     18 
     19 function DateTime(props) {
     20  const { object: grip, shouldRenderTooltip } = props;
     21  let date;
     22  try {
     23    const dateObject = new Date(grip.preview.timestamp);
     24    // Calling `toISOString` will throw if the date is invalid,
     25    // so we can render an `Invalid Date` element.
     26    dateObject.toISOString();
     27 
     28    const dateObjectString = dateObject.toString();
     29 
     30    const config = getElementConfig({
     31      grip,
     32      dateObjectString,
     33      shouldRenderTooltip,
     34    });
     35 
     36    date = span(
     37      config,
     38      getTitle(grip),
     39      span({ className: "Date" }, dateObjectString)
     40    );
     41  } catch (e) {
     42    date = span(
     43      {
     44        className: "objectBox",
     45        title: shouldRenderTooltip ? "Invalid Date" : null,
     46      },
     47      "Invalid Date"
     48    );
     49  }
     50 
     51  return date;
     52 }
     53 
     54 function getElementConfig(opts) {
     55  const { grip, dateObjectString, shouldRenderTooltip } = opts;
     56 
     57  return {
     58    "data-link-actor-id": grip.actor,
     59    className: "objectBox",
     60    title: shouldRenderTooltip ? `${grip.class} ${dateObjectString}` : null,
     61  };
     62 }
     63 
     64 // getTitle() is used to render the `Date ` before the stringified date object,
     65 // not to render the actual span "title".
     66 
     67 function getTitle(grip) {
     68  return span(
     69    {
     70      className: "objectTitle",
     71    },
     72    `${grip.class} `
     73  );
     74 }
     75 
     76 // Registration
     77 function supportsObject(grip, noGrip = false) {
     78  return getGripType(grip, noGrip) == "Date" && grip?.preview;
     79 }
     80 
     81 const rep = wrapRender(DateTime);
     82 
     83 // Exports from this module
     84 export { rep, supportsObject };