Files: d410e696e202f0fa3b384dfa76ad876acb28868b / lib / depject / profile / sheet / edit.js
5170 bytesRaw
1 | const shortFeedId = require('../../about/sync.js') |
2 | const nest = require('depnest') |
3 | const extend = require('xtend') |
4 | const { Value, h, computed, when } = require('mutant') |
5 | const fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' |
6 | const displaySheet = require('../../../sheet/display') |
7 | |
8 | exports.gives = nest('profile.sheet.edit') |
9 | |
10 | exports.needs = nest({ |
11 | 'keys.sync.id': 'first', |
12 | 'message.async.publish': 'first', |
13 | 'sbot.async.publish': 'first', |
14 | 'about.obs': { |
15 | name: 'first', |
16 | description: 'first', |
17 | image: 'first', |
18 | color: 'first' |
19 | }, |
20 | 'blob.html.input': 'first', |
21 | 'blob.sync.url': 'first', |
22 | 'intl.sync.i18n': 'first', |
23 | 'suggest.hook': 'first' |
24 | }) |
25 | |
26 | exports.create = function (api) { |
27 | const i18n = api.intl.sync.i18n |
28 | return nest('profile.sheet.edit', function ({ usePreview = true } = {}) { |
29 | const id = api.keys.sync.id() |
30 | displaySheet(close => { |
31 | const currentName = api.about.obs.name(id) |
32 | const currentImage = api.about.obs.image(id) |
33 | const currentDescription = api.about.obs.description(id) |
34 | |
35 | const publishing = Value(false) |
36 | const chosenImage = Value(currentImage()) |
37 | |
38 | // don't display if name is default |
39 | const chosenName = Value(currentName() === shortFeedId(id) ? '' : currentName()) |
40 | const chosenDescription = Value(currentDescription()) |
41 | |
42 | return { |
43 | content: h('div', { |
44 | style: { |
45 | padding: '20px', |
46 | 'text-align': 'center' |
47 | } |
48 | }, [ |
49 | h('h2', { |
50 | style: { |
51 | 'font-weight': 'normal' |
52 | } |
53 | }, [i18n('Your Profile')]), |
54 | h('ProfileEditor', [ |
55 | h('div.side', [ |
56 | h('ImageInput', [ |
57 | h('img', { |
58 | style: { 'background-color': api.about.obs.color(id) }, |
59 | src: computed(chosenImage, (id) => id ? api.blob.sync.url(id) : fallbackImageUrl) |
60 | }), |
61 | h('span', ['🖼 ', i18n('Choose Profile Image...')]), |
62 | api.blob.html.input((err, file) => { |
63 | if (err) return |
64 | chosenImage.set(file.link) |
65 | }, { |
66 | accept: 'image/*', |
67 | resize: { width: 500, height: 500 } |
68 | }) |
69 | ]) |
70 | ]), |
71 | h('div.main', [ |
72 | h('input.name', { |
73 | disabled: publishing, |
74 | placeholder: i18n('Choose a name'), |
75 | hooks: [ValueHook(chosenName), FocusHook()] |
76 | }), |
77 | h('textarea.description', { |
78 | disabled: publishing, |
79 | placeholder: i18n('Describe yourself (if you want)'), |
80 | hooks: [ValueHook(chosenDescription), api.suggest.hook()] |
81 | }) |
82 | ]) |
83 | ]) |
84 | ]), |
85 | footer: [ |
86 | h('button -save', { |
87 | 'ev-click': save, |
88 | disabled: publishing |
89 | }, when(publishing, |
90 | i18n('Publishing...'), |
91 | usePreview ? i18n('Preview & Publish') : i18n('Publish') |
92 | )), |
93 | h('button -cancel', { |
94 | disabled: publishing, |
95 | 'ev-click': close |
96 | }, i18n('Cancel')) |
97 | ] |
98 | } |
99 | |
100 | function save () { |
101 | // no confirm |
102 | const update = {} |
103 | const newName = chosenName().trim() || currentName() |
104 | |
105 | if (chosenImage() !== currentImage()) update.image = chosenImage() |
106 | if (newName !== currentName()) update.name = newName |
107 | if (chosenDescription() !== currentDescription()) update.description = chosenDescription() |
108 | |
109 | if (Object.keys(update).length) { |
110 | publishing.set(true) |
111 | |
112 | const publish = usePreview |
113 | ? api.message.async.publish |
114 | : api.sbot.async.publish |
115 | publish(extend({ |
116 | type: 'about', |
117 | about: id |
118 | }, update), (err, msg) => { |
119 | publishing.set(false) |
120 | if (err) { |
121 | showDialog({ |
122 | type: 'error', |
123 | title: i18n('Error'), |
124 | buttons: [i18n('OK')], |
125 | message: i18n('An error occurred while attempting to publish about message.'), |
126 | detail: err.message |
127 | }) |
128 | } else if (msg) { |
129 | // user confirmed publish, so close the dialog |
130 | close() |
131 | } |
132 | }) |
133 | } else { |
134 | showDialog({ |
135 | type: 'info', |
136 | title: i18n('Update Profile'), |
137 | buttons: [i18n('OK')], |
138 | message: i18n('Nothing to publish'), |
139 | detail: i18n('You have not made any changes.') |
140 | }) |
141 | close() |
142 | } |
143 | } |
144 | }) |
145 | }) |
146 | } |
147 | |
148 | function FocusHook () { |
149 | return function (element) { |
150 | setTimeout(() => { |
151 | element.focus() |
152 | element.select() |
153 | }, 5) |
154 | } |
155 | } |
156 | |
157 | function ValueHook (obs) { |
158 | return function (element) { |
159 | element.value = obs() |
160 | element.oninput = function () { |
161 | obs.set(element.value.trim()) |
162 | } |
163 | } |
164 | } |
165 | |
166 | function showDialog (opts) { |
167 | const electron = require('electron') |
168 | electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), opts) |
169 | } |
170 |
Built with git-ssb-web