on 4efb153 help sign in
bridge/src/views/Point.js
148 lines 3.5 kB history raw
1import Maybe from 'folktale/maybe'
2import React from 'react'
3import { pour } from 'sigil-js'
4import * as ob from 'urbit-ob'
5import * as azimuth from 'azimuth-js'
7import PointList from '../components/PointList'
8import ReactSVGComponents from '../components/ReactSVGComponents'
9import KeysAndMetadata from './Point/KeysAndMetadata'
10import Actions from './Point/Actions'
11import { BRIDGE_ERROR } from '../lib/error'
12import { Row, Col, H1, H3 } from '../components/Base'
15class Point extends React.Component {
17 constructor(props) {
18 super(props)
20 this.state = {
21 spawned: []
22 }
23 }
25 componentDidMount() {
26 this.cachePoints()
27 }
29 componentDidUpdate(prevProps) {
30 //
31 // NB (jtobin)
32 //
33 // When a link in the 'issued points' component is clicked, we don't
34 // actually leave the Point component, and componentDidMount will not fire
35 // again. But, we can force a call to cachePoints here.
36 //
37 const oldCursor = prevProps.pointCursor.getOrElse(true)
38 const newCursor = this.props.pointCursor.getOrElse(true)
40 if (oldCursor !== newCursor) {
41 this.cachePoints()
42 }
43 }
45 cachePoints() {
46 const { web3, contracts, pointCursor, addToPointCache } = this.props
48 web3.chain(_ =>
49 contracts.chain(ctrcs =>
50 pointCursor.chain(point => {
51 azimuth.azimuth.getPoint(ctrcs, point)
52 .then(details => addToPointCache({ [point]: details }))
53 this.updateSpawned(ctrcs, point)
54 })))
55 }
57 updateSpawned = contracts => {
58 const { pointCursor } = this.props
60 pointCursor.matchWith({
61 Nothing: _ => {},
62 Just: point => {
63 azimuth.azimuth.getSpawned(contracts, point.value)
64 .then(spawned => this.setState({ spawned }))
65 }
66 })
68 }
70 render() {
72 const { web3, popRoute, pushRoute, wallet } = this.props
73 const { setPointCursor } = this.props
74 const { pointCursor, pointCache } = this.props
76 const { spawned } = this.state
79 const point = pointCursor.matchWith({
80 Just: (cursor) => cursor.value,
81 Nothing: () => {
82 throw BRIDGE_ERROR.MISSING_POINT
83 }
84 })
86 const pointDetails =
87 point in pointCache
88 ? Maybe.Just(pointCache[point])
89 : Maybe.Nothing()
91 const name = ob.patp(point)
93 const sigil = pour({
94 patp: name,
95 renderer: ReactSVGComponents,
96 size: 256
97 })
99 const online = Maybe.Just.hasInstance(web3)
101 const authenticated = Maybe.Just.hasInstance(wallet)
103 const routeHandler = route => {
104 popRoute()
105 pushRoute(route)
106 }
108 const issuedPointList =
109 spawned.length === 0
110 ? <div />
111 : <div>
112 <H3>{ 'Issued Points' }</H3>
114 <PointList
115 setPointCursor={ setPointCursor }
116 routeHandler={ routeHandler }
117 points={ spawned }
118 />
119 </div>
123 return (
124 <Row>
125 <Col>
126 <div className={'mt-12 pt-6'}>
127 { sigil }
128 </div>
129 <H1><code>{ name }</code></H1>
130 {
131 authenticated
132 ? <Actions pushRoute={ pushRoute } online={ online } point={ point } pointDetails={ pointDetails } wallet={ wallet }/>
133 : null
134 }
136 {
137 online
138 ? <KeysAndMetadata pointDetails={ pointDetails } />
139 : <div />
140 }
141 { issuedPointList }
142 </Col>
143 </Row>
144 )
145 }
148export default Point