Skip to content

Commit 18a5b44

Browse files
committed
add invite.accept input to /network page
1 parent 46869ff commit 18a5b44

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

app/page/network.js

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const 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')
33
const Chart = require('chart.js')
44
const pull = require('pull-stream')
5+
const { isInvite } = require('ssb-ref')
56

67
const MINUTE = 60 * 1000
78
const 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

app/page/network.mcss

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ NetworkPage {
1717
margin-left: .5rem
1818
}
1919
}
20+
21+
div.invite {
22+
margin-top: 1rem
23+
24+
display: grid
25+
grid-template-columns: 55rem 8rem 2rem
26+
grid-gap: 1rem
27+
28+
input {
29+
padding-left: .5rem
30+
}
31+
32+
i.fa {
33+
color: fuchsia
34+
justify-self: center
35+
align-self: center
36+
}
37+
}
2038
}
2139
}
2240
}

0 commit comments

Comments
 (0)