tor-browser

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

SnapshotListItem.js (3929B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  Component,
      9 } = require("resource://devtools/client/shared/vendor/react.mjs");
     10 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs");
     11 const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
     12 const {
     13  L10N,
     14  getSnapshotTitle,
     15  getSnapshotTotals,
     16  getStatusText,
     17  snapshotIsDiffable,
     18  getSavedCensus,
     19 } = require("resource://devtools/client/memory/utils.js");
     20 const {
     21  diffingState,
     22 } = require("resource://devtools/client/memory/constants.js");
     23 const {
     24  snapshot: snapshotModel,
     25  app: appModel,
     26 } = require("resource://devtools/client/memory/models.js");
     27 
     28 class SnapshotListItem extends Component {
     29  static get propTypes() {
     30    return {
     31      onClick: PropTypes.func.isRequired,
     32      onSave: PropTypes.func.isRequired,
     33      onDelete: PropTypes.func.isRequired,
     34      item: snapshotModel.isRequired,
     35      index: PropTypes.number.isRequired,
     36      diffing: appModel.diffing,
     37    };
     38  }
     39 
     40  render() {
     41    const { item: snapshot, onClick, onSave, onDelete, diffing } = this.props;
     42    let className = `snapshot-list-item ${
     43      snapshot.selected ? " selected" : ""
     44    }`;
     45    let statusText = getStatusText(snapshot.state);
     46    let wantThrobber = !!statusText;
     47    const title = getSnapshotTitle(snapshot);
     48 
     49    const selectedForDiffing =
     50      diffing &&
     51      (diffing.firstSnapshotId === snapshot.id ||
     52        diffing.secondSnapshotId === snapshot.id);
     53 
     54    let checkbox;
     55    if (diffing && snapshotIsDiffable(snapshot)) {
     56      if (diffing.state === diffingState.SELECTING) {
     57        wantThrobber = false;
     58      }
     59 
     60      const checkboxAttrs = {
     61        type: "checkbox",
     62        checked: false,
     63      };
     64 
     65      if (selectedForDiffing) {
     66        checkboxAttrs.checked = true;
     67        checkboxAttrs.disabled = true;
     68        className += " selected";
     69        statusText = L10N.getStr(
     70          diffing.firstSnapshotId === snapshot.id
     71            ? "diffing.baseline"
     72            : "diffing.comparison"
     73        );
     74      }
     75 
     76      if (selectedForDiffing || diffing.state == diffingState.SELECTING) {
     77        checkbox = dom.input(checkboxAttrs);
     78      }
     79    }
     80 
     81    let details;
     82    if (!selectedForDiffing) {
     83      // See if a tree map or census is in the read state.
     84      const census = getSavedCensus(snapshot);
     85 
     86      // If there is census data, fill in the total bytes.
     87      if (census) {
     88        const { bytes } = getSnapshotTotals(census);
     89        const formatBytes = L10N.getFormatStr(
     90          "aggregate.mb",
     91          L10N.numberWithDecimals(bytes / 1000000, 2)
     92        );
     93 
     94        details = dom.span(
     95          { className: "snapshot-totals" },
     96          dom.span({ className: "total-bytes" }, formatBytes)
     97        );
     98      }
     99    }
    100    if (!details) {
    101      details = dom.span({ className: "snapshot-state" }, statusText);
    102    }
    103 
    104    const saveLink = !snapshot.path
    105      ? void 0
    106      : dom.a(
    107          {
    108            onClick: () => onSave(snapshot),
    109            className: "save",
    110          },
    111          L10N.getStr("snapshot.io.save")
    112        );
    113 
    114    const deleteButton = !snapshot.path
    115      ? void 0
    116      : dom.button({
    117          onClick: event => {
    118            event.stopPropagation();
    119            onDelete(snapshot);
    120          },
    121          className: "delete",
    122          title: L10N.getStr("snapshot.io.delete"),
    123        });
    124 
    125    return dom.li(
    126      { className, onClick },
    127      dom.span(
    128        {
    129          className: `snapshot-title ${
    130            wantThrobber ? " devtools-throbber" : ""
    131          }`,
    132        },
    133        checkbox,
    134        title,
    135        deleteButton
    136      ),
    137      dom.span({ className: "snapshot-info" }, details, saveLink)
    138    );
    139  }
    140 }
    141 
    142 module.exports = SnapshotListItem;