aboutsummaryrefslogtreecommitdiffstats
path: root/old-ui/app/components/typed-message-renderer.js
blob: 0dc673b8ab8f30ad066ca27c9e55be440a27e04a (plain) (blame)
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
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const extend = require('xtend')
const { ObjectInspector } = require('react-inspector')

module.exports = TypedMessageRenderer

inherits(TypedMessageRenderer, Component)
function TypedMessageRenderer () {
  Component.call(this)
}

TypedMessageRenderer.prototype.render = function () {
  const props = this.props
  const { value, version, style } = props
  let text
  switch (version) {
    case 'V1':
      text = renderTypedData(value)
      break
    case 'V3':
      text = renderTypedDataV3(value)
      break
  }

  const defaultStyle = extend({
    width: '315px',
    maxHeight: '210px',
    resize: 'none',
    border: 'none',
    background: 'white',
    padding: '3px',
    overflow: 'scroll',
  }, style)

  return (
    h('div.font-small', {
      style: defaultStyle,
    }, text)
  )
}

function renderTypedData (values) {
  return values.map(function (value) {
    let v = value.value
    if (typeof v === 'boolean') {
      v = v.toString()
    }
    return h('div', {}, [
      h('strong', {style: {display: 'block', fontWeight: 'bold'}}, String(value.name) + ':'),
      h('div', {}, v),
    ])
  })
}

function renderTypedDataV3 (values) {
  const { domain, message } = JSON.parse(values)
   return [
    domain ? h('div', [
      h('h1', 'Domain'),
      h(ObjectInspector, { data: domain, expandLevel: 1, name: 'domain' }),
    ]) : '',
    message ? h('div', [
      h('h1', 'Message'),
      h(ObjectInspector, { data: message, expandLevel: 1, name: 'message' }),
    ]) : '',
  ]
}