summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/components/StatusBar.js
blob: 3a6c73e0db02d03c5ab1ff85281b7da73636a196 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* 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/. */

"use strict";

const {
  Component,
} = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const {
  connect,
} = require("resource://devtools/client/shared/redux/visibility-handler-connect.js");
const { PluralForm } = require("resource://devtools/shared/plural-form.js");
const Actions = require("resource://devtools/client/netmonitor/src/actions/index.js");
const {
  getDisplayedRequestsSummary,
  getDisplayedTimingMarker,
} = require("resource://devtools/client/netmonitor/src/selectors/index.js");
const {
  getFormattedSize,
  getFormattedTime,
} = require("resource://devtools/client/netmonitor/src/utils/format-utils.js");
const {
  L10N,
} = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
const {
  propertiesEqual,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");

const { button, div } = dom;

const REQUESTS_COUNT_EMPTY = L10N.getStr(
  "networkMenu.summary.requestsCountEmpty"
);
const TOOLTIP_PERF = L10N.getStr("networkMenu.summary.tooltip.perf");
const TOOLTIP_REQUESTS_COUNT = L10N.getStr(
  "networkMenu.summary.tooltip.requestsCount"
);
const TOOLTIP_TRANSFERRED = L10N.getStr(
  "networkMenu.summary.tooltip.transferred"
);
const TOOLTIP_FINISH = L10N.getStr("networkMenu.summary.tooltip.finish");
const TOOLTIP_DOM_CONTENT_LOADED = L10N.getStr(
  "networkMenu.summary.tooltip.domContentLoaded"
);
const TOOLTIP_LOAD = L10N.getStr("networkMenu.summary.tooltip.load");

const UPDATED_SUMMARY_PROPS = ["count", "contentSize", "transferredSize", "ms"];

const UPDATED_TIMING_PROPS = ["DOMContentLoaded", "load"];

/**
 * Status Bar component
 * Displays the summary of total size and transferred size by all requests
 * Also displays different timing markers
 */
class StatusBar extends Component {
  static get propTypes() {
    return {
      connector: PropTypes.object.isRequired,
      openStatistics: PropTypes.func.isRequired,
      summary: PropTypes.object.isRequired,
      timingMarkers: PropTypes.object.isRequired,
    };
  }

  shouldComponentUpdate(nextProps) {
    const { summary, timingMarkers } = this.props;
    return (
      !propertiesEqual(UPDATED_SUMMARY_PROPS, summary, nextProps.summary) ||
      !propertiesEqual(
        UPDATED_TIMING_PROPS,
        timingMarkers,
        nextProps.timingMarkers
      )
    );
  }

  render() {
    const { openStatistics, summary, timingMarkers, connector } = this.props;
    const { count, contentSize, transferredSize, ms } = summary;
    const { DOMContentLoaded, load } = timingMarkers;

    const toolbox = connector.getToolbox();
    const countText =
      count === 0
        ? REQUESTS_COUNT_EMPTY
        : PluralForm.get(
            count,
            L10N.getStr("networkMenu.summary.requestsCount2")
          ).replace("#1", count);
    const transferText = L10N.getFormatStrWithNumbers(
      "networkMenu.summary.transferred",
      getFormattedSize(contentSize),
      getFormattedSize(transferredSize)
    );
    const finishText = L10N.getFormatStrWithNumbers(
      "networkMenu.summary.finish",
      getFormattedTime(ms)
    );

    return div(
      { className: "devtools-toolbar devtools-toolbar-bottom" },
      !toolbox.isBrowserToolbox
        ? button(
            {
              className: "devtools-button requests-list-network-summary-button",
              title: TOOLTIP_PERF,
              onClick: openStatistics,
            },
            div({ className: "summary-info-icon" })
          )
        : null,
      div(
        {
          className: "status-bar-label requests-list-network-summary-count",
          title: TOOLTIP_REQUESTS_COUNT,
        },
        countText
      ),
      count !== 0 &&
        div(
          {
            className:
              "status-bar-label requests-list-network-summary-transfer",
            title: TOOLTIP_TRANSFERRED,
          },
          transferText
        ),
      count !== 0 &&
        div(
          {
            className: "status-bar-label requests-list-network-summary-finish",
            title: TOOLTIP_FINISH,
          },
          finishText
        ),
      DOMContentLoaded > -1 &&
        div(
          {
            className: "status-bar-label dom-content-loaded",
            title: TOOLTIP_DOM_CONTENT_LOADED,
          },
          `DOMContentLoaded: ${getFormattedTime(DOMContentLoaded)}`
        ),
      load > -1 &&
        div(
          {
            className: "status-bar-label load",
            title: TOOLTIP_LOAD,
          },
          `load: ${getFormattedTime(load)}`
        )
    );
  }
}

module.exports = connect(
  state => ({
    summary: getDisplayedRequestsSummary(state),
    timingMarkers: {
      DOMContentLoaded: getDisplayedTimingMarker(
        state,
        "firstDocumentDOMContentLoadedTimestamp"
      ),
      load: getDisplayedTimingMarker(state, "firstDocumentLoadTimestamp"),
    },
  }),
  (dispatch, props) => ({
    openStatistics: () =>
      dispatch(Actions.openStatistics(props.connector, true)),
  })
)(StatusBar);