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