summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/extensions/components/ExtensionSidebar.js
blob: a351b6add97a5f5fa951c7c7ade6c8737a61a5ec (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
/* 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 {
  createFactory,
  PureComponent,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const {
  connect,
} = require("resource://devtools/client/shared/vendor/react-redux.js");

const ExtensionPage = createFactory(
  require("resource://devtools/client/inspector/extensions/components/ExtensionPage.js")
);
const ObjectTreeView = createFactory(
  require("resource://devtools/client/inspector/extensions/components/ObjectTreeView.js")
);
const ExpressionResultView = createFactory(
  require("resource://devtools/client/inspector/extensions/components/ExpressionResultView.js")
);
const Types = require("resource://devtools/client/inspector/extensions/types.js");

/**
 * The ExtensionSidebar is a React component with 2 supported viewMode:
 * - an ObjectTreeView UI, used to show the JS objects
 *   (used by the sidebar.setObject WebExtensions APIs)
 * - an ExpressionResultView UI, used to show the result for an expression
 *   (used by sidebar.setExpression WebExtensions APIs)
 * - an ExtensionPage UI used to show an extension page
 *   (used by the sidebar.setPage WebExtensions APIs).
 *
 * TODO: implement the ExtensionPage viewMode.
 */
class ExtensionSidebar extends PureComponent {
  static get propTypes() {
    return {
      id: PropTypes.string.isRequired,
      extensionsSidebar: PropTypes.object.isRequired,
      onExtensionPageMount: PropTypes.func.isRequired,
      onExtensionPageUnmount: PropTypes.func.isRequired,
      // Helpers injected as props by extension-sidebar.js.
      serviceContainer: PropTypes.shape(Types.serviceContainer).isRequired,
    };
  }

  render() {
    const {
      id,
      extensionsSidebar,
      onExtensionPageMount,
      onExtensionPageUnmount,
      serviceContainer,
    } = this.props;

    const {
      iframeURL,
      object,
      expressionResult,
      rootTitle,
      viewMode = "empty-sidebar",
    } = extensionsSidebar[id] || {};

    let sidebarContentEl;

    switch (viewMode) {
      case "object-treeview":
        sidebarContentEl = ObjectTreeView({ object });
        break;
      case "object-value-grip-view":
        sidebarContentEl = ExpressionResultView({
          expressionResult,
          rootTitle,
          serviceContainer,
        });
        break;
      case "extension-page":
        sidebarContentEl = ExtensionPage({
          iframeURL,
          onExtensionPageMount,
          onExtensionPageUnmount,
        });
        break;
      case "empty-sidebar":
        break;
      default:
        throw new Error(`Unknown ExtensionSidebar viewMode: "${viewMode}"`);
    }

    const className = "devtools-monospace extension-sidebar inspector-tabpanel";

    return dom.div(
      {
        id,
        className,
      },
      sidebarContentEl
    );
  }
}

module.exports = connect(state => state)(ExtensionSidebar);