11const nest = require ( 'depnest' )
2- const { h, Value, Dict, dictToCollection, onceTrue, computed, watch, watchAll, throttle } = require ( 'mutant' )
2+ const { h, Value, Dict, dictToCollection, onceTrue, computed, watch, watchAll, throttle, resolve } = require ( 'mutant' )
33const Chart = require ( 'chart.js' )
44const pull = require ( 'pull-stream' )
5+ const { isInvite } = require ( 'ssb-ref' )
56
67const MINUTE = 60 * 1000
78const DAY = 24 * 60 * MINUTE
@@ -42,6 +43,36 @@ exports.create = function (api) {
4243 const state = buildState ( { api, minsPerStep, scale } )
4344 const canvas = h ( 'canvas' , { height : 500 , width : 1200 , style : { height : '500px' , width : '1200px' } } )
4445
46+ const inputInvite = ev => {
47+ state . inviteResult . set ( null )
48+ const invite = ev . target . value . replace ( / ^ \s * " ? / , '' ) . replace ( / " ? \s * $ / , '' )
49+ if ( ! isInvite ( invite ) ) {
50+ state . invite . set ( )
51+ return
52+ }
53+
54+ ev . target . value = invite
55+ state . invite . set ( invite )
56+ }
57+ const useInvite = ( ) => {
58+ state . inviteProcessing . set ( true )
59+
60+ onceTrue ( api . sbot . obs . connection , server => {
61+ server . invite . accept ( resolve ( state . invite ) , ( err , data ) => {
62+ state . inviteProcessing . set ( false )
63+ state . invite . set ( )
64+
65+ if ( err ) {
66+ state . inviteResult . set ( false )
67+ console . error ( err )
68+ return
69+ }
70+ state . inviteResult . set ( true )
71+ console . log ( data )
72+ } )
73+ } )
74+ }
75+
4576 const page = h ( 'NetworkPage' , [
4677 h ( 'div.container' , [
4778 h ( 'h1' , 'Network' ) ,
@@ -65,7 +96,26 @@ exports.create = function (api) {
6596 if ( ! peers . length ) return h ( 'p' , 'No remote peers connected' )
6697
6798 return peers . map ( peer => api . about . html . avatar ( peer ) )
68- } )
99+ } ) ,
100+ h ( 'div.invite' , [
101+ h ( 'input' , {
102+ 'placeholder' : 'invite code for a remote peer (pub)' ,
103+ 'ev-input' : inputInvite
104+ } ) ,
105+ computed ( [ state . invite , state . inviteProcessing ] , ( invite , processing ) => {
106+ if ( processing ) return h ( 'i.fa.fa-spinner.fa-pulse' )
107+ if ( invite ) return h ( 'button -primary' , { 'ev-click' : useInvite } , 'use invite' )
108+
109+ return h ( 'button' , { disabled : 'disabled' , title : 'not a valid invite code' } , 'use invite' )
110+ } ) ,
111+ computed ( state . inviteResult , result => {
112+ if ( result === null ) return
113+
114+ return result
115+ ? h ( 'i.fa.fa-check' )
116+ : h ( 'i.fa.fa-times' )
117+ } )
118+ ] )
69119 ] ) ,
70120 h ( 'section' , [
71121 h ( 'h2' , 'My state' ) ,
@@ -211,7 +261,10 @@ function buildState ({ api, minsPerStep, scale }) {
211261 seq,
212262 replication,
213263 data, // TODO rename this !!
214- range
264+ range,
265+ invite : Value ( ) ,
266+ inviteProcessing : Value ( false ) ,
267+ inviteResult : Value ( null )
215268 }
216269}
217270
0 commit comments