git ssb

16+

Dominic / patchbay



Tree: ae00eb7faf88f143a241e7dc3f0bfb6d6c6a55a3

Files: ae00eb7faf88f143a241e7dc3f0bfb6d6c6a55a3 / about / html / edit.js

5683 bytesRaw
1const nest = require('depnest')
2const dataurl = require('dataurl-')
3const hyperfile = require('hyperfile')
4const hypercrop = require('hypercrop')
5const hyperlightbox = require('hyperlightbox')
6const Mutual = require('ssb-mutual')
7
8const {
9 h, Value, Dict: MutantObject, Struct,
10 map, computed, when, dictToCollection
11} = require('mutant')
12const pull = require('pull-stream')
13
14exports.gives = nest('about.html.edit')
15
16exports.needs = nest({
17 'about.obs': {
18 name: 'first',
19 imageUrl: 'first',
20 description: 'first'
21 },
22 'about.obs.groupedValues': 'first',
23 'blob.sync.url': 'first',
24 'keys.sync.id': 'first',
25 'message.html.confirm': 'first',
26 'message.html.markdown': 'first',
27 sbot: {
28 'async.addBlob': 'first',
29 'obs.connection': 'first',
30 'pull.links': 'first'
31 }
32})
33
34exports.create = function (api) {
35 return nest({
36 'about.html.edit': edit
37 })
38
39 // TODO refactor this to use obs better
40 function edit (id) {
41 // TODO - get this to wait till the connection is present !
42 var mutual = Mutual.init(api.sbot.obs.connection())
43
44 var avatar = Struct({
45 current: api.about.obs.imageUrl(id),
46 new: MutantObject()
47 })
48
49 const links = api.sbot.pull.links
50
51 var name = Struct({
52 current: api.about.obs.name(id),
53 new: Value()
54 })
55
56 const images = computed(api.about.obs.groupedValues(id, 'image'), Object.keys)
57
58 var namesRecord = MutantObject()
59 // TODO constrain query to one name per peer?
60 pull(
61 links({dest: id, rel: 'about', values: true}),
62 pull.map(e => e.value.content.name),
63 pull.filter(Boolean),
64 pull.drain(name => {
65 var n = namesRecord.get(name) || 0
66 namesRecord.put(name, n + 1)
67 })
68 )
69 var names = dictToCollection(namesRecord)
70
71 var lb = hyperlightbox()
72
73 var isPossibleUpdate = computed([name.new, avatar.new], (name, avatar) => {
74 return name || avatar.link
75 })
76
77 var avatarSrc = computed([avatar], avatar => {
78 if (avatar.new.link) return api.blob.sync.url(avatar.new.link)
79 return avatar.current
80 })
81
82 var displayedName = computed([name], name => {
83 if (name.new) return name.new
84 else return name.current
85 })
86
87
88 var balances_div = h('div.balances')
89
90 mutual.getAccountBalances(id, (error, balances) => {
91 if (balances == null) return ''
92
93 var balance_els = [];
94 Object.keys(balances).forEach(function(key) {
95 console.log(key)
96 balances_div.appendChild(
97 h('div', `💰 ${balances[key]} ${key}`)
98 )
99 });
100 })
101
102 return h('AboutEditor', [
103 h('section.lightbox', lb),
104 h('section.avatar', [
105 h('section', [
106 h('img', { src: avatarSrc })
107 ]),
108 h('footer', displayedName)
109 ]),
110 h('section.description', computed(api.about.obs.description(id), (descr) => {
111 if (descr == null) return '' // TODO: should be in patchcore, I think...
112 return api.message.html.markdown(descr)
113 })),
114 h('section.credit', balances_div),
115 h('section.aliases', [
116 h('header', 'Aliases'),
117 h('section.avatars', [
118 h('header', 'Avatars'),
119 map(images, image => h('img', {
120 'src': api.blob.sync.url(image),
121 'ev-click': () => avatar.new.set({ link: image })
122 })),
123 h('div.file-upload', [
124 hyperfile.asDataURL(dataUrlCallback)
125 ])
126 ]),
127 h('section.names', [
128 h('header', 'Names'),
129 h('section', [
130 map(names, n => h('div', { 'ev-click': () => name.new.set(n.key()) }, [
131 h('div.name', n.key),
132 h('div.count', n.value)
133 ])),
134 h('input', {
135 placeholder: ' + another name',
136 'ev-keyup': e => name.new.set(e.target.value)
137 })
138 ])
139 ]),
140 when(isPossibleUpdate, h('section.action', [
141 h('button.cancel', { 'ev-click': clearNewSelections }, 'cancel'),
142 h('button.confirm', { 'ev-click': handleUpdateClick }, 'confirm changes')
143 ]))
144 ])
145 ])
146
147 function dataUrlCallback (data) {
148 var el = crop(data, (err, data) => {
149 if (err) throw err
150
151 if (data) {
152 var _data = dataurl.parse(data)
153
154 api.sbot.async.addBlob(pull.once(_data.data), (err, hash) => {
155 if (err) throw err // TODO check if this is safely caught by error catcher
156
157 avatar.new.set({
158 link: hash,
159 size: _data.data.length,
160 type: _data.mimetype,
161 width: 512,
162 height: 512
163 })
164 })
165 }
166 lb.close()
167 })
168 lb.show(el)
169 }
170
171 function clearNewSelections () {
172 name.new.set(null)
173 avatar.new.set({})
174 }
175
176 function handleUpdateClick () {
177 const newName = name.new()
178 const newAvatar = avatar.new()
179
180 const msg = {
181 type: 'about',
182 about: id
183 }
184
185 if (newName) msg.name = newName
186 if (newAvatar.link) msg.image = newAvatar
187
188 api.message.html.confirm(msg, (err, data) => {
189 if (err) return console.error(err)
190
191 clearNewSelections()
192
193 // TODO - update aliases displayed
194 })
195 }
196 }
197}
198
199function crop (d, cb) {
200 var canvas = hypercrop(h('img', {src: d}))
201
202 return h('AboutImageEditor', [
203 h('header', 'Click and drag to crop your avatar.'),
204 canvas,
205 // canvas.selection,
206 h('section.actions', [
207 h('button.cancel', { 'ev-click': () => cb(new Error('canceled')) }, 'cancel'),
208 h('button.okay', { 'ev-click': () => cb(null, canvas.selection.toDataURL()) }, 'okay')
209 ])
210 ])
211}
212

Built with git-ssb-web