git ssb

10+

Matt McKegg / patchwork



Tree: 2150c96ae49e67fdcb638a88455af2c3ae8c9c53

Files: 2150c96ae49e67fdcb638a88455af2c3ae8c9c53 / main-window.js

4859 bytesRaw
1var combine = require('depject')
2var entry = require('depject/entry')
3var electron = require('electron')
4var h = require('mutant/h')
5var when = require('mutant/when')
6var onceTrue = require('mutant/once-true')
7var computed = require('mutant/computed')
8var catchLinks = require('./lib/catch-links')
9var insertCss = require('insert-css')
10var nest = require('depnest')
11var LatestUpdate = require('./lib/latest-update')
12var ref = require('ssb-ref')
13var setupContextMenuAndSpellCheck = require('./lib/context-menu-and-spellcheck')
14var watch = require('mutant/watch')
15
16
17module.exports = function (config) {
18 var sockets = combine(
19 overrideConfig(config),
20 addCommand('app.navigate', setView),
21 require('./modules'),
22 require('./plugs'),
23 require('patchcore'),
24 require('./overrides')
25 )
26
27 var api = entry(sockets, nest({
28 'config.sync.load': 'first',
29 'keys.sync.id': 'first',
30 'sbot.obs.connection': 'first',
31 'sbot.async.get': 'first',
32 'blob.sync.url': 'first',
33 'page.html.render': 'first',
34 'app.html.search': 'first',
35 'app.html.channels': 'first',
36 'app.views': 'first',
37 'app.sync.externalHandler': 'first',
38 'app.html.progressNotifier': 'first',
39 'profile.sheet.edit': 'first',
40 'app.navigate': 'first'
41 }))
42
43 setupContextMenuAndSpellCheck(api.config.sync.load())
44
45 var id = api.keys.sync.id()
46 var latestUpdate = LatestUpdate()
47
48 // prompt to setup profile on first use
49 onceTrue(api.sbot.obs.connection, (sbot) => {
50 sbot.latestSequence(sbot.id, (_, key) => {
51 if (key == null) {
52 api.profile.sheet.edit()
53 }
54 })
55 })
56
57 var views = api.app.views(api.page.html.render, [
58 '/public', '/private', '/gatherings', id, '/mentions'
59 ])
60
61 var pendingCount = computed([
62 views.get('/public').pendingUpdates,
63 views.get('/private').pendingUpdates
64 ], (...counts) => {
65 return counts.reduce((a, b) => a + b)
66 })
67
68 watch(pendingCount, count => {
69 electron.remote.app.setBadgeCount(count)
70 })
71
72 insertCss(require('./styles'))
73
74 var container = h(`MainWindow -${process.platform}`, [
75 h('div.top', [
76 h('span.history', [
77 h('a', {
78 'ev-click': views.goBack,
79 classList: [ when(views.canGoBack, '-active') ]
80 }),
81 h('a', {
82 'ev-click': views.goForward,
83 classList: [ when(views.canGoForward, '-active') ]
84 })
85 ]),
86 h('span.nav', [
87 tab('Public', '/public'),
88 tab('Private', '/private'),
89 tab('Gatherings', '/gatherings')
90 ]),
91 h('span.appTitle', ['Patchwork']),
92 h('span', [ api.app.html.search(api.app.navigate) ]),
93 h('span.nav', [
94 tab('Profile', id),
95 tab('Mentions', '/mentions')
96 ])
97 ]),
98 when(latestUpdate,
99 h('div.info', [
100 h('a.message -update', { href: 'https://github.com/ssbc/patchwork/releases' }, [
101 h('strong', ['Patchwork ', latestUpdate, ' has been released.']), ' Click here to download and view more info!'
102 ])
103 ])
104 ),
105 api.app.html.progressNotifier(),
106 views.html
107 ])
108
109 catchLinks(container, (href, external) => {
110 if (external) {
111 electron.shell.openExternal(href)
112 } else if (ref.isBlob(href)) {
113 electron.shell.openExternal(api.blob.sync.url(href))
114 } else if (ref.isMsg(href)) {
115 getExternalHandler(href, (err, handler) => {
116 if (err) throw err
117 if (handler) {
118 handler(href)
119 } else {
120 api.app.navigate(href)
121 }
122 })
123 } else {
124 api.app.navigate(href)
125 }
126 })
127
128 return container
129
130 // scoped
131
132 function setView (href) {
133 views.setView(href)
134 }
135
136 function getExternalHandler (key, cb) {
137 api.sbot.async.get(key, function (err, value) {
138 if (err) return cb(err)
139 cb(null, api.app.sync.externalHandler({key, value}))
140 })
141 }
142
143 function tab (name, view) {
144 var instance = views.get(view)
145 return h('a', {
146 'ev-click': function (ev) {
147 if (instance.pendingUpdates && instance.pendingUpdates() && instance.reload) {
148 instance.reload()
149 }
150 },
151 href: view,
152 classList: [
153 when(selected(view), '-selected')
154 ]
155 }, [
156 name,
157 when(instance.pendingUpdates, [
158 ' (', instance.pendingUpdates, ')'
159 ])
160 ])
161 }
162
163 function selected (view) {
164 return computed([views.currentView, view], (currentView, view) => {
165 return currentView === view
166 })
167 }
168}
169
170function overrideConfig (config) {
171 return {
172 'patchwork/config': {
173 gives: nest('config.sync.load'),
174 create: function (api) {
175 return nest('config.sync.load', () => config)
176 }
177 }
178 }
179}
180
181function addCommand (id, cb) {
182 return {
183 [`patchwork/command/${id}`]: {
184 gives: nest(id),
185 create: function (api) {
186 return nest(id, cb)
187 }
188 }
189 }
190}
191

Built with git-ssb-web